翻译进度
5
分块数量
1
参与人数

Linux Shell 脚本:条件语句

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


在本章中,我们将会了解 Unix 中 Shell 脚本的一些决策。在编写 Shell 脚本的时候,有时候需要从给定的两种情形中,选择一种情形执行。这时候,就可以使用条件语句,这些条件语句允许程序做出正确的决定并执行。

Unix 系统的 Shell是支持条件语句的,这些条件语句用于根据不同的条件执行不同的操作。接下来,我们将在此处了解两个条件语句的句式。

  • if...else 句式

  • case...esac 句式

if...fi 句式

if...fi 句式是条件语句比较基本的句式。它使 Shell 可以有条件地进行决策和执行语句。

句法

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

Shell expression 会按照上面的语法执行。 如果 expression 返回的值为 true,则执行给定的statement(s)。如果返回的是 false,则不会执行任何语句。大多数情况下,比较运算符用于制定决策。

建议注意括号和 expression 之间的空格。没有空格会产生语法错误。

如果 expression 是一个 shell 命令,那么如果它在执行后返回 0,它将被假定为 true。如果它是布尔表达式,则返回 true 时将为 true。

miss201 翻译于 2周前

例子

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
fi

if [ $a != $b ]
then
   echo "a is not equal to b"
fi

上面的脚本将会产生如下的结果 −

a is not equal to b

if...else...fi

 if...else...fi 语句是条件控制语句的另一种形式。它使 Shell 可以有条件地进行决策和执行语句。

语法

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

Shell expression 会按照上面的语法执行。 如果 expression 返回的值为 true,则执行给定的 statement(s)。如果返回的是 false,则不会执行任何语句。

Example

上面的示例也可以使用 if...else 语句编写,如下所示:

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

执行上面的脚本后,你将得到如下的结果:

a is not equal to b
miss201 翻译于 1周前

if...elif...fi

if...elif...fi 语句是条件控制语句的一种比较高级的形式。它可以是 Shell 从几个条件中做出正确的选择。

语法

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

上面的代码就是一系列的 if 语句,其中每一个 if 都是上一个条件语句的 else 部分。这里执行哪一部分代码是根据 if 返回的条件是不是 true 。如果条件都不为 true,则执行 else 下面的代码块。

例子

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

执行上面的脚本,将会得到如下的结果-

a is less than b
miss201 翻译于 1周前

The case...esac Statement

You can use multiple if...elif statements to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.

Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated if...elif statements.

Syntax

The basic syntax of the case...esac statement is to give an expression to evaluate and to execute several different statements based on the value of the expression.

The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.

case word in
   pattern1)
      Statement(s) to be executed if pattern1 matches
      ;;
   pattern2)
      Statement(s) to be executed if pattern2 matches
      ;;
   pattern3)
      Statement(s) to be executed if pattern3 matches
      ;;
   *)
     Default condition to be executed
     ;;
esac

Here the string word is compared against every pattern until a match is found. The statement(s) following the matching pattern executes. If no matches are found, the case statement exits without performing any action.

There is no maximum number of patterns, but the minimum is one.

When statement(s) part executes, the command ;; indicates that the program flow should jump to the end of the entire case statement. This is similar to break in the C programming language.

Example

#!/bin/sh

FRUIT="kiwi"

case "$FRUIT" in
   "apple") echo "Apple pie is quite tasty." 
   ;;
   "banana") echo "I like banana nut bread." 
   ;;
   "kiwi") echo "New Zealand is famous for kiwi." 
   ;;
esac

Upon execution, you will receive the following result −

New Zealand is famous for kiwi.

A good use for a case statement is the evaluation of command line arguments as follows −

#!/bin/sh

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 

Here is a sample run of the above program −

$./test.sh
test.sh: usage: [ -f filename ] | [ -d directory ]
$ ./test.sh -f index.htm
$ vi test.sh
$ ./test.sh -f index.htm
File name is index.htm
$ ./test.sh -d unix
Dir name is unix
$

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:

译文地址:https://learnku.com/server/wikis/36665

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!