Re: Testing for loops

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
Date: 05/26/05


Date: Thu, 26 May 2005 15:25:15 GMT

On 2005-05-26, InqOne <ml@hpcaz.com> wrote:
> William:
>
> In a previous post I mentioned that I was having issues getting
> commands in the while loop to read "x junk". In the for loops, I use
> the command "`$awk '{print $1}' $mvd`" to define $x. However, this
> doesn't seem to work the same way with while. I tried this while loop
>
> cd /home/virtual
> x=`$awk '{print $1}' $mvd`
> while read x
> do
> if ! cmp -s $x/etc/aliases $x/etc/aliases.tom
> then
> echo "aliases is different"
> exit 0
> fi
> if ! cmp -s $x/etc/passwd $x/etc/passwd.tom
> then
> echo "passwd is different"
> exit 0
> fi
> done
>
> $x is read correctly by BASH but isn't picked up by the commands "if !
> cmp..." This is where my problem is. Am I doing something wrong here?
> I've tried a few different variations of this while loop without
> success. In the example, "x=" is outside of the loop so this of course
> won't work...however, how do I make that part of the loop so that my
> variable changes with each iteration?

As you have been told a number of times, you need to pipe the output of
awk into the 'while loop'. And as you have also been told a number of
times you do not need to use awk at all.

The construct

while read x junk
do
        ....
done < filename

will read the filename one line at a time, putting the first "word" into
the variable x, and the rest of the line into the variable junk.

> As for the use of "explicits", I use them for a few reasons:
>
> 1) It helps with coding efficiancy. It's much quicker to type the
> variable name than to type the full path each time.

It can also increase obscurity, what does the following do?

        $ln /etc/passwd /tmp/newpasswd

The answer is that you don't know, you have to find where the variable
"ln" is defined. Now by convention you may know that $ln has the value
"/bin/ln", but what happens if someone decides that they always want to
use the '-f' flag to it. They might choose to add it to the variable.

Your own coding style is up to you of course, but yours is uncommon, and
makes it harder for other people to understand your scripts.

> 2) Some time ago, I read that it was more efficient to use the full
> path to a binary than to just specifiy the executable name. The theory
> was that only specifiying the executable name (and not the full path)
> would force a search of each directory in $PATH, including the
> "current" directory from where the script is being run.i

This is bogus. Modern shells cache the locations where they find
commands, so you only need to search once. Try timing a script written
in each style, and see if you can find a difference. The current
directory is only searched if it is specified in the PATH, and only if a
command is not found before that point. You may well not even have '.'
in your PATH, and if you do then it probably is near the end.

> Variables make
> it easier to do this as I can specifiy the full path and still keep my
> keystroaks to a minimum while writing the scripts.

If keystrokes realy are a concern, then look into things like the :ab
command in vi, or the abbrev mode in emacs. You can then create a
startup file for your editor and never have to type awk=/usr/bin/awk
again in a script.

> 3) Though it's not currently an issue, I wanted to ensure that the
> script was built as portable as possible between other Linux
> distrobutions and (possibly) platforms. Using aliases would allow
> someone to change a few variables to fix "broken" paths for their
> platform.

And if you just used the raw command name, they wouldn't have to fix
anything!



Relevant Pages

  • Re: probleme with read command in a loop
    ... >I made a script to remove accentuated characters from a series of file name ... Because the standard input (file descriptor 0) is redirected within ... the loop since your loop is at the end of a pipe-line. ... is the output of the find command from the previous pipe. ...
    (comp.unix.shell)
  • Re: Strange errors
    ... script, or at another point in the loop. ... you can do classin the command window at that ... why does the same exact code not ...
    (comp.soft-sys.matlab)
  • Re: unix pipes to perl scripts
    ... > the loop accepts the piped files but it also interferes with my STDIN! ... The STDIN of your pp script is redirected from the keyboard to the ... STDOUT of the ls command. ...
    (comp.lang.perl.misc)
  • Re: searching synchronously or asynchronously
    ... computer and immediately return to the loop, ... I have the below script. ... Is there a way to run the script, connect to all computers in the command ... If fName NetPath and fName LocalPath and _ ...
    (microsoft.public.scripting.wsh)
  • Re: Testing for loops
    ... commands in the while loop to read "x junk". ... $x is read correctly by BASH but isn't picked up by the commands "if! ... path to a binary than to just specifiy the executable name. ... "current" directory from where the script is being run. ...
    (comp.unix.shell)