Latest web development tutorials

Shell Process Control

And Java, PHP and other languages ​​are not the same, sh flow control is not empty, for example (The following is the PHP process control writing):

<?php
if (isset($_GET["q"])) {
    search(q);
}
else {
    // 不做任何事情
}

In sh / bash may not be so in writing, no statement is executed if the branch else, do not write this else.


if else

if

if statement syntax:

if condition
then
    command1 
    command2
    ...
    commandN 
fi

A single line (for terminal command prompt):

if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi

The end of the fi is reversed if the spelling later will encounter similar.

if else

if else syntax:

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if else-if else

if else-if else syntax:

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

The following examples determine whether two variables are equal:

a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

Output:

a 小于 b

if else statements are often used in conjunction with test command as follows:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo '两个数字相等!'
else
    echo '两个数字不相等!'
fi

Output:

两个数字相等!

for loop

Like other programming languages, Shell support for loop.

The general format of the for loop:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

A single line:

for var in item1 item2 ... itemN; do command1; command2… done;

When the value of the variable in the list, for loop that is executed once all commands, variable names get a list of the current value. Command can be any valid shell commands and statements. in list can include the replacement string and file name.

in list is optional, if you do not use it, for recycling location command line parameters.

For example, the order of output current list of numbers:

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

Output:

The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5

Sequentially outputs a string of characters:

for str in 'This is a string'
do
    echo $str
done

Output:

This is a string

while statement

while loop to continuously execute a series of commands, it is also used to read data from the input file; command usually test conditions. The format is:

while condition
do
    command
done

The following is a basic while loop test conditions: if int is less than or equal to 5, the condition returns true. int start from zero each time the loop processing, plus 1 int. Run the above script returns the number 1-5, then terminate.

#!/bin/sh
int=1
while(( $int<=5 ))
do
        echo $int
        let "int++"
done

Run the script, output:

1
2
3
4
5

Use used Bash let command, which is used to perform one or more expressions, variable calculations do not need to add $ to represent variables, specifically Buyers: Bash let command

.

while loop can be used to read the keyboard information. The following example, the input information is set to the variable FILM, press <Ctrl-D> end loop.

echo '按下 <CTRL-D> 退出'
echo -n '输入你最喜欢的电影名: '
while read FILM
do
    echo "是的!$FILM 是一部好电影"
done

Run the script, output similar to the following:

按下 <CTRL-D> 退出
输入你最喜欢的电影名: w3cschool本教程
是的!w3cschool本教程 是一部好电影

Infinite loop

Infinite loop syntax:

while :
do
    command
done

or

while true
do
    command
done

or

for (( ; ; ))


until loop

until loop executes a series of commands until condition is true when stopped.

until loop and while loop on the opposite approach.

Usually while loop until better circulation, but at some point - and only in rare cases, until the cycle more useful.

until syntax:

until condition
do
    command
done

Conditions may be any test conditions, the test occurs at the end of the cycle, and the loop executed at least once - please note this point.


case

Shell case statement is more select statements. You can use the case statement match a value with a pattern, if the match is successful, execute commands matching. case statement format is as follows:

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

case work as shown above. The value must be followed by a word in, each model must be closing parenthesis. The value can be variable or constant. After matching a pattern consistent with values ​​found, during which all commands executed until ;;.

The value of each match will detect a pattern. Once the pattern matching, pattern matching after executing the corresponding command no longer continue to other modes. If there is no matching a pattern, use an asterisk * to capture the value, and then run behind the command.

The following script prompts for 1-4, with each pattern matching:

echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac

Enter a different content, there will be different results, such as:

输入 1 到 4 之间的数字:
你输入的数字为:
3
你选择了 3

Out of the loop

In the cycle, and sometimes it does not meet the needs forced out of the loop when the loop end condition, Shell uses two commands to implement this feature: break and continue.

break command

break command allows out of all loops (loop terminates execution of all back).

The following example, the script into an infinite loop until the user enters a number greater than 5. To get out of this cycle and return to the shell prompt, use the break command.

#!/bin/bash
while :
do
    echo -n "输入 1 到 5 之间的数字:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
            break
        ;;
    esac
done

Implementation of the above code, the output is:

输入 1 到 5 之间的数字:3
你输入的数字为 3!
输入 1 到 5 之间的数字:7
你输入的数字不是 1 到 5 之间的! 游戏结束

continue

continue and break command command similar, with one difference, it does not jump out of all loops, just out of the current cycle.

To modify the above example:

#!/bin/bash
while :
do
    echo -n "输入 1 到 5 之间的数字: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的!"
            continue
            echo "游戏结束"
        ;;
    esac
done

Run the code was found, when the input number greater than 5, this case will not end the cycle, the statement echo "Game is over!" Will never be executed.


esac

Grammar and language differences in case C family is large, it takes a esac (case is in turn) mark the end of each case branch with a right parenthesis, represented by two semicolons break.