#!/usr/bin/env bashfor x in one two three fourdo echo number $xdoneoutput:number onenumber two number three number four
What exactly happened? The "for x" part of our "for" loop defined a new environment variable (also called a loop control variable) called "$x", which was successively set to the values "one", "two", "three", and "four". After each assignment, the body of the loop (the code between the "do" ... "done") was executed once. In the body, we referred to the loop control variable "$x" using standard variable expansion syntax, like any other environment variable. Also notice that "for" loops always accept some kind of word list after the "in" statement. In this case we specified four English words, but the word list can also refer to file(s) on disk or even file wildcards. Look at the following example, which demonstrates how to use standard shell wildcards:
#!/usr/bin/env bashfor myfile in /etc/r*do if [ -d "$myfile" ] then echo "$myfile (dir)" else echo "$myfile" fidoneoutput:/etc/rc.d (dir)/etc/resolv.conf/etc/resolv.conf~/etc/rpc
The above code looped over each file in /etc that began with an "r". To do this, bash first took our wildcard /etc/r* and expanded it, replacing it with the string /etc/rc.d /etc/resolv.conf /etc/resolv.conf~ /etc/rpc before executing the loop. Once inside the loop, the "-d" conditional operator was used to perform two different actions, depending on whether myfile was a directory or not. If it was, a " (dir)" was appended to the output line.
We can also use multiple wildcards and even environment variables in the word list:
for x in /etc/r??? /var/lo* /home/drobbins/mystuff/* /tmp/${MYPATH}/*do cp $x /mnt/mydirdone
Bash will perform wildcard and variable expansion in all the right places, and potentially create a very long word list.
While all of our wildcard expansion examples have used absolute paths, you can also use relative paths, as follows:
for x in ../* mystuff/*do echo $x is a silly filedone
In the above example, bash performs wildcard expansion relative to the current working directory, just like when you use relative paths on the command line. Play around with wildcard expansion a bit. You'll notice that if you use absolute paths in your wildcard, bash will expand the wildcard to a list of absolute paths. Otherwise, bash will use relative paths in the subsequent word list. If you simply refer to files in the current working directory (for example, if you type "for x in *"), the resultant list of files will not be prefixed with any path information. Remember that preceding path information can be stripped using the "basename" executable, as follows:
for x in /var/log/*do echo `basename $x` is a file living in /var/logdone
Of course, it's often handy to perform loops that operate on a script's command-line arguments. Here's an example of how to use the "$@" variable, introduced at the beginning of this article:
#!/usr/bin/env bashfor thing in "$@"do echo you typed ${thing}.doneoutput:$ allargs hello there you sillyyou typed hello.you typed there.you typed you.you typed silly.
联系客服