ITPub博客

首页 > Linux操作系统 > Linux操作系统 > linuxSHELL学习之for、while、until

linuxSHELL学习之for、while、until

原创 Linux操作系统 作者:haha牛 时间:2012-07-03 14:31:53 0 删除 编辑

一、for命令用于循环操作
1、读取列表中的复杂值
*使用转义字符(反斜杠符号)来转义单引号
*使用双引号来定义使用单引号的值
[root@t1 ~]# cat t20.sh
#!/bin/bash
for test in I don't know if this'll work
do
  echo "word:$test"
done

[root@t1 ~]# ./t20.sh
word:I
word:dont know if thisll ---不是我们想要的结果
word:work
[root@t1 ~]# cat t21.sh
#!/bin/bash
for test in I don\'t know if "this'll" work  --使用 "\"转义单引号,使用双引号定义使用单引号的值
do
 echo "word:$test"
done
 
[root@t1 ~]# ./t21.sh
word:I
word:don't
word:know
word:if
word:this'll
word:work
2、运行多字值,for循环认为每个值都用空格分开,如果有包含空格的数据值,就会遇到另一个问题:又包含空格的值就必须用双引号引起来使用。
[root@t1 ~]# cat t22.sh
#!/bin/bash
for test in New Hampshire New York New Mexico
do
  echo "Now going to $test"
done

[root@t1 ~]# ./t22.sh    ---不是想要的结果
Now going to New
Now going to Hampshire
Now going to New
Now going to York
Now going to New
Now going to Mexico
[root@t1 ~]# cat t23.sh
#!/bin/bash
for test in "New Hampshire" "New York" "New Mexico"  ---使用双引号之后得到想要的结果
do
 echo "Now going to $test"
done

[root@t1 ~]# ./t23.sh
Now going to New Hampshire
Now going to New York
Now going to New Mexico
3、从变量读取列表
[root@t1 ~]# cat t24.sh
#!/bin/bash
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"    ---注意要有空格
for state in $list
do
  echo "Have you ever visited $state?"
done
[root@t1 ~]# ./t24.sh
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?
使用list=$list" Connecticut"在末尾新添加了一个文本字符串   *注意有空格
4、读取命令中的值
[root@t1 ~]# cat states
Alabama
Alaska
Arizona
Colorado
Florida

[root@t1 ~]# cat t25.sh 
#!/bin/bash
#reading values from a file
file="states"       ---注意:states和t25.sh在同一个目录中,否则要写出绝对路径或相对路径
for state in `cat $file`
do
  echo "Visit beatiful $state"
done
[root@t1 ~]# ./t25.sh
Visit beatiful Alabama
Visit beatiful Alaska
Visit beatiful Arizona
Visit beatiful Colorado
Visit beatiful Florida
5、改变字段分隔符:bash shell中的内部字段分隔符(IFS)环境变量定义了用于字段分隔符的列表,默认情况下有:空格、制表符、换行符
要更改默认字段分隔符可以更改黄静变量IFS的值,例如将IFS的值更改为只识别换行符(IFS=$'\n')
[root@t1 ~]# cat states
Alabama New Alaska
Arizona Colorado Florida

[root@t1 ~]# cat t25.sh
#!/bin/bash
#reading values from a file
file="states"
IFS=$'\n'     ---定义字段分隔符为换行
for state in `cat $file`
do
  echo "Visit beatiful $state"
