我寫了一個使用if - elif構造的小腳本。出於某種原因,它不會起作用。 錯誤代碼:elif的語法錯誤
./lab4.sh: line 9: syntax error near unexpected token `elif'
./lab4.sh: line 9: `elif [ $number -eq 2 ] then func2'
我的實際代碼:
#!/bin/bash
#ask the user for a number between 1 and 3
#create some functions, write out the function number
echo "Enter a number between 1 and 3: "
read number
#which function should be called?
if [ $number -eq 1 ] then func1
elif [ $number -eq 2 ] then func2
elif [ $number -eq 3 ] then func3 fi
function func1 {
echo "This message was displayed from the first function."
}
function func2 {
echo "This message was displayed from the second function."
}
function func3 {
echo "This message was displayed from the third function."
}
在看看:HTTP://www.shellcheck .net/ – Cyrus 2014-09-21 14:45:51
在'[$ number -eq 1]之後放一個分號;' – anubhava 2014-09-21 14:48:27
謝謝,這真的很有用 – masm64 2014-09-21 14:48:38