|
Like other programming languages, Shell support for loop.
The general format of the for loop:
for variable in list
do
command1
command2
...
commandN
done
List is a sequence of a set of values (numbers, strings, etc.), each of which values are separated by a space. Each time through the loop, it will be next on the list a value is assigned to variable.
in list is optional, if you do not use it, for recycling location command line parameters.
For example, the order of output current list of numbers:
for loop in 1 2 3 4 5
do
echo "The value is: $ loop"
done
The result:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
Sequentially outputs a string of characters:
for str in 'This is a string'
do
echo $ str
done
The result:
This is a string
.bash Display in your home directory file starting with:
#! / Bin / bash
for FILE in $ HOME / .bash *
do
echo $ FILE
done
The result:
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc |
|
|
|