Getting started with Shell programming Three: Process Control __ Programming

Source: Internet
Author: User
Tags case statement closing tag

Unlike Java, PHP and other languages, the process control of SH is not NULL, such as:

public class test{public
    static void Main (string[] args) {
        int a = ten;
        if (a > 5) {
            System.out.println ("A is greater than 5");
        }
        else{
            //Do not do anything
        }
    }

You can't write this in bash, if the Else branch has no statements to execute, don't write this else. if

The statement syntax is formatted as follows:

a=10
b=20
if [$a = = $b]
then
    echo "A is same to B."
Fi
if Else

The statement syntax is formatted as follows:

a=10
b=20
if [$a = = $b]
then
    echo "A is same to B."
else
    echo "A is not same to B."
Fi
if else-if else

The statement syntax is formatted as follows:

a=10
b=20
if [$a = = $b]
then
   echo "a equals 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 "does not meet the criteria"
fi
  

The output is: A is less than B.

The If Else statement is often used in conjunction with the Test command, as follows:

NUM1=$[2*3]
num2=$[1+5]
if test $[num1]-eq $[num2]
then
    echo ' two numbers equal! '
else
    Echo ' two numbers are not equal! '
Fi

The output is: two numbers are not equal. For Loop

Sequentially outputs a number in the current list:

For loop in 1 2 3 4 5
does
    echo "The value is: $loop"
done
While statement
Num=1 while
($num <=5) does
    echo $num let
    "num++"
done

The Let command is used in bash to execute one or more expressions. You do not need to add the $ variable to the variable calculation.

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

echo "Press CTRL + D to exit
echo" Please enter a message: "While
read input
does
    echo" Your input is: $input "
done
until cycle

The Until loop executes a series of commands until the condition is true.
The Until loop and the while loop are just the opposite of how they are handled.

Num=5
until (($num >10))
do
    echo $num let
    "num++"
done

The results will be output in turn: 5 6 7 8 9 case

You can use a case statement to match a value with a pattern, and if the match succeeds, execute the matching command.

Echo ' Please enter information '
read Anum case
$aNum
    in 1)  Echo ' you chose 1 '
    ;;
    2)  echo ' You have chosen 2 '
    ;;
    3)  echo ' You have chosen 3 '
    ;;
    Wsz ")  Echo ' You have chosen Wsz '
    ;;
    *  Echo ' You did not enter a number between 1 and 4 '
    ;;
Esac
The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be a variable or a constant. When a match finds a value that conforms to a pattern, all commands begin to execute until; Indicates a break. The value will detect each pattern that matches. Once the pattern matches, the corresponding command after the match pattern is executed and no more mode continues. If there is no matching pattern, use the asterisk * to capture the value, and then execute the following command. ESAC syntax and C family language is very different, it requires a esac (that is, case in turn) as a closing tag, each case branch to use;; Indicates a break. Jump out of the loop Break

In the following example, the script goes into the dead loop until the user enters a number that is not one of the 1 2 3 4 5. To jump out of this loop, you need to use the break command.

While:
does
    read Anum case
    $aNum in
    1|2|3|4|5) echo "Your input is $aNum"
    ;;
    *) echo "Game Over" the break
    ;;
    Esac done
Continue

The continue command is similar to the break command, except that it does not jump out of all loops and simply jumps out of the current loop.

For ((i=0 i<10; i++)) do
    if [$i = 3]
    then
        continue
    else
        echo $i
    fi
Done

The above results will be output: 0 1 2 4 5 6 7 8 9, and will not output 3.

Reference from: http://www.runoob.com/linux/linux-shell-process-control.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.