在其余编程语言中,if 语句之后的对象是一个等式,这个等式的求值结果为 TRUE 或者 FALSE。但 bash shell 的 if 句并不这么做。
bash shell 的 if 语句会运行 if 后面的那个命令。假如该命令的退出状态码是 0,位于 then 部分的命令就会被执行。假如该命令的退出码是其余值,then 部分的命令就不会被执行,bash shell 会继续执行脚本中的下一个命令。fi 语句用来表示 if-then 语句到此结束。
#!/bin/bash# Program:# testing the if statement# History:# 2021-12-17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathif pwdthen echo It workedfiexit 0
在 if-then 语句中,不论命令能否成功执行,你都只有一种选择。假如命令返回一个非零退出状态码,bash shell 会继续执行脚本中的下一条命令。在这种情况下,假如能够执行另一组命令就好了。这正是 if-then-else 语句的作用。
#!/bin/bash# Program:# testting the else section# History:# 2021-12-17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathtestuser=NoSuchUserif grep ${testuser} /etc/passwdthen echo "The bash files for user ${testuser} are:" ls -a /home/${testuser}/.b* echoelse echo "The user ${testuser} does not exist on this system." echofiexit 0
在 elif 语句中,紧跟其后的 else 语句属于 elif 代码块。他们并不是之前的 if-then 代码块。
#!/bin/bash# Program:# testing netsed ifs# History:# 2021-12-17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathtestuser=NoSuchUserif grep ${testuser} /etc/passwdthen echo "The user ${testuser} exists on this system."elif ls -d /home/${testuser}then echo "The user ${testuser} does not exists on this system." echo "However, ${testuser} has a directory."fiexit 0
test 命令提供了在 if-then 语句中测试不同条件的途径。假如 test 命令中列出的条件成立,test 命令就会退出并返回退出状态码 0。这样 if-then 语句就与其余变成语言中的 if-then 语句以相似的方式工作了。假如条件不成立,test 命令就会退出并返回非零的退出状态码。这使得 if-then 语句不会再被执行。
但,这里重点讲的是 bash shell 提供的另一种条件测试方法,无需在 if-then 语句中公告 test 命令。
if [ condition ]then commandsfi
方括号定义了测试条件。注意第一个方括号和第二个方括号之前必需加上一个空格,否则会报错
(知道英文的全称之后,其实很好记简称了。)
#!/bin/bash# Program:# Using numeric test evaluations# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathvalue1=10value2=11if [ ${value1} -gt 5 ]then echo "The test value ${value1} is greater than 5"fiif [ ${value1} -eq ${value2} ]then echo "Value1 equal to value2"else echo "The values are different"fiexit 0
#!/bin/bash# Program:# testing string length# History:# 2021-12-17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathval1=testingval2=''if [ -n ${val1} ]then echo "The string '${val1}' is not empty"else echo "The string '${val1}' is empty"fiif [ -z ${val2} ]then echo "The string '${val2}' is empty"else echo "The string '${val2}' is not empty"fiif [ -z ${val3} ]then echo "The string '${val3}' is empty"else echo "The string '${val3}' is not empty"fiexit 0
这里只捡几个常用的说
#!/bin/bash# Program:# Check if either a directory or file exists# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathitem_name=${HOME}/sentinelechoecho "The item being checked: ${item_name}"echoif [ -e ${item_name} ]then echo "The item, ${item_name}, does exist" echo "But is it a file?" echo if [ -f ${item_name} ] then echo "Yes,${item_name} is a file" else echo "The item, ${item_name} is not a file." fielse echo "The item, ${item_name}, does not exist." echo "Nothing to update"fiexit 0
#!/bin/bash# Program:# check file ownership# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathif [ -O /etc/passwd ]then echo "You are the owner of the /etc/passwd file"else echo "Sorry, You are not the owner of the /etc/passwd file"fiexit 0
PS:尤其【-O file】命令特别重要,很多时候在 Linux 服务器上都是不建议用 root 客户执行脚本的,这时假如在脚本开始的时候使用这个命令,即可以避免 root 客户执行脚本的问题。
bash shell 提供了两项可在 if-then 语句中使用的高级特性:
双括号命令允许你在比较过程中使用高级数学表达式,命令格式如下,
(( expression ))
expression 可以是任意的数学赋值或者比较表达式。
#!/bin/bash# Program:# using double parenthesis# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1val1=10if (( ${val1} ** 2 > 90 )) # ** 表示平方then (( val2 = ${val1} ** 2 )) echo "The square of ${val1} is ${val2}"fiexit 0
双方括号提供了针对字符串比较的高级特性——模式匹配,命令格式如下,
[[ expression ]]
#!/bin/bash# Program:# using pattern matching# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathif [[ ${USER} == r* ]]then echo "hello ${USER}"else echo "Sorry, I do not know you"fiexit 0
case 命令会将指定的变量于不同模式进行比较。假如变量和模式是匹配的,那么 shell 会执行为该模式指定的命令。可以通过竖线操作符在一行中分隔出多个模式。星号会捕获所有与已知模式不匹配的值。命令格式如下(注意到那两个双引号了吧,这是规范,不要少写了),
case variable inpattern1 | pattern2) commands1;;pattern3) commands2;;*) default commands;;esac
#!/bin/bash# Program:# using the case command# History:# 2021/12/17 junfenghe.cloud@qq.com version:0.0.1path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport pathcase ${USER} inrich | 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";;esacexit 0
所有命令都是一个一个单词手敲出来的,在买的服务器上练习,练习了一遍,工作上的脚本也优化了一遍(感觉能用的就都用上了),这边再输出一遍,算是多巩固了两次。
j兼容arduino 入门套件 兼容arduino学习开发板mixly创客scratch
原装正品Arduino主板 Mega2560开发板主板单片机3D打印机控制板
STM32F103zet6开发板stm32f103开发板学习板stm32开发板Z400
兼容arduino控制开发板Atmega328p单片机 改进行家版本UNOR3主板
STM32F103zet6开发板stm32f103开发板学习板stm32开发板cortex-m3
普中科技ESP32开发板ESP32物联网python开发板Lua树莓派PICO套件