语法1:
for var in value1 value2 ... # var为变量,value为变量值
do
commands
done
seq命令是用来输出数据的命令,通过反引号` `引起来,具体用法可通过seq --help来查询。
#!bin/bash
for i in `seq 1 9` # 使用命令赋值, 等价于for i in 1 2 3 4 5 6 7 8 9
do
echo $i
done
for i in Marc\'s cool, marc can\'t done well # 未使用引号 相当于多个字符串, 单引号需要转义处理或者使用双引号引起来
do
echo "word: $i"
done
for i in "Marc's cool, marc can't done well" # 引号引起来 相当于是一个字符串
do
echo "word: $i"
done
[marc@localhost Shell]$ bash for1.sh
1
2
3
4
5
word: Marc's
word: cool,
word: marc
word: can't
word: done
word: well
word: Marc\'s cool, marc can\'t done well
注意:for循环的C语言写法,需要使用双括号(( ))。
语法2:
for (( 变量, 变量条件, 自增自减运算 ))
do
commands
done
#(1)、单一循环变量
#!bin/bash
for (( a=0; a<5; a++ )) # 单一循环变量
do
echo $a
done
[marc@localhost Shell]$ bash for2.sh
0
1
2
3
4
# (2)、多循环变量:
#!bin/bash
for (( a=0,b=5; a<5; a++,b--)) # 多循环变量
do
echo $a $b
done
[marc@localhost Shell]$ bash for2.sh
0 5
1 4
2 3
3 2
4 1
#!bin/bash
echo -n "countdown: "
for i in `seq 9 -1 1`
do
echo -e -n "$i\b" # -e表示识别转义字符 -n表示不换行
sleep 1 # 停1秒
done
echo
[marc@localhost Shell]$ bash sleep.sh
countdown: 9 # 动态显示
#!bin/bash
for i in `seq 1 5`
do
if [ $i -eq 3 ]
then
continue # 跳过第3次循环,即本次循环结束
else
echo "$i"
fi
done
[marc@localhost Shell]$ bash continue.sh
1
2
4
5
#!bin/bash
for ((;;)) # 无限循环 写法
do
read -p "char: " ch # 读取键盘输入并打印
if [ $ch == "q" ]
then
break # 如果输入q, 则退出程序
else
echo "char: $ch"
fi
done
[marc@localhost Shell]$ bash break.sh
char: g
char: e
char: t
char: q
[marc@localhost Shell]$
#!bin/bash
for (( i=1; i<10; i++))
do
echo "#loop : $i"
for ((;;))
do
echo "marc is hansome!"
break 2 # break 2 表示直接跳出外层(第2层)循环
done
sleep 3
done
[marc@localhost Shell]$ bash break_1.sh
#loop : 1
marc is hansome!
### 如果为break 1(即break)跳出最内层循环####
[marc@localhost Shell]$ bash break_1.sh
#loop : 1
marc is hansome!
#loop : 2
marc is hansome!
#loop : 3
marc is hansome!
^Z
[2]+ 已停止 bash break_1.sh
while [ conditions ] # 多条件判断 while [ condition ] || [ condition ] || [ condition ]
do
commands
done
#!bin/bash
while [ ! -d ~/Marc] # 家目录下不存在文件夹Marc
do
echo "not found ~/Marc directory"
break
done
[marc@localhost Shell]$ bash while.sh
not found ~/Marc directory
#!bin/bash
while read i # 将$1的内容按行读取 赋值给i变量,然后打印出来
do
echo "$i"
done < $1
[marc@localhost Shell]$ bash while_1.sh /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
......
n=1
while [ $n -lt 10 ]
do
for (( m=1; m<=$n; m++ ))
do
echo -n -e "$m*$n = $((m*n)) \t"
done
echo
n=$((n+1))
done
[marc@localhost Shell]$ bash while.sh
1*1 = 1
1*2 = 2 2*2 = 4
1*3 = 3 2*3 = 6 3*3 = 9
1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16
1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25
1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36
1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49
1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 48 7*8 = 56 8*8 = 64
1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81
格式:(与if/while循环相反,until循环condition为假时执行循环)
until [ condition ] # condition为假时,执行循环
do
commands
done
#!bin/bash
num=10
until [ $num -lt 5 ] # 在condition中,变量的$符号不可少
do
echo $num
num=$((num-1)) # 可以写成num=$(($num-1))
done
[marc@localhost Shell]$ bash until.sh
10
9
8
7
6
5
格式:
case 变量 in
条件1)
commands1
;;
条件2)
commands2
;;
条件3)
...
;;
esac
注意:每个代码块执行完毕要以;;结尾代表结束,case结尾处要以其倒过来写esac为结束
#!bin/bash
case $1 in # $1 表示从命令行传递参数
mom|MOM)
echo "Mom is beautiful!"
;;
dad|DAD) # 符号| 表示任选其一符合即可
echo "Dad is handsome!"
;;
me|ME)
echo "I am so cute!"
;;
*) # 通配符匹配任意参数
echo "USAGE(ignoreacse): please input mom|dad|me "
;;
esac
[marc@localhost Shell]$ bash case.sh xx
USAGE(ignoreacse): please input mom|dad|me
[marc@localhost Shell]$ bash case.sh mom
Mom is beautiful!
[marc@localhost Shell]$ bash case.sh DAD
Dad is handsome!
特殊变量 | 含义 |
---|---|
$0 | Shell本身的文件名 |
$1…$n | 命令行传递的参数,$1是第1参数、$2是第2参数… |
$? | 执行上一个指令的返回值 (显示最后命令的退出状态。如果是0表示执行成功;0以外的值表示失败) |
$* | 代表所有参数, 其间隔为IFS内定参数的第一个字元 |
$@ | 与星号*雷同,代表所有参数, 区别在于不参照IFS |
$# | 代表参数数量 |
$$ | 脚本本身执行的进程号Processing ID |
$_ | 最后一个执行的命令 |
#!bin/bash
echo "shell's name : $0"
echo "shell's parameters : $*"
echo "shell's numbers of parameter : $#"
echo "shell's processing id : $$"
echo "shell's last command : $_"
echo "shell's second parameter : $2"
[marc@localhost Shell]$ bash special_var.sh aa bb cc dd ee
shell's name : special_var.sh
shell's parameters : aa bb cc dd ee
shell's numbers of parameter : 5
shell's processing id : 49823
shell's last command : shell's processing id : 49823
shell's second parameter : bb