If syntax:if [expression]
Then
Commands
elif [Expression2]
Then
Commands
Else
Commands
Fi
Case Syntax: Case string1 in
STR1)
commands;;
STR2)
commands;;
*)
Commans;;
Esac
Loop statement for syntax:
for Var in list
Do
Commands done
In this form, for each item in the list, the For statement is executed once, the list can make a variable containing a few words, separated by spaces, and several values that can be entered directly, each time the loop is executed, Var is given the current value in the list until the last one;The second form of: for Var
Do Commands;donewith this form, the FOR statement executes once for each item in the variable Var, at which time the shell assumes the variable varcontains all the location parameters of the shell in the command line;
While Loop statement:While expression
do commands; Done
until Loop statement:It's only when the condition is false that the until statement is executed.
Until Expressiondo commands done
Shift statement:The shift command is used to move the current value stored in the positional parameter one position to the left; $1=-r $2=file1 $3=file3 Execute the SHIFT command: Shift you will get: $ = file1 $ = File2 can also specify the number of bits per offset: Shift 2//means two positions per move; While ["$"]
D o
If ["$" = "-i"] Then
infile = "$ 2"
Shift 2
elif ["$" = "-O"]
Then outfile = "$ 2"
Shift 2
else echo "program $ does not recognize option $"
Fi
Done
TR A-Z < $infile > $outfile//Convert lowercase to uppercase, input text is;
Select Selection statement:Select MenuItem [In List_of_items]do Commands;done when the SELECT statement executes, the shell is for each item in the List_of_items
Create a menu item marked with a number;; A list_of_items can be a variable that contains several entries, like a choose1 choose2, or a selection that is entered directly in a command; Select MenuItem in Choice1 choice2 Choice3 if there is no list_of_items, the SELECT statement uses the command-line positional parameters, just like the for expression;
Repeat statement:
The repeat statement exists only in tcsh, and there are no similar statements in Pdksh and bash. The repeat statement is used to make a single statement execute the specified number of times, and the Erpeat statement is as follows: Repeat count commands; Below is an example of a repeat statement.
it reads a string of numbers after the command line。 And according to the number on the screen branch output period.
#
foreach num ($*)
Repeat $num echo-n "."
echo ""
End
Any repeat statement can be overridden with a while or F O-r statement. The repeat statement is just more convenient.
Child functions:Shell functions can define their own functions, just like C or other languages, the greatest benefit of using a function is to make the program clearer, readable, fname () {shell commands;} When using a function: fname per1 per2 Per3
Shell syntax If, case, for, while, until, select, Repeat, child functions