在 UNIX 脚本中的 if 语句中不断出现错误
Keep getting error in if statement in UNIX Script
我不得不编写一个脚本来执行 2 个命令行参数来执行任务。除了一个 while 循环。我在第 16 行的 if 语句中遇到问题。shell 产生以下内容:
./asg6s:第 16 行:意外标记附近的语法错误 fi'
'/asg6s: line 16:
fi
我的代码如下:
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage [=10=] inputfile outputfile"
exit 1
fi
# check if input file exists
if [ ! -e ]; then
echo " not found!"
exit 1
fi
#check if input file is empty
if [ ! -s $FILE ] ; then
echo " is empty"
exit 1
fi
# copy contents of first file to second
cat >
while true
do
clear
# display the menu
echo "University of Maryland."
echo "purpose of using the app"
echo -en '\n'
echo "Choose one of the following:"
echo "1 Addition"
echo "2 Subtraction"
echo "3 Multiplication"
echo "4 Division"
echo "5 Modulo"
echo "0 Exit"
echo -en '\n'
#take input for operation
read N
case $N in
1) NAME="add";OP="+";;
2) NAME="subtract";OP="-";;
3) NAME="multiply";OP="*";;
4) NAME="divide";OP="/";;
5) NAME="modulo";OP="%";;
0) echo "The progam is ending" ; exit 0;;
*) echo “Not an Acceptable entry.” ;continue;
esac
#take input numbers
echo "Enter two numbers"
read A
read B
#display value on screen and also append in the output file
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A $OP $B`
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A $OP $B` >
done
如有任何帮助,我们将不胜感激。
编辑:
在上面的相同代码中,循环语句出现问题。好吧,我应该说,在我向程序中输入整数后,我无法让程序为我打印答案。具体来说,它什么都不做,然后返回到要求我输入我想要完成的操作的位置。任何帮助将不胜感激。
您的脚本即将运行。
在显示 expr 的结果后,您的脚本继续执行 while 循环并调用 clear。如果要查看结果,必须在清除或读取虚拟键输入后显示结果。
另一个问题是变量 $OP,它可能是一个 *.当 * 被评估为一组文件时,您的 expr 语句将不起作用。
最短的更改是添加一条读取语句并引用您的 $OP:
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A "$OP" $B`
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A "$OP" $B` >
read dummy
剧本当然可以改。您真的要用结果覆盖 $2 还是附加到文件?
2条回声线可以和tee放在一起。
我会将 clear 移动到 while 语句上方,并将脚本的最后一部分替换为
read A
read B
clear
#display value on screen and also append in the output file
echo "The operation is to ${NAME}. The result of ${NAME} $A and $B is $(expr $A "${OP}" $B)" | tee -a
echo
done
我不得不编写一个脚本来执行 2 个命令行参数来执行任务。除了一个 while 循环。我在第 16 行的 if 语句中遇到问题。shell 产生以下内容:
./asg6s:第 16 行:意外标记附近的语法错误 fi'
'/asg6s: line 16:
fi
我的代码如下:
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage [=10=] inputfile outputfile"
exit 1
fi
# check if input file exists
if [ ! -e ]; then
echo " not found!"
exit 1
fi
#check if input file is empty
if [ ! -s $FILE ] ; then
echo " is empty"
exit 1
fi
# copy contents of first file to second
cat >
while true
do
clear
# display the menu
echo "University of Maryland."
echo "purpose of using the app"
echo -en '\n'
echo "Choose one of the following:"
echo "1 Addition"
echo "2 Subtraction"
echo "3 Multiplication"
echo "4 Division"
echo "5 Modulo"
echo "0 Exit"
echo -en '\n'
#take input for operation
read N
case $N in
1) NAME="add";OP="+";;
2) NAME="subtract";OP="-";;
3) NAME="multiply";OP="*";;
4) NAME="divide";OP="/";;
5) NAME="modulo";OP="%";;
0) echo "The progam is ending" ; exit 0;;
*) echo “Not an Acceptable entry.” ;continue;
esac
#take input numbers
echo "Enter two numbers"
read A
read B
#display value on screen and also append in the output file
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A $OP $B`
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A $OP $B` >
done
如有任何帮助,我们将不胜感激。
编辑:
在上面的相同代码中,循环语句出现问题。好吧,我应该说,在我向程序中输入整数后,我无法让程序为我打印答案。具体来说,它什么都不做,然后返回到要求我输入我想要完成的操作的位置。任何帮助将不胜感激。
您的脚本即将运行。
在显示 expr 的结果后,您的脚本继续执行 while 循环并调用 clear。如果要查看结果,必须在清除或读取虚拟键输入后显示结果。
另一个问题是变量 $OP,它可能是一个 *.当 * 被评估为一组文件时,您的 expr 语句将不起作用。
最短的更改是添加一条读取语句并引用您的 $OP:
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A "$OP" $B`
echo "The operation is to $NAME. The result of $NAME $A and $B is" `expr $A "$OP" $B` >
read dummy
剧本当然可以改。您真的要用结果覆盖 $2 还是附加到文件?
2条回声线可以和tee放在一起。
我会将 clear 移动到 while 语句上方,并将脚本的最后一部分替换为
read A
read B
clear
#display value on screen and also append in the output file
echo "The operation is to ${NAME}. The result of ${NAME} $A and $B is $(expr $A "${OP}" $B)" | tee -a
echo
done