我对bash没经验,但是我似乎遇到了 在编写脚本时,总是会遇到一些随机陷阱...
INPUT=""
input() {
read -n 1 -p ":" INPUT
}
console() {
if [[ $INPUT == "1" ]]; then
hideMenu
menu1
elif [[ $INPUT == "2" ]]; then
hideMenu
menu2
elif [[ $INPUT == "3" ]]; then
hideMenu
menu3
elif [[ $INPUT == "menu" ]]; then
showMenu
elif [[ $INPUT == "q" ]]; then
quit
fi
}
此代码有效。它将读取输入,然后按照操作进行操作。
然后我在不同的文件中看似相同的东西
J_INPUT=""
j_input() {
read -n 1 -p ":" J_INPUT
}
console() {
if [ $J_INPUT = "l"]; then
CURR_PAGE=$((CURR_PAGE + 1))
elif [ $J_INPUT = "j"]; then
CURR_PAGE=$((CURR_PAGE - 1))
elif [ $J_INPUT = "asd"]; then
CURR_PAGE=$((CURR_PAGE + 1))
fi
}
但是由于某种原因,它将无法运行。 另外,如果我现在尝试使用[[]],bash会抛出语法错误吗?
这是怎么了?为什么我不能使用相同的语法? [[]]的评价是什么?
谢谢。
答案 0 :(得分:1)
Shell对[ ]
周围的空格非常挑剔。
console() {
if [ $J_INPUT = "l" ]; then
CURR_PAGE=$((CURR_PAGE + 1))
elif [ $J_INPUT = "j" ]; then
CURR_PAGE=$((CURR_PAGE - 1))
elif [ $J_INPUT = "asd" ]; then
CURR_PAGE=$((CURR_PAGE + 1))
fi
}