
Nehmen wir an, ich habe ein Skript wie dieses:
#!/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
Ist es jetzt möglich, die Zeile echo "Something went wrong"
in einen Befehl zu ändern, der sofort ausgeführt wird Option two
, ohne das PS3-Menü erneut anzuzeigen?
Antwort1
Was Sie suchen, wird als „Fall-Through“ bezeichnet und in case
den Anweisungen von Bash wird Fall-Through mithilfe ;&
von anstelle von durchgeführt. Sie können jedoch kein bedingtes Fall-Through durchführen (d. h. Sie können kein in die Mitte eines Blocks ;;
einfügen ). Ich schlage vor, dass Sie immer ein Fall-Through durchführen und wenn der Code erfolgreich ausgeführt wurde:;&
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