shell 中的for、while循环及if语句

shell 中的for、while循环及if语句

shell与其他语言一样也支持for、while循环

for循环的一般格式如下:

1 #!/bin/sh
2
3 for 变量 in 列表
4 do
5 command 1
6 command 2
7 command 1
8 ………
9 command n
10 done

列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
列表也可以是一个文件:
in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

1 #!/bin/sh
2
3 for line in 1 2 3 4 5 6 7
4 do
5 echo “line is $line”
6 done

结果:

[root@localhost 1st]# sh test.sh
line is 1
line is 2
line is 3
line is 4
line is 5
line is 6
line is 7

下面用for循环读一个文件:

查看文件内容

1 [root@localhost 1st]# cat test.txt
2 hello
3 wolrd
4 hello
5 shell
6 [root@localhost 1st]#

code:

1 #!/bin/sh
2
3 FILE=./test.txt
4
5 for line in (< FILE)
6 do
7 echo “line is: $line”
8 done

Results:

1 [root@localhost 1st]# sh xx.sh
2 line is: hello
3 line is: wolrd
4 line is: hello
5 line is: shell
6 [root@localhost 1st]#

while循环的一般格式如下:

1 while command
2 do
3
4 statement
5
6 done

code:

1 #!/bin/sh
2
3 i=0
4 sum=0
5 while [ ile100]6do7sum= (( sum+ i))
8 i= (( i + 1))
9 done
10 echo “sum is $sum”

rusults:

1 [root@localhost 1st]# sh xx.sh
2 sum is 5050
3 [root@localhost 1st]#

if语句的一般格式如下:

1 if ….; then
2 ….
3 elif ….; then
4 ….
5 else
6 ….
7 fi

if 条件语句:

文件表达式:

1 [ -f “somefile” ] :  判断是否是一个文件
2 [ -x “/bin/ls” ] :   判断/bin/ls是否存在并有可执行权限
3 [ -n “ var"]    var变量是否有值
4 [ “ a"=" b” ] :  判断 a b是否相等
5 -r file       用户可读为真
6  -w file       用户可写为真
7  -x file       用户可执行为真
8  -f file       文件为正规文件为真
9  -d file       文件为目录为真
10  -c file       文件为字符特殊文件为真
11  -b file       文件为块特殊文件为真
12  -s file       文件大小非0时为真
13  -t file       当文件描述符(默认为1)指定的设备为终端时为真

字符串表达式:

[string string_operator string]

这里string_operator可为如下几种:

1 = 两个字符串相等。
2 != 两个字符串不等。
3 -z 空串。
4 -n 非空串

eg:

1 #!/bin/sh
2
3 NUMS=”hello”
4 [ NUMS=hello]56echo ?

results:

1 [root@localhost 1st]# sh xx.sh
2 0

整数表达式:

1 -eq 数值相等。
2 -ne 数值不相等。
3 -gt 第一个数大于第二个数。
4 -lt 第一个数小于第二个数。
5 -le 第一个数小于等于第二个数。
6 -ge 第一个数大于等于第二个数。

Eg:

1 #!/bin/sh
2
3 NUMS=130
4 if [ $NUMS -eq “130” ]; then
5
6 echo “equal”
7 else
8 echo “not equal”
9 fi

results:

1 [root@localhost 1st]# sh xx.sh
2 equal

下满贴出一个用shell写的简单图书管理系统:
复制代码

1 #!/bin/bash
2 #Name:Books Management System(BMS)
3 #Author:DREAM
4 file=books.txt
5 function information
6 {
7 echo “Books Management System(BMS)”
8 echo “—————————”
9 echo -e ” 1: ADD Books”
10 echo -e ” 2: Show Books”
11 echo -e ” 3: Select Books”
12 echo -e ” 4: Delete Books”
13 echo -e ” 5: Exit System”
14 #echo
15 echo “—————————”
16 read -p “please input your choice:” num
17 echo
18
19 case “num” in  
 20         1) Add  
 21         ;;  
 22         2) Show  
 23         ;;  
 24         3) Select  
 25         ;;  
 26         4) Delete  
 27         ;;  
 28         5) exit  
 29         ;;  
 30         *) information  
 31         ;;  
 32     esac  
 33 }  
 34   
 35 function Add  
 36 {  
 37     echo -e “please books information eg:(English    101    Jerry)”  
 38     echo  
 39     read -p “please input books name: ” books_name  
 40     read -p “please input books number: ” books_num  
 41     read -p “please input books author: ” books_author   
 42   
 43     echo -e “
books_name\t booksnum\t books_author" >>file && {  
 44         echo “Add Books success”  
 45         echo “—————————”  
 46     }  
 47     if [
? -ne 0 ]
48 then
49 echo “Add Books failure”
50 fi
51
52 information
53 }
54
55 function Show
56 {
57 echo -e “Bname\tBnum\tBauthor”
58 grep -Ev “^ " file
59 echo “———————–”
60 echo
61 information
62 }
63
64 function Search_menu
65 {
66 echo “———————–”
67 echo -e ” 1: Search By Bname”
68 echo -e ” 2: Search By Bnum”
69 echo -e ” 3: Search By Bauthor”
70 echo -e ” 4: Eixt Search System”
71 echo
72 echo “———————–”
73 }
74
75 function Select
76 {
77 Search_menu
78 read -p “please input your choice:” ch
79 case “ch”  in  
 80     1)  
 81         read -p “please input books name: ” name  
 82         echo -e “Bname\tBnum\tBauthor\n————————-”   
 83         awk ‘{if($1~/^'$name’/) print $0}'
file
84 echo “————————-”
85 if [ ?ne0]86then87echothefilenoexist88fi89;;902)91readppleaseinputbooksnumber:num92echoeBname\tBnum\tBauthor\n93awkvs= num ' 2==sprint$0 file 2>/dev/null
94 echo “————————-”
95 if [ ?ne0]96then97echothefilenoexist98fi99echo ?
100 ;;
101 3)
102 read -p “please input books author: ” author
103 echo -e “Bname\tBnum\tBauthor\n————————-”
104 awk ‘{if( 3 / author’/) print 0}'file
105 echo “————————-”
106 if [ ?ne0]107then108echothefilenoexist109fi110echo ?
111
112 ;;
113 4) exit
114 ;;
115 *) Select
116 ;;
117 esac
118 echo
119 information
120 }
121
122 function Delete
123 {
124 read -p “please input your want delete boos number:” num
125 sed -i “/ num/d" file
126
127 if [ $? -ne 0 ]
128 then
129 echo “success failure”
130 fi
131
132 information
133 }
134
135 information

利用popen快速获取一个shell命令的返回值

优点:避免了生成临时文件

函数原型:

1 #include

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值