[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Taking input line by line from a config file in Bash

From: Bob Proulx
Subject: Re: Taking input line by line from a config file in Bash
Date: Wed, 2 Jan 2008 11:43:20 -0700
User-agent: Mutt/1.5.13 (2006-08-11)

Matthew_S wrote:
> With the 'while read line', it appears that I don't need to keep track of
> the line numbers.  In fact this below does exactly what I need it to do;


> cat $File |   # Supply input from a file

Search the web for "useless use of cat".  Your use of it here is

But it is more than simply useless here as it causes the while loop to
be run inside of a subshell.  This has subtle effects that often trip
people up.  (e.g. Variables set inside the loop don't apply outside
the subshell.  See Bash FAQ E4 for more information.)

> while read line   # As you suggested...
>         do echo $line
>         echo >> $RESULTS
>         echo $line >> $RESULTS
>         $line
>         [ $? == 0 ] && echo "PASSED" >> $RESULTS || echo "FAILED" >> $RESULTS

Double check the test operator syntax.  (e.g. 'help test' or 'man test')
Use of "==" is a bash extension.  Okay in a #!/bin/bash script
but in #!/bin/sh script that should be a single "=" for portability.

I suggest wrapping the entire group within curly braces and then
redirecting the entire list.  (e.g. { ... ;} >> $RESULTS)

  while read line; do
    echo $line
      echo $line
      $line && echo "PASSED" || echo "FAILED"
    } >> $RESULTS
  done < $File

> exit

I always like to see an explicit 'exit 0' or 'exit 1' as appropriate.
Use of plain 'exit' passes the exit status of the last command that
was executed in accumulator machine style.  That seems counter to the
explicit use of an exit there.  Plus many commands such as 'grep'
overload the return code with status information that is not
appropriate for an exit code elsewhere in the script.  Better to be
explicit here.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]