我試圖創建一個bash命令,輸入一個介於1和12之間的數字,並且它使用了switch命令,但在bash shell中是case命令。我只是需要確保我有正確的想法去做這件事,或者如果我完成了關閉使用bash腳本並使用case語句
#!/bin/bash
1="January"
2="February"
3="March"
4="April"
5="May"
6="June"
7="July"
8="August"
9="September"
10="October"
11="November"
12="December"
case "1" in
print "$1"
case "2" in
print "$2"
case "3" in
print "$3"
case "4" in
print "$4"
case "5" in
print "$5"
case "6" in
print "$6"
case "7" in
print "$7"
case "8" in
print "$8"
case "9" in
print "$9"
case "10" in
print "$10"
case "11" in
print "$11"
case "12" in
print "$12"
echo "Please enter a number 1 through 12"
read input_variable
echo "$input_variable"
'1 = 1月份'也不是有效的變量賦值。如果你想通過'$ 12'設置'$ 1',你可以運行:'set - January February March April April June July August September'。 –
...但是,既然你使用bash,更好的答案是一個數組:'declare -a months =(January February ... etc ...);讀-r input_variable; echo「$ {months [$((input_variable - 1))}」'。對於這種用法,'case'陳述是過分的。 –
@CharlesDuffy我認爲是一樣的 - 我在我的答案中添加了一個基於數組的示例。 –