使用if-then语句
如果命令的退出状态是0(成功执行命令),将执行then后面的所有命令。
如果命令的退出状态是0以外的其他值,那么then后面的命令将不会执行,bash shell会移动到脚本的下一条命令。

#!/bin/bash
# testing the if statement
if date
thenecho "it worked"
fi

(date返回0,执行then语句it worked)

#!/bin/bash
#testing multiple commands in the then section
testuser=jiqing9006
if grep $testuser /etc/passwd
thenecho The bash files for user $testuser are:ls -a /home/$testuser/.b*
fi

(The bash files for user jiqing9006 are:
/home/jiqing9006/.bash_logout  /home/jiqing9006/.bash_profile  /home/jiqing9006/.bashrc
if语句行使用grep命令搜索/etc/passwd文件,查看系统是否正在使用一个特定的用户名。
如果一个用户拥有该登录名,脚本会显示一些文本,然后列出用户HOME目录下的bash文件

if-then-else语句

#!/bin/bash
#testing multiple commands in the then section
testuser=jim
if grep $testuser /etc/passwd
thenecho The bash files for user $testuser are:ls -a /home/$testuser/.b*
elseecho "The user name $testuser does't exist on this system"
fi

(如果不存在,执行下面的语句)

嵌套if语句

#!/bin/bash
#testing multiple commands in the then section
user1=jim
user2=jiqing9006
if grep $user1 /etc/passwd
thenecho The bash files for user $user1 are:ls -a /home/$user1/.b*
elif grep $user2 /etc/passwd
thenecho The bash files for user $user2 are:ls -a /home/$user2/.b*
elseecho "The user name $user1 and $user2 does't exist on this system"
fi

(不是elseif 而是elif ,注意与其它语言的区别)

test命令
if [condition]
then 
  commands
 fi
 test命令能够评估三类条件
 数值
 字符串
 文件
 a.数值比较
 n1 -eq n2(是否等于)
 n1 -ge n2(是否大于等于)
 n1 -gt n2(是否大于)
 n1 -le n2(是否小于等于)
 n1 -lt n2(是否小于)
 n1 -ne n2(不等于)

#!/bin/bash
#using numeric test comparisons
val1=10
val2=11
if [ $val1 -gt 5 ]
thenecho "The test value $val1 is greater than 5"
fiif [ $val1 -eq $val2 ]
thenecho "The values are equal"
elseecho "The values are not equal"
fi

(注意if与[之间有空格,[与$val1之间有空格)
test命令无法处理浮点数,bash shell只能处理整数数字

b.字符串比较
str1 = str2 (str1与str2是否相同)
str1 != str2 (是否不同)
str1 < str2(是否小于)
str1 > str2 (是否大于)
-n str1(长度是否大于0)
-z str1(长度是否为0)

#!/bin/bash
#testing string equality
testuser=root
if [ $USER = $testuser ]
thenecho "Welcome $testuser"
fi

(比较相等)

#!/bin/bash
#testing string equality
testuser=baduser
if [ $USER != $testuser ]
thenecho "This isn't $testuser"
elseecho "Welcome $testuser"
fi

(不相等比较,所有标点符号和大小写都考虑在内)

使用大小于号,需要转义一下

#!/bin/bash
#mis-using string comparisons
val1=baseball
val2=hockey
val3=book
val4=Book
if [ $val1 \> $val2 ]
thenecho "$val1 is greater than $val2"
elseecho "$val1 is less than $val2"
fi
if [ $val1 \> $val3 ]
thenecho "$val1 is greater than $val3"
elseecho "$val1 is less than $val3"
fi
if [ $val1 \> $val2 ]
thenecho "$val3 is greater than $val4"
elseecho "$val3 is less than $val4"
fi

结果:
baseball is less than hockey
baseball is less than book
book is greater than Book
(test命令采用的是ascii码排序的,sort采用的是当前语言设置定义的排列顺序。由结果可知,比较第一个字母b小于h,如果第一个字符相同,比较第二个字符,a小于o。小写字符,大于大写字符a97,A65。)

ls -lt(按时间排序,最新的在最前面)
ls -l|sort -k1
ls -l|sort -k2
ls -l|sort -k3(按照第几列进行排序)
..

#!/bin/bash
#testing string length
str1="helloworld"
str2=""
if [ -n $str1 ]
thenecho "str1 is not null"
elseecho "str1 is null"
fiif [ -z $str2 ]
thenecho "str2 is null"
elseecho "str2 is not null"
fi

(我自己写的代码,发现str中不能有空格,否则无法判断)

c.文件比较
test命令能够测试Linux文件系统上的文件状态和路径。
-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新
file2 -ot file2 检查file1是否比file2旧
如果想将文件写到一个目录下,或试图改变到目录位置之前,最好检查一下-d

#!/bin/bash
#look before you leap
if [ -d $HOME ]
thenecho "Your HOME directory exists"cd $HOMEls -l
elseecho "There is a problem with your HOME directory"
fi

(检查一个目录是否存在,存在就做一些事情,不存在就提示错误。)
在脚本中使用文件或目录之前,-e比较能够检查它们是否存在

#!/bin/bash
# check if a directory exists
if [ -e $HOME ]
thenecho "Your home directory exists"#check if file exists in the directoryif [ -e $HOME/testing ]thenecho "$HOME/tesing exist in the directory"date >>$HOME/testingelseecho "Create a new file"date >$HOME/testingfi
elseecho "Your home directory doesn't exists"
fi

(这里稍微复杂了一点,用了嵌套,并且注释很清晰,提示很到位)
-f检测文件

#!/bin/bash
#testing if a file
if [ -e $HOME ]
thenecho "The obj exist,if it is a file?"if [ -f $HOME ]thenecho "Yes,it is a file!"elseecho "No,it is not a file!"if [ -f $HOME/testing ]thenecho "But $HOME/testing is a file!"elseecho "$HOME/testing is not a file too!"fifi
elseecho "The obj doesn't exist"
fi

(这个好无聊,不过展示了一个流程,逐级向下进行查询)
是否能读
在尝试从文件中读取数据之前,通常首先检查一下是否能读取文件。-r

#!/bin/bash
#testing if can read a file
pwfile=/etc/shadow
#first,check if it is a file
if [ -f $pwfile ]
then#now test if you can read itif [ -r $pwfile ]thentail $pwfileelseecho "sorry,the file can be read."fi
elseecho "sorry,it is not a file."
fi

(注意变量使用是一定要加上$符,不然就会出错)

检查空文件
应该用-s比较来检查文件是否为空,尤其是想删除文件时。注意,-s比较成功时,它表明文件包含数据

#!/bin/bash
#testing a file is empty
file=testfile
touch $fileif [ -s $file ]
then  echo "The $file exists and has data in it"
elseecho "The $file doesn't exist or is empty"
fidate >$file
if [ -s $file ]
thenecho "The $file exists and has data in it"
elseecho "The $file doesn't exist or is empty"
fi

(真表示有数据,假表示无数据)

检查是否能够写入数据-w

#!/bin/bash
#checking if a file if writeable
logfile=$HOME/logtest
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`if [ -w $logfile ]
thenecho "The program ran at:$now">>$logfileecho "The first attempt succeeded"
elseecho "The first attempt failed"
fichmod u+w $logfile
if [ -w $logfile ]
thenecho "The program ran at:$now">>$logfileecho "The second attempt succeeded"
elseecho "The second attempt failed"
fi

(这里的判断有点问题,需要再研究)

..
检查文件日期

#!/bin/bash
#testing file dates
if [ ./test1 -nt ./test10 ]
thenecho "The test1 file is newer than test10"
elseecho "The test10 file is newer than test1"
fiif [ ./test1 -ot ./test10 ]
thenecho "The test1 file is older than test10"
elseecho "The test10 file is older than test1"
fi

(比较两个文件的新旧)

复合条件检查
&&
||

#!/bin/bash
#testing compound comparisons
if [ -d $HOME ] && [ -x $HOME/testing ]
thenecho "The file exists and you can execute it"
elseecho "You can't execute the file"
fi

(并列执行)

if-then的高级特征
(())双圆括号,可以进行复杂的算术操作
val++ 后增量
val-- 后减量
++val 前增量
--val 前减量
!     逻辑否定
~     取反
**    取幂
<<    左移
>>    右移
&
|
&&    逻辑与
||    逻辑或

#!/bin/bash
#using double parenthesis
val1=10
if (($val1**2>90))
then((val2 =$val1**2))echo "The square of $val1 is $val2"
fi

(双圆括号里面的内容更加智能,会识别很多东西,不用总是空格空格的编写代码了)

[[]]双方括号
可以进行字符串比较,更加智能
模式匹配,也就是正则表达式可以更好的使用

#!/bin/bash
# using pattern matching
if [[ $USER==r* ]]
thenecho "Hello $USER"
elseecho "Sorry,I don't know you"
fi

(规范编写)

case使用

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

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3198346.html,如需转载请自行联系原作者

Linuxshell之结构化命令相关推荐

  1. Linux shell 学习笔记(8)— 使用结构化命令(if-then 语句、数值比较、字符串比较、文件比较、case 语句)

    1. 使用 if-then 语句 最基本的结构化命令就是if-then语句.if-then语句有如下格式. if command then ​ commands fi 或者 if command; t ...

  2. 使shell用结构化命令

    shell--使用结构化命令 使用结构化命令 知识内容: # 改变命令流 # 使用if-then逻辑 # 嵌套if-then # 测试条件 # 高级if-then功能 许多程序在脚本命令之间需要某些逻 ...

  3. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

  4. linux SHELL之结构化命令

    SHELL之使用结构化命令 使用if-then语句 Bash代码   if command then commands fi 如果if后面的命令退出状态码=0,那么就执行then 另外一种形式 Bas ...

  5. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  6. Shell脚本编程基础 三 使用结构化命令

    结构化命令允许我们改变程序执行的顺序,在某些条件下执行一些命令而在其他条件下跳过另一些命令. (1)使用if-then语句 结构化命令中,最基本的类型就是if-then语句,其格式如下: if com ...

  7. shell编程(七) : [shell基础] 使用结构化命令

    接上一篇文章Linux shell编程(六): 基本shell脚本 3.2 使用结构化命令 前面介绍的都是顺序执行的命令,有时需要按照逻辑顺序执行命令,这是就需要对命令命令施加一些逻辑流程控制,这样的 ...

  8. shell脚本中的结构化命令(if-then-else、case、for、while、until) 脚本中的循环控制

    1. 结构化命令 上一次我们学习了shell脚本的一些基础知识,包括环境变量.重定向.数学运算.退出脚本的方式等,想了解的可以戳这个: shell脚本基础 之前,在我们的示例shell脚本里,shel ...

  9. Linux shell 脚本结构化命令 if-then

    shell 脚本结构化命令 1. if-then 语句基本使用 if-then 语句的基本格式: if command thencommands elifcommands elsecommands f ...

最新文章

  1. 2021年大数据ELK(二):Elasticsearch简单介绍
  2. 我的BLOG:阅读目录
  3. MySQL基础学习过程
  4. 李宏毅深度学习——第一天
  5. 为什么我们做分布式要用 Redis ?
  6. 8 MyBatis动态SQL
  7. ThinkPHP调用连连支付
  8. 2021年,Azure云遇到. NET5,注定开启高光时刻,微软的心,真大!
  9. docker 4 section
  10. java min 函数的使用方法_【Python】Java程序员学习Python(五)— 函数的定义和使用...
  11. Rational RequisitePro
  12. 计算机网络 Kurose 第二章 应用层 2.5 P2P文件分发 2.6 视频流和内容分发网
  13. 第四届江西省高校网络安全技能大赛初赛WebMisc—Writeup
  14. 2020软件测试学科全套上课视频教程网盘免费分享
  15. java生成wsdl文件_webservice之通过wsdl文件生成客户端
  16. LABVIEW详细介绍:LABVIEW是什么软件?都可以干什么?
  17. Linux ftrace 2.3、kprobe event的使用
  18. 数据库连接串的问题。(如果是集群数据库的话)
  19. windows RDP远程连接卡死问题
  20. 主动学习(Active Learning)概述及最新研究

热门文章

  1. nginx在linux reload报错,linux下安装nginx
  2. springboot脚本启动bat_SpringBoot系列(1)基础入门
  3. java WebMvcConfig 全局设置时间服务器时区
  4. 超简单-用协程简化你的网络请求吧,兼容你的老项目和旧的网络请求方式
  5. python知识:numpy的维度之变
  6. c语言入口及出口参数说明,麻烦帮忙指出一下这个函数的入口参数和出口参数呀!...
  7. mysql带c的命令_mysql命令整理
  8. 【新星计划】MATLAB-微积分
  9. 2020-12-14 Matlab 模糊控制 车辆泊车 案例分享
  10. Python 统计列表中元素出现的次数