第四章 控制流结构
1.if语句
1if 条件1
2then
3 命令1
4elif 条件2
5then
6 命令2
7else
8 命令3
9fi
10------------------
11if 条件
12then 命令
13fi
14eg:
15#!/bin/bash
16#if test
17#this is a comment line
18if [ "10" -lt "12" ];then
19#yes 10 is less than 12
20echo "yes,10 is less than 12"
21else
22echo "no"
23fi
注意:if语句必须以fi终止。”10″ 前一个空格,“12”后也有一个空格。
这个条件都是通过test命令来指定。条件表达为test expression或者[expression]
条件表达式中的比较函数
1man test
2NAME
3 test - check file types and compare values
4SYNOPSIS
5 test EXPRESSION
6 [ EXPRESSION ]
7 [ OPTION
8DESCRIPTION
9 Exit with the status determined by EXPRESSION.
10 --help display this help and exit
11 --version
12 output version information and exit
13 EXPRESSION is true or false and sets exit status. It is one of:
14 ( EXPRESSION )
15 EXPRESSION is true
16 ! EXPRESSION
17 EXPRESSION is false
18 EXPRESSION1 -a EXPRESSION2
19 both EXPRESSION1 and EXPRESSION2 are true
20 EXPRESSION1 -o EXPRESSION2
21 either EXPRESSION1 or EXPRESSION2 is true
22 [-n] STRING
23 the length of STRING is nonzero
24 -z STRING
25 the length of STRING is zero
26 STRING1 = STRING2
27 the strings are equal
28 STRING1 != STRING2
29 the strings are not equal
30 INTEGER1 -eq INTEGER2
31 INTEGER1 is equal to INTEGER2
32 INTEGER1 -ge INTEGER2
33 INTEGER1 is greater than or equal to INTEGER2
34 INTEGER1 -gt INTEGER2
35 INTEGER1 is greater than INTEGER2
36 INTEGER1 -le INTEGER2
37 INTEGER1 is less than or equal to INTEGER2
38 INTEGER1 -lt INTEGER2
39 INTEGER1 is less than INTEGER2
40 INTEGER1 -ne INTEGER2
41 INTEGER1 is not equal to INTEGER2
42 FILE1 -ef FILE2
43 FILE1 and FILE2 have the same device and inode numbers
44 FILE1 -nt FILE2
45 FILE1 is newer (modification date) than FILE2
46 FILE1 -ot FILE2
47 FILE1 is older than FILE2
48 -b FILE
49 FILE exists and is block special
50 -c FILE
51 FILE exists and is character special
52 -d FILE
53 FILE exists and is a directory
54 -e FILE
55 FILE exists
56 -f FILE
57 FILE exists and is a regular file
58 -g FILE
59 FILE exists and is set-group-ID
60 -h FILE
61 FILE exists and is a symbolic link (same as -L)
62 -G FILE
63 FILE exists and is owned by the effective group ID
64 -k FILE
65 FILE exists and has its sticky bit set
66 -L FILE
67 FILE exists and is a symbolic link (same as -h)
68 -O FILE
69 FILE exists and is owned by the effective user ID
70 -p FILE
71 FILE exists and is a named pipe
72 -r FILE
73 FILE exists and is readable
74 -s FILE
75 FILE exists and has a size greater than zero
76 -S FILE
77 FILE exists and is a socket
78 -t [FD]
79 file descriptor FD (stdout by default) is opened on a terminal
80 -u FILE
81 FILE exists and its set-user-ID bit is set
82 -w FILE
83 FILE exists and is writable
84 -x FILE
85 FILE exists and is executable
86eg.
87#!/bin/bash
88#if test
89#this is a comment line
90echo "Enter your filename:"
91read myfile
92if [ -e $myfile ]
93then
94 if [ -s $myfile ];then
95 echo "$myfile exist and size greater than zero"
96 else
97 echo "$myfile exist but size is zero"
98 fi
99else
100echo "file no exist"
101fi
102[test@szbirdora 1]$ sh iftest.sh
103Enter your filename:
10411
10511 exist but size is zero
2.case语句
case语句为多选择语句。
1case 值 in
2模式1)
3 命令1
4 ;;
5模式2)
6 命令2
7 ;;
8esac
9eg.
10#!/bin/bash
11#case select
12echo -n "enter a number from 1 to 3:"
13read ans
14case $ans in
151)
16echo "you select 1"
17;;
182)
19echo "you select 2"
20;;
213)
22echo "you select 3"
23;;
24*)
25echo "`basename $0`:this is not between 1 and 3">&2
26exit;
27;;
28esac
3.for 循环
for循环一般格式:
1for 变量名 in 列表 (列表以空格作为分割)
2do
3 命令1
4 命令2
5done
6eg:
7#!/bin/bash
8#forlist1
9for loop in 1 2 3 4 5
10do
11echo $loop
12done
4.until循环
1until 条件
2do
3 命令1
4 命令2
5 ...
6done
条件测试发生在循环末尾,所以循环至少可以执行一次。
5.while循环
1while 命令 (可以是一个命令也可以是多个,做条件测试)
2do
3 命令1
4 命令2
5 ...
6done
注意:如果从文件中读入变量<filename要放到done后
6.break和continue控制
break跳出,continue跳过
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/structure/224.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.