for var in list
do
commands
done
for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done
echo "The last state we visited was $test"
for test in I don't know if this'll work
for test in I don\'t know if "this'll" work
for test in Nevada New Hampshire New Mexico New York North Carolina
for test in Nevada "New Hampshire" "New Mexico" "New York"
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"
for state in $list
file="states.txt"
for state in $(cat $file)
$ cat states.txt
Alabama
Alaska
Arizona
$ ./test5
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
IFS=$'\n'
file="states.txt"
IFS=$'\n'
for state in $(cat $file)
IFS.OLD=$IFS
IFS=$'\n'
<在代码中使用新的 IFS 值>
IFS=$IFS.OLD
IFS=:
IFS=$'\n:;"'
for file in /home/rich/test/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done
for file in /home/rich/.b* /home/rich/badtest
for (( variable assignment ; condition ; iteration process ))
for (( i=1; i <= 10; i++ ))
do
echo "The next number is $i"
done
for (( a=1, b=10; a <= 10; a++, b-- ))
while test command
do
other commands
done
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done
var1=10
while echo $var1
[ $var1 -ge 0 ]
do
echo "This is inside the loop"
var1=$[ $var1 - 1 ]
done
until test commands
do
other commands
done
$ cat test12
#!/bin/bash
# using the until command
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1 - 25 ]
done
var1=100
until echo $var1
[ $var1 -eq 0 ]
do
echo Inside the loop: $var1
var1=$[ $var1 - 25 ]
done
#!/bin/bash
# changing the IFS value
IFS.OLD=$IFS
IFS=$'\n'
for entry in $(cat /etc/passwd)
do
echo "Values in $entry –"
IFS=:
for value in $entry
do
echo " $value"
done
done
$
for var1 in 1 2 3 4 5 6 7 8 9 10
do
if [ $var1 -eq 5 ]
then
break
fi
echo "Iteration number: $var1"
done
for (( a = 1; a < 4; a++ ))
do
echo "Outer loop: $a"
for (( b = 1; b < 100; b++ ))
do
if [ $b -eq 5 ]
then
break
fi
echo " Inner loop: $b"
done
done
break n
for (( a = 1; a < 4; a++ ))
do
echo "Outer loop: $a"
for (( b = 1; b < 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Inner loop: $b"
done
done
for (( var1 = 1; var1 < 15; var1++ ))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number: $var1"
done
continue n
for file in /home/rich/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif
echo "$file is a file"
fi
done > output.txt
for state in "North Dakota" Connecticut Illinois Alabama Tennessee
do
echo "$state is the next place to go"
done | sort
实战演练-查找可执行文件
$ cat test25
#!/bin/bash
# finding files in the PATH
IFS=:
for folder in $PATH
do
echo "$folder:"
for file in $folder/*
do
if [ -x $file ]
then
echo " $file"
fi
done
done
实战演练-创建多个用户账户
$ cat test26
#!/bin/bash
# process new user accounts
input="users.csv"
while IFS=',' read -r loginname name
do
echo "adding $loginname"
useradd -c "$name" -m $loginname
done < "$input"
$ cat users.csv
rich,Richard Blum
christine,Christine Bresnahan