循环处理文件数据
xin@xin ~/Test $ cat 2.sh
#!/bin/bash
IFS.OLD=$IFS
for entery in `cat /etc/passwd` ; do
echo "Values in $entery -"
IFS=:
for value in $entery ; do
echo " $value"
done
done
xin@xin ~/Test $ bash 2.sh
2.sh: 行 2: IFS.OLD=: 未找到命令
Values in root:x:0:0:root:/root:/bin/bash -
root
x
0
0
root
/root
/bin/bash
break退出循环
continue退出本轮循环
循环输出的处理
xin@xin ~/Test $ cat 3.sh
#!/bin/bash
for (( a=1; a < 10; a++ )) ; do
echo "The number is $a"
done > 123.txt
xin@xin ~/Test $ bash 3.sh
xin@xin ~/Test $ cat 123.txt
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
脚本中$0指程序名,$1-$n指第几个参数,对于多位要使用$(10)
basename用来获得文件名
$#脚本运行时参数个数
${!#}最后一个参数
$*将所有参数当成单个参数,$@变量会单独处理每个参
shift用来移动参数