Linux shell scripts and linuxshell scripts

Source: Internet
Author: User
Tags case statement

Linux shell scripts and linuxshell scripts

[This article is my own learning notes. You are welcome to repost it, but please note the Source: http://blog.csdn.net/jesson20121020]

Today, I started to learn shell flow control structures that are the same as other advanced languages.
Flow Control statement: 1. if statement

Statement format:

if condition1      then   command1else condition2then   command2else   command3fi
Note: The if statement must be terminated with fi.

If there is no condition2, the if statement can be simplified as follows:

if conditionthen       command1else       command2fi
Or:

if conditionthen       command1fi

Example:

If_test.sh

#!/bin/bash#if_testecho -n "please input two number(a and b):"read a bif [ $a -lt $b ]then   echo "$a is less than $b"elif [ $a -gt $b ]then   echo "$a is greater than $b"else   echo "$a ia equal to $b"fi

Grant executable permissions to execute the script:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx if_test.shjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1010 ia equal to 10jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1210 is less than 12jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):8 28 is greater than 2
Note that the comparison operator is used. For more detailed comparison operators, see man test.


2. case statement

Statement format:

Case value in

Mode 1)

Command1

;;

Mode 2)

Command2

;;

Esac


Note:

The 1case value must be followed by the word in, and each pattern must end with parentheses. The value can be a variable or constant.

2. The pattern match * indicates any character ;? Represents any single character; [...] represents any character in the class or range.

Example:

Case_test.sh

#!/bin/bash#case_testecho -n "Enter a number from 1 to 3:"read ncase $n in1)    echo "You select 1"    ;;2)    echo "You select 2"    ;;3)    echo "You select 3"    ;;*)  echo "You select *"    ;;esac

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx case_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:1You select 1jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:2You select 2jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:3You select 3jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:*You select *jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:20You select *
3. for Loop

Format:

For variable name in list

Do

Command 1

Command 2

Done

Example:

For_test1.sh

#!/bin/bash#for_testfor loop in 1 2 3 4 5do    echo $loopdone
Running result:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test1.sh 12345
For_test2.sh

#!/bin/bash#for_test2for loop in orange red blue graydo   echo $loopdone
Running result:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test2.sh orangeredbluegray
For_test3.sh

#!/bin/bash#for_test3for loop in `cat name.txt`do    echo $loopdone
Running result:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ cat name.txt jessoncherryjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test3.sh jessoncherry

4. until Loop

Format:

Until Condition

Do

Command

Done

Until_test.sh

#!/bin/bash#until_testi=10until [ $i -lt 0 ]do    echo $i    let i=i-1   # ((i=$i-1))   #i=$[i-1]done
The execution result is as follows:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx until_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./until_test.sh 109876543210
  

5. while Loop

Format:

While command

Do

Command

.......

Done

Example:

While_test1.sh

#! /Bin/bash # while_test1while echo-n "Enter your favorite stars:"; read Mingxingdo echo "$ Mingxing is your favorite star" done
Grant permissions and execute:

Jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $ chmod a + rx while_test1.sh jesson @ jesson-K43SV :~ /Develop/worksapce/shell_workspace $./while_test1.sh enter your favorite stars: Zhou Jie is your favorite star. Enter your favorite stars: Sun Nan sun Nan is your favorite star.
While_test2.sh

#!/bin/bash#while_testwhile read NAMEdo   echo $NAMEdone <name.txt
Grant permissions and execute:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx while_test2.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./while_test2.sh jessoncherry

6. break and continue

Break [n]

-Exit the loop

-In an embedded loop, you can specify n as the number of loops to be jumped out.

Continue

-Skip the cyclic step

Note: This is the same as in other advanced languages. The continue command is similar to break. It does not jump out of the loop but passes through the current loop step.

Example:

Break_test.sh

#!/bin/bash#break_test.shwhile :do    echo -n "Enter any number(1,...,5):"    read ANS    case $ANS in    1|2|3|4|5)        echo "You enter anumber between 1 and 5"        ;;    *)        echo "Wrong number,Bye"        break;        ;;    esacdone
Grant the following permissions:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx break_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./break_test.sh Enter any number(1,...,5):1You enter anumber between 1 and 5Enter any number(1,...,5):2You enter anumber between 1 and 5Enter any number(1,...,5):3You enter anumber between 1 and 5Enter any number(1,...,5):4You enter anumber between 1 and 5Enter any number(1,...,5):5You enter anumber between 1 and 5Enter any number(1,...,5):6Wrong number,Bye

Continue_test.sh

#!/bin/bash#continue_break_testwhile :do     echo -n "Enter any number(1,...,5):"     read ANS     case $ANS in     1|2|3|4|5)         echo "You enter a number between 1 and 5"         ;;     *)         echo -n "Wrong number,continue(y/n)?:"         read IS_CONTINUE         case $IS_CONTINUE in         y|yes|Y|Yes)                continue                ;;         *)                break                ;;         esac     esacdone
Similarly, execute

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx continue_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./continue_test.sh Enter any number(1,...,5):2You enter a number between 1 and 5Enter any number(1,...,5):3You enter a number between 1 and 5Enter any number(1,...,5):4You enter a number between 1 and 5Enter any number(1,...,5):5You enter a number between 1 and 5Enter any number(1,...,5):8Wrong number,continue(y/n)?:yEnter any number(1,...,5):1You enter a number between 1 and 5Enter any number(1,...,5):79Wrong number,continue(y/n)?:n

 



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.