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

摘要:
与其他语言一样,shell也支持for和while循环。for循环的一般格式如下:1#!列表也可以是文件:in列表是可选的。如果未使用,for循环将使用命令行/Bin/sh23forlinein12345674do5echo“lineis$line”6done结果的位置参数:[root@localhost1st]#shtest。Shline1lineis2lineis3lineis4lineis5lineis6lineis7接下来,使用for循环读取文件:查看文件内容1[root@localhost1st]#斤。txt2hello3word4hello5shell6[root@localhost1st]#代码:1#!/bin/sh23FILE=./test。txt45forlinein$6do7echo“lineis:$line”8doneResults:1[root@localhost1st]#shxx。sh2lineis:hello3lineis:word4lineis:hello5lineis:shell6[root@localhost1st]#while循环的一般格式如下:1whilecommand2do34语句56donecode:1#/bin/sh23NUMS=1304if[$NUMS eq“130”];则56echo“equal”7else8echo“notequal”9firesults:1[root@localhost1st]#一个用shell编写的简单图书管理系统发布在shxx下。sh2equal:1#!

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 [ $i -le 100 ]
 6 do 
 7    sum=$(($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" ]
5  
6  echo $?

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	$books_num	$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	Bnum	Bauthor"
 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	Bnum	Bauthor
-------------------------" 
 83         awk '{if($1~/^'$name'/) print $0}' $file
 84         echo "-------------------------"
 85         if [ $? -ne 0 ]
 86         then
 87             echo "the file no exist"
 88         fi
 89     ;;
 90     2)
 91         read -p "please input books number: " num
 92         echo -e "Bname	Bnum	Bauthor
-------------------------" 
 93         awk -v s=$num '$2==s {print $0}' $file 2>/dev/null
 94         echo "-------------------------"
 95         if [ $? -ne 0 ]
 96         then
 97             echo "the file no exist"
 98         fi
 99         echo $?
100     ;;
101     3)
102         read -p "please input books author: " author
103         echo -e "Bname	Bnum	Bauthor
-------------------------" 
104         awk '{if($3~/'$author'/) print $0}' $file
105         echo "-------------------------"
106         if [ $? -ne 0 ]
107         then
108             echo "the file no exist"
109         fi
110         echo $?
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 <stdio.h>
2 
3        FILE *popen(const char *command, const char *type);
4 
5        int pclose(FILE *stream);

函数说明:

  popen()函数通过创建一个管道,调用fork()产生一个子进程,执行一个shell以运行命令来开启一个进程。这个管道必须由pclose()函数关闭,而不是fclose()函数。pclose()函数关闭标准I/O流,等待命令执行结束,然后返回shell的终止状态。如果shell不能被执行,则pclose()返回的终止状态与shell已执行exit一样。

  type参数只能是读("r")或者写("w")中的一种,得到的返回值(标准I/O流)也具有和type相应的只读或只写类型。如果type是"r"则文件指针连接到command的标准输出;如果type是"w"则文件指针连接到command的标准输入。

  command参数是一个指向以NULL结束的shell命令字符串的指针。这行命令将被传到bin/sh并使用-c标志,shell将执行这个命令。

  popen()的返回值是个标准I/O流,必须由pclose来终止。前面提到这个流是单向的(只能用于读或写)。向这个流写内容相当于写入该命令的标准输入,命令的标准输出和调用popen()的进程相同;与之相反的,从流中读数据相当于读取命令的标准输出,命令的标准输入和调用popen()的进程相同。

返回值:

  如果调用fork()或pipe()失败,或者不能分配内存将返回NULL,否则返回标准I/O流。popen()没有为内存分配失败设置errno值。如果调用fork()或pipe()时出现错误,errno被设为相应的错误类型。如果type参数不合法,errno将返回EINVAL。

下面是使用popen获取主机中虚拟机个数的范例:

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     FILE *fp;
 7     size_t value = 0;
 8     int nums;
 9     char cmd[1024] = {0};
10     int guestnums;
11 
12     strcpy(cmd, "virsh list --all | sed -n '3, $p' | grep -v '^$' | wc -l");
13 
14    /*使用popen获取shell命令返回值*/ 
15     if((fp = popen(cmd, "r")) != NULL) {
16 
17         if(fgets((char *)&nums, 2, fp))
18         {   
19             guestnums = atoi((char *)&nums);
20         }   
21     }   
22     
23     printf("%d
", guestnums); 
24     pclose(fp);
25 
26     return 0;
27     
28 }

  

免责声明:文章转载自《shell 中的for、while循环及if语句》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQLServer 命令批量删除数据库中指定表(游标循环删除)【译】Js基础运行机制下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Shell脚本学习笔记3——控制GPIO

在我参与维护的一个项目中,硬件上有一处设计失误:本应该短接到地的一个GPIO没有短接到地,导致某一个功能不能实现,正式出货的时候会进行改版。 但目前还处于测试阶段,只需要先把这个功能实现出来,方案有两个,一是硬件上飞线去实现,而是通过软件去拉低这个GPIO。鉴于硬件飞线比较麻烦,所以我们选择软件上写一个脚本来实现。 我们可以先通过命令行来控制某一个GPIO...

django shell 操作

插件:django-extensions django-extensions==1.9.8 pip3 installdjango-extensions 1.数据库shell 命令(项目目录下) python3 manage.py dbshell 执行sql语句 2.查看django orm对应的sql语句 https://www.jianshu.com/p...

使用 ssmtp 於 shell 透過 Gmail 寄信

有很多程式於 bash shell 執行, 執行完要自動寄信出去, 但是最近都被 Google 退信, 最好的方法是透過 Gmail 直接寄信. 本來是要另外寫隻 script 來做這種事, 剛剛發現 $4 的介紹文, 真是超方便的, 一樣使用原本 shell 的 mail, script 都不需要改, 只要設定設一設即可~  原文:http://blog...

Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-1

1  更高级的算法牵扯到更多重的循环和复杂的计算,尤其是现在人工智能的算法尤其如此。有些历史知识的人能够了解到,人工智能的很多基本算法其实近百年之前就有了,但是当时的计算机技术达不到去实现这些算法的要求,因此当今计算机的技术和当时已经不是在一个量级上面,因此人工智能等方案才有被重新提上日程,获得飞速的发展。也就是说,当人的思想超越当今的技术的时候我们只能等...

YARN应用程序的开发步骤

开发基于YARN的应用程序需要开发客户端程序和AppMaster程序: 我们基于程序自带的例子来实现提交application 到YARN的ResourceManger。 Distributed Shell application:Client 步骤: 连接  ResourceManager; 通过ApplicationClientProtocol协议 与...

如何分析和提高(C/C++)程序的编译速度?

版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。 本文链接:https://www.cnblogs.com/lihuidashen/p/12937085.html 微信链接:https://mp.weixin.qq.com/s/MFOaa-Dw1iNMXuXPfXjLBA 一个别人的vs 2010 的程序...