Ninth cycle
Basic Syntax:
While command
Do
List
Done
Single Row format:
While command; do list; done
For example:
X = 0
While [$ X-LT 10]
Do
Echo $ x
X = $ x + 1
Done
While loops are most commonly used to check whether user input is obtained correctly.
1) set a variable value to null.
2) When the variable value is not null, an existing while loop is started.
3) In the while loop, ask the user questions and read the user response.
4) verify the correctness of the response.
5) if the response is invalid, the variable value is set to null again and the loop continues.
6) Exit the loop until the response is valid and the variable value remains unchanged.
For example:
Response =
While [-z "$ response"];
Do
Echo "enter the name of a files"
Read response
If [! -D "$ response"]; then
Echo "error: Please enter a pathname"
Response =
Fi
Done
11.2 For and select Loops
11.2.1 For Loop
Shell's for loop is similar to PhP's foreach loop. The number of elements determines the number of loops.
Format:
For name in word1 word2 word3... wordn
Do
List
Done
When word1 to wordn are character sequences separated by spaces. Each time a for loop is executed, the value of the variable name is set to the bottom of the word list.
A word. The first name is set to word1, and the second name is set to word2.
11.2.2 select Loop
Similar to the select case statement in PHP
Format:
Select name in word1 word2 word3... wordn
Do
List
Done
Here, name is the variable name, And word1 to wordn are character or word sequences separated by spaces.
The command set executed after the user selects is specified by list.
The execution process of the select loop is as follows:
1) each item in list1 is displayed with a number.
2) display a prompt, usually #?.
3) when a user inputs a value, $ reply is assigned this value.
4) if $ reply contains many displayed items, the variable set by name is assigned to the selected item in list1; otherwise, the item in list1 is displayed again.
5) after a valid selection, execute list2
6) If list2 does not use the loop control mechanism to roll out from the select loop, the entire process starts from the first step.
Select component in comp1 comp2 comp3 all none
Do
Case $ component in
Comp1 | comp2 | comp3) compconf $ component ;;
All) compconf comp1
Compconf comp2
;;
None) break ;;
*) Echo "error ";;
Esac
Done
11.3 Loop Control
1. Break
2. Continue
11.3.1 infinite loop and break commands
Interactive script:
While:
Do
Read cmd
Case $ cmd in
[QQ] | [QQ] [UU] [II] [TT]) break ;;
*) Precess $ cmd ;;
Esac
Done
11.3.2 the continue command enters the next loop
Tenth hour Parameters
In UNIX, the common format for activating a command is:
Command Options files
Here, command is the command name, options is the specified option, and files is the list of files selected by the command.
Example: $ LS-L *. Doc
In shell scripts, this format should be adhered to and the option form should be used as a parameter.
The following two methods are commonly used to process options passed to shell scripts:
1) Use the case statement for manual processing.
2) use the getopts command to process the options.
We recommend that you use case statements for scripts with only one or two options.
If there are many parameters and can be combined at any time, we recommend that you use getopts, because this command has great flexibility in syntax analysis options.
12.1 special Variables
Variable description
$0 indicates the name of the command being executed. For shell scripts, this is the path of the activated command
$ N this variable corresponds to the parameter included when the script is activated. The first parameter is $1, and the second parameter is $2.
$ # Parameter number provided to the script
$ * All parameters are enclosed in double quotation marks. If a script receives two parameters. $ * Equal to $1 $2
$ @ All parameters are enclosed in double quotation marks. If a script receives two parameters. $ * Equal to $1 $2
$? Exit status after execution of the previous command
$ Process ID of the current shell. For shell scripts, This is the ID of the process being executed.
$! Process number of the previous background command
12.2 options and Parameters
You can give options on the command line to change the script or program behavior.
Command parameters refer to all strings or words that appear after the command name in the command line.
The option is a command line parameter.
For example:
$ LS-AF fruit
The command is LS, and the parameter is-AF and fruit. The option of the command is-AF.