UNIX Shell控制结构—IF
来源:互联网 发布:无损音乐论坛 知乎 编辑:程序博客网 时间:2024/04/28 21:24
流控制(Decision Making)
IF语句有三种格式:
第一种:if ... fi statement
下面是一个实例:
①条件和处理命令分开的一种写法:
if 条件; then
处理命令
fi
②条件和处理命令分开的另一种写法:
if 条件
then
处理命令
fi
这里需要根据个人习惯去选择。
上面的例子中,变量的值是赋死了的,若要给此脚本传递两个参数,可做如下修改:
给脚本传递参数,只需要在sh命令后面添加即可,使用空格隔开:
第二种:if ... else ... fi,具体如下:
if [ expression ]
then
statement(s) to be sxecuted if expression is true
else
statement(s) to be sxecuted if expression is not true
fi
一个简单的实例
echo "arg1:$2"
echo "arg1:$3"
脚本注解:
1.$#表示参数输入的个数
2.basename $0打印文件的名称
3.若输入小于三个参数则,将输出一个信息,这个信息被当做是错误信息(>&2)。
执行脚本:
sh ifparam.sh scott tom
Usage:ifparam.sh arg1 arg2 arg3
sh ifparam.sh scott tom jim
arg1:scott
arg1:tom
arg1:jim
再来看一个测试:
Your EDITOR environment is not set
这里使用三种方式去检测环境变量是否设置;
①直接报错:语法错误
②正确写法,使用test检测,-z表示如果为设置则长度为0返回值为true
③wc -c计算包括了空行,所以不准确
第三种:if ... elif ... fi,具体如下:
sh elif.sh
the first number is equal to the next
当输入数字的时候,如下:
sh elif.sh 10 20
elif.sh: test: unknown operator ==
这种情况,我们可以将其错误信息输入到一个文件当中,如下:
sh elif.sh 10 20 > log.txt 2>&1
cat log.txt
elif.sh: test: unknown operator ==
当将“==”号修改为-eq,结果如下:
sh elif.sh 10 20
the first number is less than the next
下面是一个if的实例,包括这三种命令格式;
脚本的作用是创建一个目录,如果不输入任何值,则打印脚本的作用说明;
输入则提示是否创建,输入非提示,则报错误,否则按提示走。
sh ifmkdir.sh
Usage:ifmkdir.sh directory to create
2.输入一个存在的目录,提示目录已经存在:
sh ifmkdir.sh test
Directory test exists
查看确实有test目录:
[ -d test ]
echo $?
0
创建test1目录:sh ifmkdir.sh test1
The Directory does exist
Create it now?[y..n]:y
creating now
Creating successful
3.执行脚本,但不想创建目录:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:n
Giving up creating directory test2
4.执行脚本时,不按照提示输入:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:d
IF语句有三种格式:
第一种:if ... fi statement
下面是一个实例:
cat if1.sh#!/bin/sha=10b=20#①if [ $a -eq $b ]; then echo "a is equal to b";fiif [ $a -gt $b ]; then echo "a is great than b";fi#②if [ $a -lt $b ]then echo "a is less than b";fi# the EOF注意:
①条件和处理命令分开的一种写法:
if 条件; then
处理命令
fi
②条件和处理命令分开的另一种写法:
if 条件
then
处理命令
fi
这里需要根据个人习惯去选择。
上面的例子中,变量的值是赋死了的,若要给此脚本传递两个参数,可做如下修改:
cat if1.sh#!/bin/sh# a=10# b=20if [ $1 -eq $2 ]; then echo "the first number is equal to the second";fiif [ $1 -gt $2 ]; then echo "the first number is great than the second";fiif [ $1 -lt $2 ]then echo "the first number is less than the second";fi# the EOF
给脚本传递参数,只需要在sh命令后面添加即可,使用空格隔开:
sh if1.sh 1 2the first number is less than the secondsh if1.sh 12 1the first number is great than the secondsh if1.sh 1 1the first number is equal to the second
第二种:if ... else ... fi,具体如下:
if [ expression ]
then
statement(s) to be sxecuted if expression is true
else
statement(s) to be sxecuted if expression is not true
fi
一个简单的实例
cat ifparam.sh#!/bin/shif [ $# -lt 3 ]; then echo "Usage:`basename $0` arg1 arg2 arg3" >&2 exit 1fi#EOFecho "arg1:$1"
echo "arg1:$2"
echo "arg1:$3"
脚本注解:
1.$#表示参数输入的个数
2.basename $0打印文件的名称
3.若输入小于三个参数则,将输出一个信息,这个信息被当做是错误信息(>&2)。
执行脚本:
sh ifparam.sh scott tom
Usage:ifparam.sh arg1 arg2 arg3
sh ifparam.sh scott tom jim
arg1:scott
arg1:tom
arg1:jim
再来看一个测试:
cat ifeditor.sh#!/bin/csh#if [ -z $EDITOR ]①#if [ -z "`echo $EDITOR`" ]③#下面这种写法不能正确的计算出环境的值#因为wc -c计算包括新的空的行#if [ `echo $EDITOR | wc -c` -eq 0 ]②if [ -z "`echo $EDITOR`" ] then echo "Your EDITOR environment is not set"else echo "Using $EDITOR as the default editor"fi#EOFsh ifeditor.sh
Your EDITOR environment is not set
这里使用三种方式去检测环境变量是否设置;
①直接报错:语法错误
②正确写法,使用test检测,-z表示如果为设置则长度为0返回值为true
③wc -c计算包括了空行,所以不准确
第三种:if ... elif ... fi,具体如下:
if [ expression 1 ]; then statement(s) to be sxecuted if expression 1 is trueelif [ expression 2 ]; then statement(s) to be sxecuted if expression 2 is trueelif [ expression 3 ]; then statement(s) to be sxecuted if expression 3 is trueelse statement(s) to be sxecuted if no expression is truefi下面是一个比较两个数字大小的例子:
cat elif.sh#!/bin/shif [ $1 == $2 ]; then echo "the first number is equal to the next"elif [ $1 -gt $2 ]; then echo "the first number is great than the next"elif [ $1 -lt $2 ]; then echo "the first number is great than the next"else echo "None of the condition met"fi#EOF当不输入任何数字的时候,也就是为空,结果如下:
sh elif.sh
the first number is equal to the next
当输入数字的时候,如下:
sh elif.sh 10 20
elif.sh: test: unknown operator ==
这种情况,我们可以将其错误信息输入到一个文件当中,如下:
sh elif.sh 10 20 > log.txt 2>&1
cat log.txt
elif.sh: test: unknown operator ==
当将“==”号修改为-eq,结果如下:
sh elif.sh 10 20
the first number is less than the next
下面是一个if的实例,包括这三种命令格式;
脚本的作用是创建一个目录,如果不输入任何值,则打印脚本的作用说明;
输入则提示是否创建,输入非提示,则报错误,否则按提示走。
#!/bin/shDIR=$1if [ "$DIR" = "" ]; then echo "Usage:`basename $0` directory to create" >&2 exit 1fiif [ -d $DIR ]; then echo "Directory $DIR exists"else echo "The Directory does exist" echo -n "Create it now?[y..n]:" read ANS if [ "$ANS" = "y" ] || [ "$ANS" = "Y" ]; then echo "creating now" mkdir $DIR > log.txt 2>&1 if [ $? -ne 0 ]; then echo "Errors creating the directory $DIR" >&2 exit 1 fi echo "Creating successful" elif [ "$ANS" = "n" ] || [ "$ANS" = "N" ]; then echo "Giving up creating directory $DIR" else echo "Bad input" fifi#EOF1.不输入参数
sh ifmkdir.sh
Usage:ifmkdir.sh directory to create
2.输入一个存在的目录,提示目录已经存在:
sh ifmkdir.sh test
Directory test exists
查看确实有test目录:
[ -d test ]
echo $?
0
创建test1目录:sh ifmkdir.sh test1
The Directory does exist
Create it now?[y..n]:y
creating now
Creating successful
3.执行脚本,但不想创建目录:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:n
Giving up creating directory test2
4.执行脚本时,不按照提示输入:
sh ifmkdir.sh test2
The Directory does exist
Create it now?[y..n]:d
Bad input
更多信息,参考:Decision making
- UNIX Shell控制结构—IF
- Unix Shell控制结构—CASE
- Shell编程之控制结构if
- Unix Shell - If statement
- unix shell if 参数
- shell-流程控制 if
- unix shell if 相关参数
- UNIX Shell循环控制—while
- Linux Shell 之 Shell 基本控制结构(一)(if and case)
- shell 编程控制语句if
- 2 Perl - if 控制结构
- 流程控制:if分支结构
- If-then结构控制介绍
- if单选控制结构
- plsql控制结构if then
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Linux-2.6平台下SCSI子系统初始化及退出
- MFC 获得当前exe文件的路径
- python 装饰器
- c#中从string数组转换到int数组
- Tomcat 7 HTTP 连接器(一)
- UNIX Shell控制结构—IF
- 80x86寻址模式
- 【大话设计模式】笔记——开放封闭原则
- php 获取 multipart/form-data 的raw data
- MTK feature phone 待机电流过大调试
- centos6 配置 vnc
- JavaSE第七十八讲:AWT深入详解
- 用Tagxedo制作文字云
- 文件编程:格式化读fscanf()函数