我正在做一个简单的确认提示,这是代码
read -r -p "Are You Sure? [Y/n] " input
case $input in
[yY][eE][sS]|[yY])
echo "Yes"
;;
[nN][oO]|[nN])
echo "No"
;;
*)
echo "Invalid input..."
exit 1
;;
esac
但是当我回答任何问题时,无论它是否有效,它都会显示错误
Quang-Anhs-iPhone-3GS:~ root# /var/mobile/Testarea/confirm.sh
Are You Sure? [Y/n] y
/var/mobile/Testarea/confirm.sh: line 1: read: `input
': not a valid identifier
/var/mobile/Testarea/confirm.sh: line 2:
: command not found
/var/mobile/Testarea/confirm.sh: line 3: syntax error near unexpected token `in
'
/var/mobile/Testarea/confirm.sh: line 3: `case $input in
'
那么我做错了什么?
答案1
因为我不知道 iOS 出了什么问题,但我找到了替代方案。这个替代方案的工作原理类似于选择菜单
function yes {
echo "Yes"
}
all_done=0
while (( !all_done )); do
options=("Yes" "Exit")
echo "Are you sure "
COLUMNS=12
select opt in "${options[@]}"; do
case $REPLY in
1) yes; break ;;
2) all_done=1; break ;;
*) echo "Invalid input..." ;;
esac
done
done
echo "Exiting"
答案2
这比你想象的更简单,首先保证你正在使用 bash,然后尝试这个:
#!/bin/bash
read -r -p "Are You Sure? [Y/n] " input
input=$(echo "$input" | tr "[:lower:]" "[:upper:]")
case $input in
Y*|S*|E*)
echo "Yes"
;;
N*|O*)
echo "No"
;;
*)
echo "Invalid input..."
exit 1
;;
esac
答案3
问题是cydia上的移动终端没有内置功能read
,所以这个脚本无法启动。