Shell Note 8

Source: Internet
Author: User

  1. Shell control Structure: conditional structure, branching structure, cyclic structure

  2. If, conditional structure


  3. If expression then command table [else command table]fi you can use semicolons to combine multiple commands in one line. Therefore, in the aesthetic and space-saving considerations, the above format is abbreviated as: if expression; then command table [else command table]fi
  4. If statements can be nested indefinitely

  5. If expression then command table [elif expression then command table] ... [Else Command table]fi
  6. You can change the direct command table to an IF condition statement

  7. Case structure

  8. Case expression in-mode 11[| mode 12] ...) command table 1;; mode 21[| mode 22] ...) command table 2;;.. *) command table n;; Esac
  9. Example

  10. 1 #! /bin/bash 2 #filename: a.sh 3 Case $4 dir |  Path) echo "Current path is ' pwd '";; 5 date |  Time) echo "Today is ' date '";;  6 *) echo "Invalid argument!!"  7 echo "Enter argument:dir/path/date/time";; 8 ESAC
  11. Case and if statements can be nested

  12. Select structure

    The Select Loop structure allows you to generate menus in a simpler way. The generated menu is numbered, and after the user enters the selected number, the corresponding code is executed according to the user's choice.

  13. Select variable in list do command table done
  14. List, which is a sequence of strings.

  15. Using the Select structure will generate a digitized menu on the screen, prompting the user to make a selection, and the default prompt is #. The user simply needs to enter the corresponding number at the prompt to complete the selection.

  16. Typically, select is used with the case structure, allows the user to select from the menu, and executes the corresponding command based on the selection.

  17. The select structure is a loop that requires you to exit the loop using the break command, or you can use the Exit command to end the script.

  18. Example

  19.   1 #! /bin/bash  2  #filename: a.sh   3 echo  "---------Select one week plan---------"   4 select  N in Mon Tue Wed Thu Fri Sat Sun  5 do   6   case  $N  in  7   mon)  echo  "Monday";   8   tue)  echo  "Tuesday";;   9   wed)  echo  "Wednesday";;  10   thu)  echo  "Thursday";;  11   FRI)  echo  "Friday";;  12   sat)  echo  "Saturday";;  13   sun)  echo  "Sunday";;  14   *)  echo  "wrong"  15       break;;  16   esac 17 done 
  20. When you run the script with SH a.sh, the select:not found is displayed. Need to use bash a.sh to run scripts

  21. # bash a.sh---------Select one week plan---------1) Mon2) Tue3) Wed4) Thu5) Fri6) Sat7) sun#? 1monday#?
  22. While loop

  23. While expression do command table done
  24. While loops can be nested

  25. Example

  26. 1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 While [$i-le] 5 does 6 echo "i= $i" 7 i=$ (($i + 1)) 8 done
  27. Note that there is a space behind [],[] after the while and there is a space in front of it, otherwise an error occurs.

  28. For loop

  29. For variable [in List]do command table done
  30. Example

  31. 1 #! /bin/bash 2 #filename: a.sh 3 for I in 1 2 3 4 5 6 4 does 5 echo "i= $i" 6 i=$ (($i + 1)) 7 done
  32. If the In list is omitted, for each positional parameter in the current execution script as a list, the for executes one of the list at a time. That is, the "for variable" implicitly means "for variable [email protected]".

  33. Example (ARG, can be any name, not fixed)

  34. 1 #! /bin/bash 2 #filename: a.sh 3 i=1 4 for ARG 5 does 6 echo "i= $i, Parameters: $arg" 7 i=$ (($i + 1)) 8 done
  35. Until cycle

  36. Until is the continuation of the loop when the condition is false, and stops execution when the condition is true.

  37. Until command table 1test expression do command table 2done
  38. Example

  39. 1 #!  /bin/bash 2 #filename: a.sh 3 i=1 4 echo "Please enter the number N:" 5 read n 6 until 7 test $i-ge $N 8 do 9 echo "I= $i" i=$ (($i + 1)) done
  40. Break statement

  41. Break statement, which can end the execution of a structure such as while, for, until, or select, which jumps out of the structure. After exiting the loop, it goes to the done statement and resumes execution.

  42. If you have a multi-layered structure nested, you can also add a number after the break keyword to indicate the number of layers of the structure you want to jump out of. For example, break 2

  43. Example

  44. 1 #!  /bin/bash 2 #filename: a.sh 3 i=1 4 echo "Please enter the number N:" 5 read n 6 until 7 test $i-ge $N 8 do 9 if    [$i-le 4];then echo "Hello" one-i=$ (($i + 1))-Else echo "i= $i" echo "World" 16 Fi + Done
  45. Continue statements

  46. The continue statement is the same as the break usage and is used to skip the remaining code in this loop.

Shell Note 8

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.