for val in list
do
commands
done
for val in list; do
#!/bin/bash
for name in cky cbh xjc njc fl jjh
do
echo "您的名字是$name"
done
#!/bin/bash
for name in cky cbh xjc njc fl jjh
do
echo "您的名字是$name"
done
echo "最后一个名字为$name"
name=wy
echo "修改后的名字为$name"
#### 错误示范
#!/bin/bash
for word in I dont't know if this'll work
do
echo "单词是:$word"
done
#### 正确操作
#!/bin/bash
for word in I dont\'t know if "this'll" work
do
echo "单词是:$word"
done
#!/bin/bash
echo "展示城市如下:"
for city in New York New Mexico
do
echo "城市是:$city"
done
#!/bin/bash
echo "展示城市如下:"
for city in "New York" "New Mexico"
do
echo "城市是:$city"
done
#!/bin/bash
old_list="wy wwy wwyy wyy"
new_list=$old_list" wwwyyy"
echo $new_list
for name in $new_list
do
echo "name is $name!!"
done
#!/bin/bash
file="name_list"
for name in $(cat $file)
do
echo "name is $name!!"
done
IFS=$'\n'
#!/bin/bash
file="name_list"
IFS=$'\n'
for name in $(cat $file)
do
echo "name is $name!!"
done
IFS=:
如果要指定多个IFS字符,只要将它们在赋值行串起来就行:
IFS=$'\n':;"
这个赋值会将换行符、冒号、分号和双引号作为字段分隔符。如何使用IFS字符解析数据没
有任何限制
IFS_OLD=$IFS
IFS=$'\n'
<在shell代码中使用新的IFS值>
IFS=$IFS_OLD
#!/bin/bash
for file in /root/shell-23-04-10/*
do
if [ -d "$file" ]
then
echo "$file是个目录!!"
elif [ -f "$file" ]
then
echo "$file是个文件!!"
fi
done
if [ -d "$file" ]
if [ -f "$file" ]
#!/bin/bash
for file in /root/shell-23-04-10/* /root/shell-23-04-07/*
do
if [ -d "$file" ]
then
echo "$file是个目录!!"
elif [ -f "$file" ]
then
echo "$file是个文件!!"
fi
done
for (i = 0; i < 10; i++)
{
printf("The next number is %d\n", i);
}
for (( a = 1; a < 10; a++ ))
#!/bin/bash
for (( i = 1; i <= 6; i++))
do
echo "number Is $i"
done
#!/bin/bash
for (( a=1,b=6; a<=6; a++,b-- ))
do
echo "$a - $b"
done
while test command
do
other commands
done
#!/bin/bash
var1=6
while [ $var1 -gt 0 ]
do
echo "var1的值为$var1"
var1=$[ $var1 - 1 ]
done
while [ $var1 -gt 0 ]
var1=$[ $var1 - 1 ]
#!/bin/bash
var1=6
while echo $var1
[ $var1 -ge 0 ]
do
echo "循环内!!"
var1=$[ $var1 - 1 ]
done
第一个测试简单地显示了var1变量的当前值。第二个测试用方括号来判断var1变量的值。在循环内部,echo语句会显示一条简单的消息,说明循环被执行了
while循环会在var1变量等于0时执行echo语句,然后将var1变量的值减一。接下来再次执行测试命令,用于下一次迭代。echo测试命令被执行并显示了var变量的值(现在小于0了)。直到shell执行test测试命令,whle循环才会停止
这说明在含有多个命令的while语句中,在每次迭代中所有的测试命令都会被执行,包括测试命令失败的最后一次迭代。要留心这种用法。另一处要留意的是该如何指定多个测试命令。注意,每个测试命令都出现在单独的一行上
until test commands
do
other commands
done
#!/bin/bash
var1=100
until [ $var1 -eq 0 ]
do
echo "var1的值为$var1!!"
var1=$[ $var1 - 25 ]
done
#!/bin/bash
for (( a = 1; a <= 3; a++ ))
do
echo "第一层循环$a"
for (( b = 1; b <= 3; b++ ))
do
echo " 第二层循环$b"
done
done
#!/bin/bash
a=3
while [ $a -ge 0 ]
do
echo "外层循环值为:$a"
for (( b = 1; $b < 3; b++))
do
c=$[ $a * $b ]
echo "内部循环:$a * $b = $c"
done
a=$[ $a - 1 ]
done
#!/bin/bash
var1=3
until [ $var1 -eq 0 ]
do
echo "外层循环为$var1"
var2=1
while [ $var2 -lt 5 ]
do
var3=$(echo "scale=4;$var1 / $var2" | bc)
echo " 内层循环为$var1 / $var2 = $var3"
var2=$[ $var2 + 1]
done
var1=$[ $var1 - 1 ]
done
#!/bin/bash
IFS_OLD=$IFS
IFS=$'\n'
for line in $(cat cp-passwd)
do
echo "行值为$line"
IFS=:
for value in $line
do
echo " 值为$value"
done
done
#!/bin/bash
num_list="11 22 33 44 55 66 77 88"
for var in $num_list
do
if [ $var -eq 66 ]
then
break
fi
echo "当前的值为$var"
done
#!/bin/bash
for (( a = 1; a < 4; a++ ))
do
echo "外层循环的值为$a"
for (( b = 1; b < 1000; b++ ))
do
if [ $b -eq 5 ]
then
break
fi
echo " 内部循环值为$b"
done
done
break n
#!/bin/bash
for (( a = 1; a < 4; a++ ))
do
echo "外层循环值为$a"
for (( b = 1; b < 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " 内层循环值为$b"
done
done
#!/bin/bash
for (( var1 = 1; var1 < 15; var1++))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "当前值为:$var1"
done
#!/bin/bash
# 不恰当举例
var1=0
while echo "外部值为:$var1"
[ $var1 -lt 15 ]
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo " 内部值为:$var1"
var1=$[ $var1 + 1 ]
done
braek n
#!/bin/bash
for (( a = 1; a <= 5; a++ ))
do
echo "外部值为:$a"
for (( b = 1; b < 3; b++ ))
do
if [ $a -gt 2 ] && [ $a -lt 4 ]
then
continue
fi
var3=$[ $a * $b ]
echo " 变量var3的值为:$var3"
done
done
....
....
done > output.txt
#!/bin/bash
for (( a = 1; a <= 10; a++ ))
do
echo "变量a的值为:$a"
done > output-a.txt
#!/bin/bash
for num in 9 5 7 1 6 4 2
do
echo "排序好的数字为:$num"
done | sort
echo "排序结束!!"