Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am currently writing my third ever shell script and I have run into a problem. This is my script so far:

#!/bin/bash
echo "choose one of the following options : \
  1) display all current users \
  2) list all files \
  3) show calendar \
  4) exit script"

while read  
do  
 case in  
        1) who;;  
        2) ls -a;;  
        3) cal;;  
        4) exit;;  
 esac    
done

when I try to run the script it says this:

line2 : unexpected EOF while looking for matching '"'  
line14 : syntax error: unexpected end of file.    

What am I doing wrong?

share|improve this question
    
Surely you mean "EOF", not "ECF"? –  l0b0 9 hours ago

4 Answers 4

The problem is, that your case statement is missing the subject - the variable which it should evaluate. Hence you probably want something like this:

#!/bin/bash
cat <<EOD
choose one of the following options:
1) display all current users
2) list all files
3) show calendar
4) exit script
EOD

while true; do
    printf "your choice: "
    read
    case $REPLY in
        1) who;;
        2) ls -a;;
        3) cal;;
        4) exit;;
    esac    
done

Here case uses the default variable $REPLY which read fills when it's not given any variable names (see help read for details).

Also note the changes: printf is used to display the prompt in each round (and doesn't append a newline), cat is used to print instructions on several lines so that they don't wrap and are easier to read.

share|improve this answer

Let's not forget select:

choices=( 
    "display all current users" 
    "list all files" 
    "show calendar" 
    "exit script"
)
PS3="your choice: "
select choice in "${choices[@]}"; do
    case $choice in
        "${choices[0]}") who;;
        "${choices[1]}") ls -a;;
        "${choices[2]}") cal;;
        "${choices[3]}") break;;
    esac
done
share|improve this answer

Let us try for a single case initially. I will use read -p to read the user input into a variable opt followed by case statement as below.

#!/bin/bash
read -p "choose one of the following options : \
  1) display all current users \
  2) list all files \
  3) show calendar \
  4) exit script" opt
case $opt in
1) who;;
2) ls -a;;
3) cal;;
4) exit;;
esac

The above script works fine and now, I believe you need to have it in a loop so that you can read user input until the user presses option 4.

So, we could do it with a while loop as below. I set the variable opt with the initial value as 0. Now, I am iterating in the while loop as long as the opt variable has a value as 0 (which is why I reset the opt variable as 0 at the end of the case statement).

#!/bin/bash
opt=0;
while [ "$opt" == 0 ]
do
read -p "choose one of the following options : \
  1) display all current users \
  2) list all files \
  3) show calendar \
  4) exit script" opt

case $opt in
1) who;;
2) ls -a;;
3) cal;;
4) exit;;
esac
opt=0
done
share|improve this answer

I would personally put the "while" at the beginning of the code. If you then follow it with a :, it will allow it to loop around as many times as you wish. This is how I would write it.

while :
do
    echo "choose one of the following options : \
      1) display all current users \
      2) list all files \
      3) show calendar \
      4) exit script"
    read string
    case $string in
        1)
            who
            ;;

then carry on the questions, and end with

esac
done
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.