Prev (Escape Characters) | Next (Test) |
for
loops iterate through a set of values until the list is exhausted:
#!/bin/sh for i in 1 2 3 4 5 do echo "Looping ... number $i" done
#!/bin/sh for i in hello 1 * 2 goodbye do echo "Looping ... i is set to $i" done
*
and grasp the idea, then re-read
the Wildcards section and try it again with
the *
in place. Try it also in different directories, and with the *
surrounded by
double quotes, and try it preceded by a backslash (\*
)
while
loops can be much more fun! (depending on your idea of fun, and how
often you get out of the house... )
#!/bin/sh INPUT_STRING=hello while [ "$INPUT_STRING" != "bye" ] do echo "Please type something in (bye to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING" done
INPUT_STRING=hello
before testing it. This makes
it a repeat loop, not a traditional while loop.
The colon (:
) always evaluates to true; whilst using
this can be necessary sometimes, it is often preferrable to use a real
exit condition. Compare quitting the above loop with the one below;
see which is the more elegant. Also think of some situations in which
each one would be more useful than the other:
#!/bin/sh while : do echo "Please type something in (^C to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING" done
while read f
loop. This example uses the case statement,
which we'll cover later. It reads from the file myfile
, and for each line, tells you what language it thinks is being used. Thanks to Asmus for a number of improvements to this section.
#!/bin/sh while read f do case $f in hello) echo English ;; howdy) echo American ;; gday) echo Australian ;; bonjour) echo French ;; "guten tag") echo German ;; *) echo Unknown Language: $f ;; esac done < myfile
#!/bin/sh while f=`line` do .. process f .. done < myfile
while read f
works with any *nix, and doesn't depend on the
external program line
, the former is preferable. See External
Programs to see why this method uses the backtick (`).$i
in the default
("Unknown Language") case above - you will get no warnings or errors in this case, even though
$i
has not been declared or defined. For example:
$ i=THIS_IS_A_BUG $ export i $ ./while3.sh something Unknown Language: THIS_IS_A_BUG $So make sure that you avoid typos. This is also another good reason for using
${x}
and not just $x
- if x="A"
and you want to say "A1", you need
echo ${x}1
, as echo $x1
will try to use the variable x1
, which
may not exist, or may be set to B2
.
mkdir rc{0,1,2,3,4,5,6,S}.dinstead of the more cumbersome:
for runlevel in 0 1 2 3 4 5 6 S do mkdir rc${runlevel}.d doneAnd this can be done recursively, too:
$ cd / $ ls -ld {,usr,usr/local}/{bin,sbin,lib} drwxr-xr-x 2 root root 4096 Oct 26 01:00 /bin drwxr-xr-x 6 root root 4096 Jan 16 17:09 /lib drwxr-xr-x 2 root root 4096 Oct 27 00:02 /sbin drwxr-xr-x 2 root root 40960 Jan 16 19:35 usr/bin drwxr-xr-x 83 root root 49152 Jan 16 17:23 usr/lib drwxr-xr-x 2 root root 4096 Jan 16 22:22 usr/local/bin drwxr-xr-x 3 root root 4096 Jan 16 19:17 usr/local/lib drwxr-xr-x 2 root root 4096 Dec 28 00:44 usr/local/sbin drwxr-xr-x 2 root root 8192 Dec 27 02:10 usr/sbin
Prev (Escape Characters) | Next (Test) |