function can make a similar custom execution command in shell script, the biggest function is to be able to simplify many of our program code. It is important to note that the shell script is executed from top to bottom/left to right, so the function in Shellscript must be set at the very front of the program so that the available program segments can be found at execution time.
Copy Code code as follows:
#!/bin/bash
# program
# This program was to show the use of ' function '
# History
# 2013/5/4 by Lvcy
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/bin
Export PATH
#输出统一信息
function Printinfo ()
{
Echo-n "Your choice is"
}
#将小写字符转换为大写字符
function Dotr ()
{
Tr ' A-Z '
}
Read-p "Please input your choice (one|two|three|four):" num
#用case做条件判断
Case $num in
"One")
Printinfo; echo $num | Dotr
;;
"Two")
Printinfo; echo $num | Dotr
;;
"Three")
Printinfo; echo $num | Dotr
;;
"Four") Printinfo; echo $num | Dotr
;;
Esac
Exit 0
The following is a generic shell script with function functions:
Copy Code code as follows:
#!/bin/bash
# program
# this are show the params of function
# History
# 2013/5/14 by Lvcy
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
function Printinfo ()
{
echo "Your choice is $"
}
Case is in
"One")
Printinfo 1
;;
"Two")
Printinfo 2
;;
"Three")
Printinfo 3
;;
"Four")
Printinfo 4
;;
Esac
Exit 0
If the above file name is sh02.sh, the command to execute this script is:
Copy Code code as follows: