Objective
today I just wrote an automated packaging script, again using the bash shell, a good feeling of happiness. The main point here is to introduce the Select command, which can help us complete the menu selection function.
Format
I am also the first to use Select Process Control today, in PHP, Java, C these languages do not implement the Select function. In the Bash shell, the Select format is as follows:
Select $var in ${list[@]}
do
statements so can use $var
done
When select executes, the selection menu is given according to the list array, the result of the user selection is saved in the $var variable, and then the statements statement is executed. Once the execution is complete, give the menu again, waiting for the user to choose. If the user wants to jump out of the selection loop, you need to increase the break statement according to the condition in the loop body.
Example
Given a select example, you can refer to:
#!/bin/bash
fruits= (
"apple"
"pear"
"orange"
"Watermelon"
)
echo "Please guess Which fruit I like: '
select var in ${fruits[@]} ' do
if [$var = ' Apple ']; Then
echo "Congratulations, your are my good firend!"
Break
Else
echo "Try again!"
Fi done