美文网首页
shell脚本之if_else

shell脚本之if_else

作者: L白水飘萍 | 来源:发表于2018-11-15 15:25 被阅读0次

(一)if/else 命令

if command1
then
commands
elif command2
then
more commands
else
more commands 
fi

(二) test 命令

test命令提供了在if-then语句中测试不同条件的途径。如果test命令中列出的条件成立,
test命令就会退出并返回退出状态码0。这样if-then语句就与其他编程语言中的if-then语句
以类似的方式工作了。如果条件不成立, test命令就会退出并返回非零的退出状态码,这使得
if-then语句不会再被执行。
test命令的格式非常简单。

test condition
if test condition
then
commands
fi

Ex1:

#!/bin/bash
# Testing the test command
#
if test
then
echo "No expression returns a True"
else
echo "No expression returns a False"
fi

out1:

No expression returns a False

Ex2:

#!/bin/bash
# Testing the test command
#
my_variable="Full"
#
if test $my_variable
then
echo "The $my_variable expression returns a True"
#
else
echo "The $my_variable expression returns a False"
fi

# Testing the test command
#
my_variable=""
#
if test $my_variable
then
echo "The $my_variable expression returns a True"
#
else
echo "The $my_variable expression returns a False"
fi

Out:

The Full expression returns a True
The expression returns a False
bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。
if [ condition ]
then
commands
fi

(三)、数值比较

使用test命令最常见的情形是对两个数值进行比较。

n1 -eq n2 检查n1是否与n2相等   //equal 
n1 -ge n2 检查n1是否大于或等于n2 //great equal 
n1 -gt n2 检查n1是否大于n2 //great then 
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2

Ex1:

#!/bin/bash
# Using numeric test evaluations
value1=10
value2=11
#
if [ $value1 -gt 5 ]
then
echo "The test value $value1 is greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
echo "The values are equal"
else
echo "The values are different"
fi

Out:

The test value 10 is greater than 5
The values are different

(四)、字符串比较

条件测试还允许比较字符串值。

str1 != str2 检查 str1 是否和 str2 不同
str1 < str2 检查 str1 是否比 str2 小
str1 > str2 检查 str1 是否比 str2 大
-n str1 检查 str1 的长度是否非0
-z str1 检查 str1 的长度是否为0

注意在使用字符串的时候最好使用以下形式

[ -n "$str1" ]
[ -z  "$str2" ]

要测试一个字符串是否比另一个字符串大就是麻烦的开始。当要开始使用测试条件的大于或
小于功能时,就会出现两个经常困扰shell程序员的问题:

  • 大于号和小于号必须转义,否则shell会把它们当作重定向符号,把字符串值当作文件名;
  • 大于和小于顺序和sort命令所采用的不同。
#!/bin/bash
# mis-using string comparisons
#
val1=baseball
val2=hockey
#
if [ $val1 \> $val2 ]244 第 12 章 使用结构化命令
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

Out:

baseball is less than hockey

(五) 文件比较

-d file 检查file是否存在并是一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧

(六) 复合条件的测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用

[ condition1 ] && [ condition2 ]  
[ condition1 ] || [ condition2 ]

(七) if-then的高级特性

bash shell提供了两项可在if-then语句中使用的高级特性:

  • 用于数学表达式的双括号
  • 用于高级字符串处理功能的双方括号

双括号命令允许你在比较过程中使用高级数学表达式。其表达式如下:

(( expression ))

expression可以是任意的数学赋值或比较表达式。

val++ 后增
val-- 后减
++val 先增
--val 先减
! 逻辑求反
~ 位求反
** 幂运算
<< 左位移
>> 右位移
& 位布尔和
| 位布尔或
&& 逻辑和
|| 逻辑或

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:

[[ expression ]]

Ex1:

#!/bin/bash
# using pattern matching
#
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I do not know you"
fi

(八) Case命令

case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac

case命令会将指定的变量与不同模式进行比较。如果变量和模式是匹配的,那么shell会执行
为该模式指定的命令。可以通过竖线操作符在一行中分隔出多个模式模式。星号会捕获所有与已
知模式不匹配的值。
Ex1:

#!/bin/bash
# using the case command
#
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to log off when you're done";;
*)
echo "Sorry, you are not allowed here";;
esac

相关文章

  • shell脚本之if_else

    (一)if/else 命令 (二) test 命令 test命令提供了在if-then语句中测试不同条件的途径。如...

  • Shell脚本之杂项

    title: Shell脚本之杂项tags: shell编程, 杂项 记录一些学到的shell脚本里面的一些杂项(...

  • Linux之shell脚本编程

    Linux之shell脚本编程 主要内容: • Vim 编辑器• Shell 脚本• 任务提交 Vim 编辑器 V...

  • Shell入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

网友评论

      本文标题:shell脚本之if_else

      本文链接:https://www.haomeiwen.com/subject/zauyfqtx.html