1

I have had to write a script that can execute 2 command line arguments to execute task. In addition to a while-loop. I am having trouble with the if statement on line 16. The shell produces the following:

./asg6s: line 16: syntax error near unexpected token fi' '/asg6s: line 16:fi

My code is as follows:

#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

Any help would be appreciated.

Edit:

In the same code above I have been getting a problem with the loop statement. Well I should say the fact that I cannot get the program to print the answer for me after I input to integers into the program. Specifically, it does nothing and goes back to the point where it ask me input for what operation I want to complete. Any help would be appreciated.

5
  • 1
    (Is this bash?) What is $FILE here? Purely a guess but maybe it should be $1, as per the lines above it. Commented Feb 28, 2015 at 2:42
  • Tried changing that it doesn't seem to fix anything Commented Feb 28, 2015 at 2:45
  • Try taking that code out of the script and seeing if you get an error. If so, something else before it might be the problem. Commented Feb 28, 2015 at 2:51
  • nothing obvious, except you don't have a shebang line (#!/bin/bash (or whatever)). So then you have to play the binary search, commenting out code until you isolate what is causing the problem. Agree with above that if there is anything else above (not shown here), that may be the source of your problem, although the line# 16 seems to match with your code. Commented Feb 28, 2015 at 4:31
  • Ok you guys were right I figured it out, I didn't have it executing in the korn shell. I fixed that but now I have a new error with the loop that I will edit so you can see if you see the error. Commented Feb 28, 2015 at 5:44

1 Answer 1

2

Your script is almost working. After showing the result of the expr your script continues with the while loop and calls clear. If you want to see the result, you must show the result after the clear or read a dummy key input.
Another problem is the variable $OP, that could be a *. When * is evaluated to a set of files, your expr statement will not work.

The shortest changes is adding a read statement and quoting your $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

Of course the script can be changed. Do you really want to overwrite $2 with the results or append to the file?
The 2 echo lines can be put together with tee.

I would move the clear to above the while statement and replace the last part of your script with

    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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.