IFS(Internal Field Separator) 内部字段分隔符
1.
chicol@debian:~/scripts$ vi ifs.sh
#!/bin/bash
# FileName: ifs.sh
data="name,sex,rollno,location"
oldIFS=$IFS
IFS=, #now
for item in $data
do
echo Item: $item
done
IFS=$oldIFS
chicol@debian:~/scripts$ ./ifs.sh
Item: name
Item: sex
Item: rollno
Item: location
chicol@debian:~/scripts$
2.
#!/bin/bash
# FileName: ifs2.sh
line="root:x:0:0:root:/root:/bin/bash"
oldIFS=$IFS
IFS=:
count=0
for item in $line
do
[ $count -eq 0 ] && user=$item;
[ $count -eq 6 ] && shell=$item;
let count++
done
IFS=$oldIFS
echo $user\'s shell is $shell.
chicol@debian:~/scripts$ ./ifs2.sh
root's shell is /bin/bash.
循环:
1.for循环
for var in list
do
commands;
done
list可以是字符串,也可以是一个序列
数字列表:{1..50}
字母列表:{a..z} {A..Z}
for i in {a..z}; do actions; done;
for循环也可以采用c语言for循环的格式:
for ((i=0;i<10;i++))
{
commands;
}
2. while循环:当条件为真时执行循环
while condition
do
commands;
done
用true作为循环条件能产生无限循环
3. until循环:会一直循环,直到条件为真
until condition
do
commands;
done
阅读(1469) | 评论(0) | 转发(0) |