Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1441040
  • 博文数量: 165
  • 博客积分: 2068
  • 博客等级: 上尉
  • 技术积分: 2102
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-27 16:07
文章分类

全部博文(165)

文章存档

2018年(1)

2017年(22)

2016年(9)

2015年(22)

2014年(8)

2013年(25)

2012年(53)

2011年(25)

分类: Python/Ruby

2015-10-11 22:27:23

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
 

阅读(1417) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~