
假設我有這樣的腳本:
#!/bin/bash
PS3='Select option: '
options=("Option one" "Option two")
select opt in "${options[@]}"
do
case $opt in
"Option one")
# few lines of code
if [ "check that code did everything it was supposed to do" ]
then
echo "Completed"
else
echo "Something went wrong"
fi
;;
"Option two")
# more code
;;
esac
done
現在是否可以將行更改echo "Something went wrong"
為立即運行的命令,Option two
而無需再次顯示 PS3 選單?
答案1
您正在尋找的稱為“fall-through”,在 bash 的case
語句中,fall-through 是使用;&
而不是 來完成的;;
。但是,您不能有條件地失敗(即,您不能在區塊;&
的中間插入 a if
)。我建議你總是失敗,continue
如果程式碼執行成功:
case $opt in
"Option one")
# few lines of code
if [ "check that code did everything it was supposed to do" ]
then
echo "Completed"
continue
else
echo "Something went wrong"
fi
;&
"Option two")
# more code
;;
esac