预备知识
内部字段分隔符(Internal Field Separator, IFS)
- IFS读取变量中的每一个条目
[root@shaofeng ~]# ./ifs.sh
Item: name
Item: sex
Item: rollno
Item: location
[root@shaofeng ~]# cat ./ifs.sh
#!/bin/bash
export data="name, sex, rollno, location"
oldIFS=$IFS
IFS=, #now
for item in $data;
do
echo Item: $item
done
实战演练
- ifs结合循环
[root@shaofeng ~]# ./ifs.sh
wo buai shaofeng
[root@shaofeng ~]# cat ./ifs.sh
#!/bin/bash
line="wo:ai:buai"
IFS=:
count=0
for item in $line;
do
[ $count -eq 0 ] && user=$item;
[ $count -eq 2 ] && do=$item;
let count++;
done
echo $user $do shaofeng
- for循环
for var in list;
do
commands;
done
- while循环
while condition
do
commands;
done
- until循环,直到到达条件
[root@shaofeng ~]# ./util.sh
0
1
[root@shaofeng ~]# cat ./util.sh
#!/bin/bash
count=0
until [ $count -eq 2 ];
do
echo $count
let count++;
done