美文网首页
Linux 命令 & shell 脚本之05(使用结构化命令)

Linux 命令 & shell 脚本之05(使用结构化命令)

作者: 轻飘飘D | 来源:发表于2020-09-27 00:14 被阅读0次
  1. 使用if-then 语句
語句格式如下:
if command; then
commands
fi

[oracle@DB02 myshell]$ cat test_if.sh 
#!/bin/bash
# testing the if statement
if pwd; then
   echo "It worked"
fi

[oracle@DB02 myshell]$ chmod u+x test_if.sh 

[oracle@DB02 myshell]$ ./test_if.sh 
/home/oracle/myshell
It worked
  1. if-then-else 语句
語句格式如下:
if command; then
   commands
else
   commands
fi

[oracle@DB02 myshell]$ cat test_ifelse.sh 
#!/bin/bash
# testing the else section
#
testuser=NoSuchUser
#
if grep $testuser /etc/passwd; then
   echo "The bash files for user $testuser are:"
   ls -a /home/$testuser/.b*
   echo
else
   echo "The user $testuser does not exist on this system."
   echo
fi

[oracle@DB02 myshell]$ chmod u+x test_ifelse.sh 

[oracle@DB02 myshell]$ ./test_ifelse.sh 
The user NoSuchUser does not exist on this system.
  1. 嵌套if
語句格式如下:
if command; then
   commands
else
   if command; then
      commands
   else
      commands
   fi
fi

#使用else部分的另一种形式:elif
語句格式如下:
if command1; then
    commands
elif command2; then
    more commands
fi

[oracle@DB02 myshell]$ cat test_elif.sh 
#!/bin/bash
# Testing nested ifs - use elif
#
testuser=myshell
#

if grep $testuser /etc/passwd; then
   echo "The user $testuser exists on this system."
elif ls -d /home/$testuser; then
   echo "The user $testuser does not exist on this system."
fi

[oracle@DB02 myshell]$ chmod u+x test_elif.sh 

[oracle@DB02 myshell]$ ./test_elif.sh 
ls: cannot access /home/myshell: No such file or directory
  1. test 命令
命令格式:
if test condition; then
   commands
fi

#使用test命令确定变量中是否有内容
[oracle@DB02 myshell]$ cat test_test.sh 
#!/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

[oracle@DB02 myshell]$ chmod u+x test_test.sh 

[oracle@DB02 myshell]$ ./test_test.sh 
The Full expression returns a True

#bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。
命令格式如下:
if [ condition ]; then
   commands
fi

#数值比较
test命令的数值比较功能 --比 较 描 述

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

#案例
[oracle@DB02 myshell]$ cat test_num.sh
#!/bin/bash
# Using numeric test evaluations

value1=10

if [ $value1 -gt 5 ]; then
  echo "The test value $value1 is greater than 5"
else
  echo "False"
fi

[oracle@DB02 myshell]$ chmod u+x test_num.sh 
[oracle@DB02 myshell]$ ./test_num.sh 
The test value 10 is greater than 5
  1. 字符串比较测试--比 较 描 述
str1 = str2   检查str1是否和str2相同
str1 != str2  检查str1是否和str2不同
str1 < str2   检查str1是否比str2小         ----大于号和小于号必须转义 如 if [ $val1 \< $val2 ]; then
str1 > str2   检查str1是否比str2大         ----大于号和小于号必须转义 如 if [ $val1 \> $val2 ]; then
-n str1       检查str1的长度是否非0
-z str1       检查str1的长度是否为0
  1. test命令的文件比较功能 --比 较 描 述
-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旧
  1. 复合条件测试
#命令格式:
 [ condition1 ] && [ condition2 ] 
 [ condition1 ] || [ condition2 ]
  1. if-then 的高级特性 (  用于数学表达式的双括号 )
#双括号命令允许你在比较过程中使用高级数学表达式。test命令只能在比较中使用简单的算术操作。双括号命令提供了更多的数学符号

#双括号命令的格式如下:
(( expression ))

双括号命令中会用到的其他运算符-- 双括号命令符号--符 号 描 述
val++   后增
val--   后减
++val   先增
--val   先减
!       逻辑求反
~       位求反
**      幂运算
<<       左位移
>>       右位移
&       位布尔和
|       位布尔或
&&       逻辑和
||       逻辑或

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

$ ./test23.sh
The square of 10 is 100

#注意,不需要将双括号中表达式里的大于号转义。这是双括号命令提供的另一个高级特性
  1. if-then 的高级特性 (  用于高级字符串处理功能的双方括号 )
提供的另一个特性——模式匹配(pattern matching)
#双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:
[[ expression ]]

们使用了双等号(==)。双等号将右边的字符串(r*)视为一个模式,
并应用模式匹配规则。双方括号命令$USER环境变量进行匹配,看它是否以字母r开头。如果是
的话,比较通过,shell会执行then部分的命令

$ cat test24.sh
#!/bin/bash 
# using pattern matching 

if [[ $USER == r* ]] ; then 
   echo "Hello $USER" 
else 
    echo "Sorry, I do not know you" 
fi 

$ ./test24.sh
Hello rich
  1. case 命令
#命令的格式如下:
case variable in 
  pattern1 | pattern2) commands1;; 
  pattern3) commands2;; 
  *) default commands;; 
esac

$ cat test26.sh
#!/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 

$ ./test26.sh
Welcome, rich 
Please enjoy your visit


相关文章

  • Linux 命令 & shell 脚本之05(使用结构化命令)

    使用if-then 语句 if-then-else 语句 嵌套if test 命令 字符串比较测试--比 较 描 ...

  • shell脚本基础

    shell介绍 shell命令: 在linux终端能被解析的命令,就是shell命令。 shell脚本: 多个sh...

  • 【Linux 命令行与 shell 脚本编程大全】 12 使用结

    导览 使用结构化命令可以改变 shell 脚本的正常执行流程 if-then 语句是最基本的结构化命令,允许执行一...

  • [Linux]Shell

    shell:命令解释器,驱动linux内核;应用程序调用shell命令 1.Shell脚本的执行方式 脚本格式要求...

  • Fabric01-1

    一:shell脚本基础 1.shell命令 : 在linux终端能够被解析出来的命令2.shell脚本 : 多个s...

  • Linux shell编程模板

    只要会写Linux命令,就可以写shell脚本。 之前基本上写的shell脚本都是Linux命令的组合,最多加入i...

  • 自动化脚本实践(Shell + Expect)

    Linux Shell脚本入门: Linux awk 命令 | 菜鸟教程 Shell 教程 | 菜鸟教程 lin...

  • 17. Interview-Linux

    1 用过哪些Linux命令? 2 写过shell脚本吗?shell脚本基本格式? 3 Linux I/O读写方式 ...

  • Shell命令和Linux命令

    Shell命令和Linux命令 Shell shell是c语言编写的程序,是用户使用linux的桥梁。即是一种命令...

  • 常用的shell脚本记录

    主要记录《Linux Shell 脚本攻略》一书很使用的脚本命令,以备用! 基础入门命令 1、获取字符串长度 2、...

网友评论

      本文标题:Linux 命令 & shell 脚本之05(使用结构化命令)

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