I was curious as to whether or not there is an equivalent to PowerShell's "Switch" command that lets you maneuver with input instead of using a multitude of "if-statements"
Asked
Active
Viewed 938 times
2
-
If only there were such a construct in Python. Alas. – DopeGhoti Apr 19 '18 at 18:48
1 Answers
11
If you're looking at a variable to determine flow, you want to use a case statement.
case "$var" in
val1)
do_something
;;
val2)
do_something_else
;;
esac
If you're looking to interactively get user input, you want to also use a select statement.
select action in proceed ponder perspire quit; do
case "$action" in
proceed)
go_on_then
;;
ponder)
have_a_think
;;
perspire)
exude_salty_secretions
;;
quit)
break
;;
esac
done
DopeGhoti
- 73,792
- 8
- 97
- 133
-
2Worth noting that it's matching against glob patterns. For example, if you write `[Qq]uit|exit|go-*)`. It'd match "Quit", "quit", "exit", and anything beginning with "go-" like "go-elsewhere". `default` would be `*)`, which is simply a glob that matches anything. – JoL Apr 19 '18 at 22:51