Shell test命令/流程控制

Shell test命令


Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。

数值测试

参数,说明

-eq
等于则为真

-ne
不等于则为真

-gt
大于则为真

-ge
大于等于则为真

-lt
小于则为真

-le
小于等于则为真

实例演示:

num1=100
num2=100
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi


输出结果:

The two numbers are equal!


字符串测试

参数,说明

=
等于则为真

!=
不相等则为真

-z 字符串
字符串长度伪则为真

-n 字符串
字符串长度不伪则为真


实例演示:

num1=100
num2=100
if test num1=num2
then
echo 'The two strings are equal!'
else
echo 'The two strings are not equal!'
fi

输出结果:

The two strings are equal!


文件测试


参数,说明
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真

实例演示:

cd /bin
if test -e ./bash
then
echo 'The file already exists!'
else
echo 'The file does not exists!'
fi


输出结果:

The file already exists!

另外,Shell还提供了与( ! )、或( -o )、非( -a )三个逻辑操作符用于将测试条件连接起来,其优先级为:"!"最高,"-a"次之,"-o"最低。例如:

cd /bin
if test -e ./notFile -o ./bash
then
echo 'One file exists at least!'
else
echo 'Both dose not exists!'
fi

输出结果:

One file exists at least!


Shell 流程控制

和Java、PHP等语言不一样,sh的流程控制不可为空,如(以下为PHP流程控制写法):

<?php
if (isset($_GET["q"])) {
search(q);
}
else {
//do nothing
}

在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样

if else

if
if 语句语法格式:

if condition
then
command1
command2
...
commandN
fi
写成一行(适用于终端命令提示符):

if `ps -ef | grep ssh`; then echo hello; fi
末尾的fi就是if倒过来拼写,后面还会遇到类似的。

if else
if else 语法格式:

if condition
then
command1
command2
...
commandN
else
command
fi


if else-if else
if else-if else 语法格式:

if condition1
then
command1
elif condition2
command2
else
commandN
fi


if else语句经常与test命令结合使用,如下所示:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出结果:

The two numbers are equal!

for 循环
与其他编程语言类似,Shell支持for循环。

for循环一般格式为:

for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done


写成一行:

for var in item1 item2 ... itemN; do command1; command2… done;
当变量值在列表里,for循环即执行一次所有命令,使用变量名获取列表中的当前取值。命令可为任何有效的shell命令和语句。in列表可以包含替换、字符串和文件名。

in列表是可选的,如果不用它,for循环使用命令行的位置参数。

例如,顺序输出当前列表中的数字:

for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
输出结果:

The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
顺序输出字符串中的字符:

for str in 'This is a string'
do
echo $str
done
输出结果:

This is a string


while 语句
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:


while condition
do
command
done
命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。


无限循环


until 循环

until循环执行一系列命令直至条件为真时停止。

until循环与while循环在处理方式上刚好相反。

一般while循环优于until循环,但在某些时候—也只是极少数情况下,until循环更加有用。


case
Shell case语句为多选择语句。可以用case语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。


跳出循环
在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,Shell使用两个命令来实现该功能:break和continue。

break命令
break命令允许跳出所有循环(终止执行后面的所有循环)。


continue

continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环。

esac

case的语法和C family语言差别很大,它需要一个esac(就是case反过来)作为结束标记,每个case分支用右圆括号,用两个分号表示break。

原文地址:https://www.cnblogs.com/Alanf/p/10273449.html