我必须编写一个脚本,可以执行2个命令行参数来执行任务。除了一个while循环。我在第16行的if语句遇到问题.hell产生以下内容:
./ asg6s:第16行:意外令牌fi'
'/asg6s: line 16:
附近的语法错误
我的代码如下:
#check if number of arguments are 2
if [ $# -ne 2 ]; then
echo "Does not equal two arguments"
echo "Usage $0 inputfile outputfile"
exit 1
fi
# check if input file exists
if [ ! -e $1 ]; then
echo "$1 not found!"
exit 1
fi
#check if input file is empty
if [ ! -s $FILE ] ; then
echo "$1 is empty"
exit 1
fi
# copy contents of first file to second
cat $1 > $2
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` > $2
done
任何帮助都将不胜感激。
编辑:
在上面的代码中,我遇到了循环语句的问题。好吧,我应该说,在输入整数到程序后,我无法让程序为我打印答案。具体来说,它什么都不做,并且回到它要求我输入我想要完成的操作的程度。任何帮助将不胜感激。
答案 0 :(得分:2)
您的脚本几乎正常运行。 在显示expr的结果后,您的脚本继续使用while循环并调用clear。如果要查看结果,必须在清除或读取虚拟键输入后显示结果 另一个问题是变量$ OP,可能是*。当*被评估为一组文件时,你的expr语句将不起作用。
最短的更改是添加一个read语句并引用$ 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` > $2
read dummy
当然可以更改脚本。你真的想用结果覆盖$ 2或附加到文件吗?
2个回波线可以与T形放在一起。
我会将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 $2
echo
done