done
[root@t1 ~]# ./t25.sh
Visit beatiful Alabama New Alaska
Visit beatiful Arizona Colorado Florida
为了确保使用过的IFS字符便会原来的默认值,可以在使用前后进行如下操作:
IFSOLD=$IFS ---使用IFSOLD保存原来的默认值
IFS=$'\n':;"  ---定义为自定义的分隔符
IFS=$IFSOLD  ---还原为默认值
6、使用通配符读取目录
[root@t1 ~]# cat t26.sh
#!/bin/bash
for file in /root/*
do
  if [ -d "$file" ]
  then
    echo "$file is a directory"
  elif [ -f "$file" ]
  then
    echo "$file is a file"
  fi
done
[root@t1 ~]# ./t26.sh
/root/anaconda-ks.cfg is a file
/root/Desktop is a directory
/root/install.log is a file
/root/install.log.syslog is a file
/root/l is a file
/root/readline-5.0 is a directory
/root/rlwrap-0.37 is a directory
/root/states is a file
/root/t13.sh is a file
/root/t14.sh is a file
*$file加引号是因为在linux中,包含空格的路径和文件名是合法的,要容纳他们,应该使用双引号将变量$file包围起来,如果不这样做,
遇到包含空格的路径或文件时就会出错。
二、C式的for循环
例:for (( a=1; a<=10; a++ ))
*变量的赋值可以包含空格;
*条件中的变量不以美元符号做前缀;
*迭代处理式不适用expr命令格式;
[root@t1 ~]# cat t27.sh
#!/bin/bash
for (( i=1; i<=10; i++ ))
do
  echo "the next number is $i"
done

[root@t1 ~]# ./t27.sh
the next number is 1
the next number is 2
the next number is 3
the next number is 4
the next number is 5
the next number is 6
the next number is 7
the next number is 8
the next number is 9
the next number is 10
1、使用多个变量
[root@t1 ~]# cat t28.sh
#!/bin/bash
for ((i=1,b=10; i<=10; i++,b--))
do
  echo "$i-$b"
done

[root@t1 ~]# ./t28.sh
1-10
2-9
3-8
4-7
5-6
6-5
7-4
8-3
9-2
10-1
三、while命令
1、while的基本格式
while test command
do
  other commands
done
[root@t1 ~]# cat t29.sh
#!/bin/bash
#while command test
var1=10
while [ $var1 -gt 0 ]
do
  echo "$var1"
  var1=$[ $var1-1 ]
done

[root@t1 ~]# ./t29.sh
10
9
8
7
6
5
4
3
2
1
2、使用多条测试命令
while命令允许在while语句行定义多条test命令。只有最后一条测试命令的推出状态是用来决定循环是合适停止的。
[root@t1 ~]# cat t30.sh
#!/bin/bash
#testing a multicommand while loop
var1=10
while echo $var1
    [ $var1 -ge 0 ]   ---**每一行代表一个test command,即[ $var1 -ge 0 ]必须在新的一行。
do
  echo "this is inside the loop"
  var1=$[ $var1 - 1 ]
done

[root@t1 ~]# ./t30.sh
10
this is inside the loop
9
this is inside the loop
8
this is inside the loop
7
this is inside the loop
6
this is inside the loop
5
this is inside the loop
4
this is inside the loop
3
this is inside the loop
2
this is inside the loop
1
this is inside the loop
0
this is inside the loop
-1
四、until命令
until命令刚好与while命令相反,until命令需要制定一条测试命令,这条命令通常产生一个非0的退出状态,只要测试命令的退出状态非0,
bash shell就会执行列在循环当中的命令。一旦测试条件返回0退出状态,循环停止。
until test commands
do
   other commands
done
和while 一样,可以在until命令语句中使用多条测试命令,只有最后一条命令的推出状态能够决定bash shell是否执行其他的命令。
[root@t1 ~]# cat t31.sh
#!/bin/bash
#using the until command
var1=100
until [ $var1 -eq 0 ]
do
   echo $var1
   var1=$[ $var1 - 25 ]
done
[root@t1 ~]# ./t31.sh
100
75
50
25
一旦变量var1的值等于0,until命令就停止循环。
*多条测试命令
[root@t1 ~]# cat t32.sh
#!/bin/bash
#using the until command
var1=100
until echo $var1
      [ $var1 -eq 0 ]   ----注意要新起一行或者这样写 until echo $var1 ; [ $var1 -eq 0 ]  
 do
   echo "Inside the loop:$var"
   var1=$[ $var1 - 25 ]
done
[root@t1 ~]# ./t32.sh
100
Inside the loop:
75
Inside the loop:
50
Inside the loop:
25
Inside the loop:
0

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23655288/viewspace-734467/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-04-04

  • 博文量
    102
  • 访问量
    298525