Understanding of the shell function calls

Today, do a test question is the question of calling a function, the meaning of the questions as follows:

Execute a shell script, print a fruit menu below:

1.apple

2.pear

3.banana

4.cherry

When the user enters the number corresponding to when the fruit of choice, he chose to tell what the fruit, and fruit to add a color word (random), it requires a case to realize statement.

Answers are as follows:

  Color function:

[root @ M01 04 07:29:40] # CAT 4-3.sh
#! / bin / bash
Red = "\ 033 [31M" # define red
green = "\ 033 [32m" # define green
yellow = "\ 033 [33m "# define yellow
blue =" \ 033 [34m " # define blue
tail =" \ 033 [0m " # ending color defined
color () {# function defined color 
Case" $. 1 "in
Red)
  echo -e" Red {2} $ $ $ {} tail "
  ;;
Green)
  echo -e" $ {Green}} $ 2 $ {tail "
  ;;
Yellow)
  echo -e" $ {Yellow}} $ 2 $ {tail "
  ;;
Blue)
  -e echo "$ {Blue}} $ 2 $ {tail"
  ;;
*)
  echo "the Usage: $ 0 arg1 arg2"
Esac
}
Color * $ # calling function

The main script, output:

[root @ M01 04 07:31:05] # CAT 4-2.sh
#! / bin / bash
. /server/scripts/04/4-3.sh # calls another script
CAT << EOF
1.apple
2 .pear
3.banana
4.cherry
the EOF
Usage () {
echo "the Usage: $ {0. 1 | 2 |. 3 |. 4}"
}
. # /server/scripts/04/4-3.sh
selectfruits () {
Read -p "PLS SELECT Fruits the nums:" NUM
Case "$ NUM" in
. 1)
Color Red # Apple parameter value provided by the 4-3.sh, parent-child function
;;
2)
Color Green PEAR
;;
. 3)
Color Yellow Banana
;;
. 4 )
Color Blue Cherry
;;
*)
Usage
Exit. 1
Esac
}
main () {
#if [ $# -ne 1 ];then
# usage
#fi
selectfruits
}
main $*

got the answer

 

Guess you like

Origin www.cnblogs.com/wang50902/p/10932750.html