Shell常用语句及结构

条件判断语句之if

if 语句通过关系运算符判断表达式的真假来决定执行哪个分支;shell有三种if语句样式,如下:

语句1
if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

语句2
if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

语句3
if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

说明

  • 如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。
  • 最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。
  • expression 和方括号([ ])之间必须有空格,否则会有语法错误;这里也可以用双括号来进行表达

判断表达式可以参考文档:https://www.cnblogs.com/guge-94/p/10678144.html
单中括号与双中括号的区别请参考:https://www.cnblogs.com/guge-94/p/10482276.html

迭代循环语句之for

for循环是一种有限迭代循环,一般格式如下:

for 变量 in 列表
do
    command1
    command2
    ...
    commandN
done

列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。

示例

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

递归循环语句之while

while 循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。其格式为:

while command
do
   Statement(s) to be executed if command is true
done

命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。

示例

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
    echo "Yeah! great film the $FILM"
done

递归循环语句之until

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般 while 循环优于 until 循环,但在某些时候,也只是极少数情况下,until 循环更加有用。格式如下:

until command
do
   Statement(s) to be executed until command is true
done

command 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环。

示例

#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

匹配执行语句之case

case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。case 语句格式如下:

casein
模式1)
    command1
    command2
    command3
    ;;
模式2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac

case 工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。

示例

#!/bin/bash

option="${1}"
case ${option} in
   -f) FILE="${2}"
      echo "File name is $FILE"
      ;;
   -d) DIR="${2}"
      echo "Dir name is $DIR"
      ;;
   *) 
      echo "`basename ${0}`:usage: [-f file] | [-d directory]"
      exit 1 # Command to come out of the program with status 1
      ;;
esac

菜单循环语句之select

  select 循环主要用于创建菜单,按数字顺序排列的菜单项将显示在标准错误上,并显示PS3 提示符,等待用户输入;用户输入菜单列表中的某个数字,执行相应的命令;用户输入被保存在内置变量REPLY 中。select 是个无限循环,因此要记住用break 命令退出循环,或用exit 命令终止脚本。也可以按ctrl+c 退出循环。格式如下:

select variable in list
   do
     循环体命令
  done

示例

select 经常和case 联合使用,与for 循环类似,可以省略in list ,此时使用位置参量;

#!/bin/bash
select choice in 1yuan 2yuan 5yuan Quit ;do 
    case $choice in
        1yuan)
            echo "You can buy a glass of water "
            ;;  
        2yuan)
            echo "You can buy  an ice cream "
            ;;  
        5yuan)
            echo "You can buy  a chicken leg "
            echo "Choice $REPLY" 
            ;;  
        Quit)
            echo "Bye"
            break
            ;;  
        *)  
            echo "Enter error!"
            exit 2
     
    esac
done
原文地址:https://www.cnblogs.com/guge-94/p/11023326.html