Bash学习笔记03 : 更多结构化命令 for和 while

For

1 基本格式:

for var in list 

do

command

done

举例:

for test in Beijing Tianjin Wuhan
do
    echo "the next station is $test"
done

结果:

the next station is Beijing
the next station is Tianjin
the next station is Wuhan


2 读取列表中的复杂值

如果列表中有引号

for test in I don't know how if this's still work
do
    echo "$test"
done

得出的结果将是:

I
dont know how if thiss
still
work


对于这种情况,使用反斜线或者双引号

for test in I don\'t know how if "this's" still work
do
    echo "$test"
done

结果:

I
don't
know
how
if
this's
still
work


for根据空格来分隔单词

如果单词中有空格,也应该用双引号引起来.


3 从变量中读取列表

cities="Changsha Wuhan Beijing"
cities=$cities" ShengZhen"
for test in $cities 
do
    echo "$test"
done


cities中包含了列表,注意这种往字符串后面添加字符串的方法


4 从命令中读取值

for line in `cat test2.sh`
do
    echo "$line"
done

将test2.sh的每个单词都读出来了


5 更改字段分隔符

默认情况下,bash shell 会将下列字符当做分隔符: 

空格

制表符

换行符


如果bash shell在数据中看到任意一个,它就会假定你在列表中开始了新的数据段.

IFS=$'\n'
for line in `cat test2.sh`
do
    echo "$line"
done

这样test2.sh的内容就是按行输出的

需要注意的是如果处理长脚本时,要注意保存IFS原来的值,处理完后恢复到默认值

IFS.OLD=$IFS

#commad block

IFS=$IFS.OLD


如果要指定多个分隔符  连起来即可 

IFS=$'\n:;' 风格符为换行符 冒号 分号

不要写 IFS="\n" 或者 '\n' 这不是指以\和n作为分隔符


6 用通配符读取目录

遍历文件目录时,必须使用通配符,它会强制shell使用文件扩展匹配

for file in /home/spider/*
do
    if [ -d "$file" ];then
        echo "$file is a directory"
    elif [ -f "$file" ];then
        echo "$file is a file"
    fi
done


注意"$file"请用双引号,否则如果遇到含有空格的文件名或目录名时有错误产生

"[ too many arguments"


7 c风格的for命令

for(( a = 1; a < 2*5; a++))

do

echo "the number is $a"

done

结果:

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


有一些没有遵守shell语法:

赋值可以写空格

条件中的变量不以美元符号开头

迭代过程中的算式不写expr


还可以使用多个变量

for(( a = 1,b=1; a < 2*5; a++,b--))
但要注意,判断条件只能写一句


while

1 基本格式

while test command 

do 

other command

done


例子:

var=5;
while [ $var -gt 0 ] 
do
    echo "$var"
    var=$[$var-1]
done


2 使用多个测试命令

var=5;
while echo $var
    [ $var -gt 0 ] 
do
    echo "var=$var"
    var=$[$var-1]
done

多个测试命令要分行写,只有最后一个命令的退出码对while有效

5
var=5
4
var=4
3
var=3
2
var=2
1
var=1
0

注意结尾,会多打印了一个0 .


3 嵌套循环

循环处理文件数据

当你使用\n作为分隔符时,你可以取出文件的行,但要分析每个字段,需要改变分隔符并再一次循环

经典例子: 处理/etc/passwd里面的数据 

/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
adm:x:3:4:adm:/var/adm:/sbin/nologin



IFSOLD=$IFS
IFS=$'\n'
for line in `cat /etc/passwd`
do
    echo "entry line"
    IFS=':' 
    for word in $line
    do
        echo $word
    done    
done

IFS=$IFSOLD


4 循环控制

break 和 continue 的用法跟C语言中一样

但是可以跳出多重循环

break 2 跳出2重循环


5  循环的输出重定向

在done 后面接 > 重定向到文件,或者 | 管道到其他程序

for test in I don\'t know how if "this's" still work
do
    echo "$test"
done > output



while echo $var
    [ $var -gt 0 ] 
do
    echo "var=$var"
    var=$[$var-1]
done | sort




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值