do | |
done | df -h |
done |
The for..in control structure is great for looping over a range of values. This loop will display the status of the
for SERVICE in httpd ftpd NetworkManager
do
/sbin/service $SERVICE status
done
for...in is even more useful when the list of values is specified as an ambiguous filename. In this script, the loop is repeated once for each file in the directory
mkdir backup
for FILE in /etc/*.conf
do
echo 'Backing up the file $FILE...'
cp $FILE backup/
done
For the if and while control structures, a
For example, the
if who | grep -q helen
then
echo 'Helen is logged in!'
fi
The exit status of the last command is taken as the exit status of a pipeline, which is
The built-in command
Table 4-19. Common bash conditional operators
Operator | Tests whether... | Example using an environment variable |
---|---|---|
-f | File exists and is a regular file | -f '$A' |
-d | File exists and is a directory | -d '$B' |
-r | File exists and is readable | -r '$C' |
-w | File exists and is writable | -w '$D' |
-x | File exists and is executable | -x '$E' |
Strings match | '$F' == 'red' | |
Strings don't match | '$G' != 'blue' | |
Integer values are equal | '$H' -eq 2 | |