Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm writing a bash script for a class that has a main menu with two submenus. I haven't finished my script yet, but I already have a problem I haven't been able to remedy:

My script runs fine but every time I run it, I receive the following error (but my code still runs as intended):
./menu1.sh: line 105: =: command not found

Additionally, when I enter the other two submenus, I receive the same error (because of similar code): ./menu1.sh: line 7: e: command not found
./menu1.sh: line 79: r: command not found

The line that is causing the error is: until ($choice = q); which I'm using to stay on each individual menu until the user inputs q.

Adding set -x to my code shows + = q right before it prints the error.

Typing which until into the terminal yields no result (it doesn't print out any path).

Finally, typing echo $SHELL prints /bin/bash so I am using bash.

Here is all of my source code (unfinished):

#!/bin/bash

q=
cNum=
cNum1=
editSub () {
until ($choice = q); #ERROR OCCURS HERE
do
    echo ""
    echo "Edit Submenu:"
    echo ""
    echo "(a) Add a course"
    echo "(d) Delete a course"
    echo "(p) Display a course"
    echo "(u) Update a course (status and/or grade)"
    echo "(q) Return to Main Menu"
    echo ""
    echo -n "Enter choice: "
    read choice

    case $choice in
    a) 
    echo -n "Course Number:"
    read cNum
    if (grep $cNum my_course)
    then
        echo "$cNum already exists!"
    else
        awk '/'$cNum'/ {printf $1$2 OFS $4 OFS $5 OFS $6 OFS $7 OFS $8" 0 N/A"}' courses.txt | cat >> my_course #THIS NEEDS WORK. clear the extra stuff and add integer credit value
    fi
    ;;
    d)
    echo -n "Course Number: "
    read cNum
    if (grep $cNum my_course)
    then
        sed --in-place '/'$cNum'/d' my_course
        echo "$cNum successfully deleted."
    else
        echo "Course does not exist."
    fi
    ;;
    p) 
    echo -n "Course Number: "
    read cNum
    if (grep $cNum my_course)
    then
        awk'/'$cNum'/ {printf "Course Number: "$1"'
        #print the rest of the course info in a pretty format
    else
        echo "Course does not exist."
    fi
    ;;
    u)
    echo -n "Course Number: "
    read cNum
    echo -n "Status: "
    read status
    echo -n "Grade: "
    read grade
    if (grep $cNum my_course)
    then
        date
        #validate input for status and grade
        #if status 0 or 1, change my_course to say so
        #grade ABC, 1. N/A, 0.
        #error message if invalid input
    else
        echo "Course does not exist."
    fi
    ;; #FINISH THIS
    q) return ;;
    *) echo "Invalid choice.";;
    esac
done
}

reportSub () {
until ($choice = q); #ERROR OCCURS HERE
do
    echo ""
    echo "Report Submenu:"
    echo ""
    echo "(a) Sort by course number"
    echo "(p) Display all courses that you have taken"
    echo "(g) Calculate the GPA"
    echo "(q) Return to Main Menu"
    echo ""
    echo -n "Enter choice: "
    read choice

    case $choice in
    a) sort -n my_course
    ;;
    p) grep ' 1 ' my_course | sort
    ;;
    g) date ;; #calculate GPA
    q) return ;;
    *) echo "Invalid choice.";;
    esac
done
}

#main function
until ($choice = q); #ERROR OCCURS HERE
do
if [ -f my_course ]
then
    echo ""
    echo "Main Menu:"
    echo ""
    echo "(e) Edit Submenu"
    echo "(r) Report Submenu"
    echo "(q) Quit"
    echo ""
    echo -n "Enter choice: "
    read choice

    case $choice in
        e) editSub ;;
        r) reportSub ;;
        q) exit ;;
        *) echo "Invalid choice.";;
    esac
else
    >> my_course
fi
done
exit
share|improve this question
    
You might want to read about the select compound command, which you are mostly reimplementing. –  chepner Mar 27 '13 at 12:58

2 Answers 2

up vote 3 down vote accepted
until ($choice = q)

gives a syntax error, since the parentheses cause the command $choice = q to get executed in a a subshell environment.

In bash, you can use

until [[ $choice = q ]]

to check if the variable choice's value is equal to q.

For further information, see the section Compound Commands in man bash.

share|improve this answer
    
Thank you so much! I made a pretty rudimentary mistake, apparently, haha. –  Powdercake Mar 27 '13 at 2:02

if $choice can be empty you should add a character. if $choice can have blank characters, you should use "".

until [ "_$choice" = "_q" ]
do
done

And don't forget spaces after "[" and before "]" !

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.