table of Contents
8. Operator
8.1 Basic Introduction
Learn how to make a variety of arithmetic operations in the shell.
8.2 The basic syntax
1) “$((运算式))”或“$[运算式]”
2) expr m + n
注意 expr 运算符间要有空格
3) expr m - n
4) expr \*, /, % 乘,除,取余
- Application Example
Case 1: Calcd. (2 + 3) * 4
1) $ ((expression))
figure 1
2) $ [expression]
FIG 2
3) expr
3
Case 2: two request command line parameters [integer] and
FIG. 4
9. Analyzing conditions
9.1 The basic syntax
Case 1: "ok" is equal to "ok"
the judge sentences:
[ condition ](注意 condition 前后要有空格)
#非空返回 true,可使用$?验证(0 为 true,>1 为 false)
9.2 Application examples
[ atguigu ]
[]
返回 true
返回 false
[condition] && echo OK || echo notok
条件满足,执行后面的语句
Analyzing conditions used 9.3
1)两个整数的比较
= 字符串比较
-lt 小于
-le 小于等于
-eq 等于
-gt 大于
-ge 大于等于
-ne 不等于
2) 按照文件权限进行判断
-r 有读的权限 [ -r 文件 ]
-w 有写的权限
-x 有执行的权限
3)按照文件类型进行判断
-f 文件存在并且是一个常规的文件
-e 文件存在
-d 文件存在并是一个目录
9.4 Application examples
Case 1: "ok" is equal to "ok"
the judge sentences:
5
2:23 case is greater than equal to 22
Analyzing statement:
6
Case 3: /root/install.log directory file exists
judge sentences:
7
10. The flow control
10.1 if judgment
- The basic syntax
if [ 条件判断式 ];then
程序
fi
或者
if [ 条件判断式 ]
then
程序
elif [条件判断式]
then
程序
fi
Note: (1) There must be a space between [conditional formula], brackets, and conditional formula (2) A second embodiment is recommended
- Application Example
Case: Please prepare a shell program, if the input parameter is greater than or equal to 60, the output "a pass", is less than 60,
the output "fail"
in FIG. 8
10.2 case statement
- The basic syntax
case $变量名 in
"值 1")
如果变量的值等于值 1,则执行程序 1
;;
"值 2")
如果变量的值等于值 2,则执行程序 2
;;
...省略其他分支...
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
- Application Example
Case 1: When the command line parameter is 1, the output "Monday" is 2, it outputs "Tuesday", otherwise outputs
"other"
FIG. 9
10.3 for loop
- The basic syntax 1
for 变量 in 值 1 值 2 值 3...
do
程序
done
Application examples
Case 1: Print command-line parameters will be used to input [@] $ * $
10- The basic syntax 2
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
- Application Example
Case 1: 1 is added to value output from the display 100
in FIG. 11
10.4 while loop
- The basic syntax
while [ 条件判断式 ]
do
程序
done
- Application examples
Case 1: command line, enter a number n, the statistical value from 1 + .. + n is the number?
12
11. read read console input
11.1 The basic syntax
read(选项)(参数)
选项:
-p:指定读取值时的提示符;
-t:指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了。。
参数
变量:指定读取值的变量名
11.2 Application examples
Case 1: a reading console input value num
Case 2: a reading num console input value input within 10 seconds.
13
12. Functions
12.1 Functions Introduction
shell programming and other programming languages, with a system function, may be a custom function. System function, here we introduce two.
12.2 System Functions
- basename basic grammatical
functions: Returns the last / part of the full path, often used to obtain the file name
basename [pathname] [suffix]
basename [string] [suffix]
(功能描述:basename 命令会删掉所有的前缀包括最后一个(‘/’)
字符,然后将字符串显示出来。
选项:
suffix 为后缀,如果 suffix 被指定了,basename 会将 pathname 或 string 中的 suffix 去掉。
- dirname basic grammar
功能:返回完整路径最后 / 的前面的部分,常用于返回路径部分
dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录的
部分),然后返回剩下的路径(目录的部分))
12.3 Application examples
Case 1: Please return /home/aaa/test.txt of "test.txt" section
14
12.4-defined functions
- The basic syntax
[ function ] funname[()]
{
Action;
[return int;]
}
调用直接写函数名:funname [值]
• Application Example
Case 1: calculation of the input parameters and two (read), getSum
15