方法1
#!/bin/bash
#Author:mcsiberiawolf
#Time:2019-02-12 11:15:49
#Name:menu.sh
#Version:V1.0
#Description: This is a test script.
RETVAR=0
# 定義腳本路徑
path=/server/scripts
# 如果路徑不存在就創建
[ -d "$path" ] && mkdir -p $path
# 定義幫助函數
function Usage(){
echo "Usage:$0 argv"
return 1
}
# 定義安裝服務函數
function InstallService(){
if [ $# -ne 1 ]; then
Usage
fi
local RETVAR=0
echo "start installing ${1}."
sleep 2
if [ ! -x "$path/${1}.sh" ]; then
echo "$path/${1}.sh does not exist or can not be exec."
return 1
else
$path/${1}.sh
return $RETVAR
fi
}
# 定義主函數
function main(){
# 菜單提示
PS3="`echo Pls input the num you want:`"
# select 循環,菜單內容列表,列表中有空格加引號
select var in "Install lamp" "install lnmp" "exit"
do
case "$var" in
"Install lamp")
InstallService lamp
RETVAR=$?
;;
"Install lnmp")
InstallService lamp
RETVAR=$?
;;
exit)
echo bye.
return 3
;;
*)
echo "The num you input must be {1|2|3}"
echo "Input ERROR"
esac
done
exit $RETVAR
}
main
方法2
#!/bin/bash
#Author:mcsiberiawolf
#Time:2019-02-12 11:15:49
#Name:menu.sh
#Version:V1.0
#Description: This is a test script.
RETVAR=0
path=/server/scripts
[ -d "$path" ] && mkdir -p $path
function Usage(){
echo "Usage:$0 argv"
return 1
}
function InstallService(){
if [ $# -ne 1 ]; then
Usage
fi
local RETVAR=0
echo "start installing ${1}."
sleep 2
if [ ! -x "$path/${1}.sh" ]; then
echo "$path/${1}.sh does not exist or can not be exec."
return 1
else
$path/${1}.sh
return $RETVAR
fi
}
function main(){
PS3="`echo Pls input the num you want:`"
select var in "Install lamp" "install lnmp" "exit"
do
case "$REPLY" in
1)
InstallService lamp
RETVAR=$?
;;
2)
InstallService lamp
RETVAR=$?
;;
3)
echo bye.
return 3
;;
*)
echo "The num you input must be {1|2|3}"
echo "Input ERROR"
esac
done
exit $RETVAR
}
main