我试图让这个脚本正常运行,但我得到了./passmark.sh line 7: [y command not found
。
这是我的脚本
#!/bin/bash
# A simple script to ask a user for a pass mark and then display a grade
# the grade will be either an A, B, C, D or F
#
CONT=y
echo
while [$CONT = "y" ]
do
echo -e "\"Please enter a pass mark: \c \""
read MARK
if [$MARK -ge "0" -a $MARK -lt "50"] ; then
echo -e "\n \"F\" \n"
elif [$MARK -ge "50" -a $MARK -lt "60"] ; then
echo -e "\n \"D\" \n"
elif [$MARK -ge "60" -a $MARK -lt "70"] ; then
echo -e "\n \"C\" \n"
elif [$MARK -ge "70" -a $MARK -lt "80"] ; then
echo -e "\n \"B\" \n"
elif [$MARK -ge "80" -a $MARK -lt "100"] ; then
echo -e "\n \"A\" \n"
else
echo -e "\"Invalid input!!\n \""
fi
echo -e "\"Would you like to enter another pass mark? (y/n) \c \""
read REPLY
case "$REPLY" in
n | N | no | No | NO ) CONT=n ;;
*) ;;
esac
echo
done
答案1
您需要在[
和周围留有空格]
,例如
if [ "$MARK" -ge 0 -a "$MARK" -lt 50 ]; then
按照您编写的方式,当$MARK
is 时7
,它会尝试执行[7
命令,而不是7
作为参数传递给[
命令([
只是 的缩写test
)。
您还应该引用该变量。否则,如果用户输入空行或多个单词,则测试表达式将无法正确解析。