我有一個 bash 腳本:
PS3='Please enter your choice: '
options=("1" "2" "3" "4" "Quit")
select opt in "${options[@]}"
do
case $opt in
"1")
echo "Set configuration"
break
;;
"2")
echo "Setting configuration and execution Install"
break
;;
"3")
echo "Setting configuration and execution Unlink"
break
;;
"4")
echo "Running tests"
break
;;
"Quit")
break
;;
*) echo "Selected option '$REPLY' couldn't be find in the list of options";;
esac
done
我有兩個問題:
- 如何使用預定義選項運行此腳本?(例如,我想用已經選擇的第一個選項執行這個腳本)
- 是否可以在另一個選項中重用一個選項?(例如,我的第一個選項只是設定配置,而我的第二個選項也設定了相同的配置,然后執行安裝,如果選項 2 選擇執行第一個選項然后執行第二個,它們可以寫成這樣嗎?)
如果寫得太糟糕,我愿意接受建議 =)
uj5u.com熱心網友回復:
如何使用預定義選項運行此腳本?(例如,我想用已經選擇的第一個選項執行這個腳本)
有點難看select,將所有案例邏輯移動到一個函式中(沒有中斷命令)
fun(){
case $1 in
"1") echo "Set configuration";;
"2") echo "Setting configuration and execution Install";;
"3") echo "Setting configuration and execution Unlink";;
"4") echo "Running tests";;
"Quit") :;;
*) echo "Selected option '$REPLY' couldn't be find in the list of options";;
esac
}
使您的腳本采用如下引數:
preset=$1
[[ $preset ]] && { opt=$preset; fun; } || select opt in "${options[@]}"
do
fun
break
done
...
是否可以在另一個選項中重用一個選項?(例如,我的第一個選項只是設定配置,而我的第二個選項也設定了相同的配置,然后執行安裝,如果選項 2 選擇執行第一個選項然后執行第二個,它們可以寫成這樣嗎?)
把options里的代碼變成函式,這樣你就可以輕松復用了
fun1(){ echo "Set configuration"; }
fun2(){ echo "Execution Install"; }
...
case $opt in
"1") fun1;;
"2") fun1; fun2;;
...
還有這些運算子用于case:;&和;;&
man bash
...
Using ;& in place of ;; causes execution to continue with the list associated with the next set of patterns.
Using ;;& in place of ;; causes the shell
to test the next pattern list in the statement, if any, and execute any associated list on a successful match.
因此,如果您想讓選項 1 在選擇了選項 2 的情況下也運行,可以這樣做:
case $opt in
"2") fun1;&
"1") fun1;;
...
但就我個人而言,我發現這種方法有點棘手且難以閱讀。
uj5u.com熱心網友回復:
如果將選擇部分放在函式中
main(){
select opt in "${options[@]}"
do
case $opt in
"1")
set_config # <--- an other funtion for option 1 to reuse it
break
;;
.
.
.
}
# set a default option
def_opt=1
# or use command line argument
def_opt="$1"
'$def_opt'您可以使用預定義的選項呼叫 mainyes
yes "$def_opt" | main
uj5u.com熱心網友回復:
在深入研究并盡力做到最好之后,我仍然需要一些幫助來完成我的腳本。
- 沒有任何引數的運行腳本現在可以完美運行。
- 以這種方式傳遞選項(getopts :c:i:u:d:s:h:) 在執行命令后會導致出現錯誤訊息
sh ./script.sh -c=>Wrong argument 'c' provided, run sh ./scripts/collection.sh -h for help - 以這種方式傳遞選項(getopts“ciudsh”)=>作業完美,但如果我使用未傳遞的引數(例如x),它會導致錯誤:
Wrong argument '' provided, run sh ./scripts/collection.sh -h for help或者有時甚至會導致錯誤Syntax error: "(" unexpected (expecting "fi")
請在下面查看我的完整腳本,不幸的是,出于安全原因,我無法發布函式本身的內容。
對于修復樣式、錯誤或其他任何問題,我將不勝感激。
根據您對stackoverflow的建議和其他答案,我得出了這個結論:
#!/usr/bin/env bash
#Colors
BRed='\033[1;31m'
Green='\033[0;32m'
BCyan='\033[1;36m'
NC='\033[0m'
f1(){
...
}
f2(){
...
}
f3(){
...
}
f4(){
...
}
f5(){
...
}
Help(){
echo -e "${Green}====================================================================================================================${NC}"
echo "You may execute the commands by selecting a number from menu or pass it as argument, see examples below:"
echo ""
echo -e "${Green}sh $0 ${BCyan}-argument${NC} :To execute specific command"
echo -e "${Green}sh $0 ${NC} :To see menu with all available options"
echo ""
echo -e "${BCyan} -c ${NC}..."
echo -e "${BCyan} -i ${NC}..."
echo -e "${BCyan} -u ${NC}..."
echo -e "${BCyan} -d ${NC}..."
echo -e "${BCyan} -s ${NC}..."
echo -e "${BCyan} -h ${NC}..."
echo -e "${Green}====================================================================================================================${NC}"
exit 1;
}
if [ $# -eq 0 ]
then
PS3='Please enter your choice: '
options=("1" "2" "3" "4" "5" "Help" "Quit")
select opt in "${options[@]}"
do
case $opt in
"1")
f1;;
"2")
f1; f2;;
"3")
f1; f2;;
"4")
f3;;
"5")
f4;;
"Help")
Help;;
"Quit")
break;;
*) echo -e "${BRed}Selected option ${BCyan}'$REPLY'${NC} ${BRed}couldn't be find in the list of provided options${NC}"
break;;
esac
done
fi
while getopts :c:i:u:d:s:h: OPTION
do
case $OPTION in
c)
f1;;
i)
f1; f2;;
u)
f1; f3;;
d)
f4;;
s)
f5;;
h)
Help;;
*) echo -e "${BRed}Wrong argument ${BCyan}'$OPTARG'${NC} ${BRed}provided, run${NC} ${BCyan}sh $0 -h${NC} ${BRed}for help${NC}"
esac
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/493698.html
標籤:重击
下一篇:Gnuplot:每分鐘頻率
