Currently I’m trying to get a menu working for my Into to Unix class. The problem I’m getting right now is that when I input something into the script my input appears instead of the script responding as how it should base on the input. But base on the book I should be doing it right since I followed the format that case is used, or so I think? What would I need to modify in order to make this work? Also the shell I'm using is bash.

This is the assignment I’m working on:

  1. Write a case script that will give the choices to do the following:

    a. List the users of the /etc/passwd file that have a four digit ID number greater than 999.

    b. Show only the month, day, and year from the date command.

    c. Take the file of the user’s choosing and show all the letters in uppercase.

Here is the code I'm using at the moment

if [ "$#" -ne 1 ]; then
    echo Press 1 for a list of users with the UID greater than 999
    echo Press 2 for the Day, Month, and Year
    echo Press 3 to show a file name from lower case to upper case
    read a

case "$1" in
    1) awk -F: '($3>=999)  && ($3!=9999)' /etc/passwd ;;
    2) echo Todays date is: ; date | awk '{print $2, $3, $6}' ; sleep 1 ;;
    3) ls -aC | more ; echo What file would you like to change from lower case to uppercase?  ;read y ;sleep 1 ; clear ; echo $y | tr '[:lower:]' '[:upper:]' ; sleep 1 ;;
    *) echo Invalid input
  • Ho do you run your script? – coffeMug Nov 21 '13 at 8:38
  • 1
    Sure you want to unconditionally exit after diplaying the options? No idea about what your teacher wants, but I would not accept that date piped into awk. (Which on my system displays “21 Nov,” as there date displays only 5 fields.) Try something like date +'Today is %d %B %Y' instead with no awk. – manatwork Nov 21 '13 at 11:23
  • Again, not sure about what your teacher wants, but I have a feeling (s)he wants you to use select: pastebin.com/qTC59eur – manatwork Nov 21 '13 at 11:53
  • 1
    @manatwork It seems to me that the exercise is aimed specifically at writing a case statement, which a select loop would supersede. – Joseph R. Nov 21 '13 at 13:08

As @manatwork noted in the comments, you don't want to exit right after the user enters their choice. Also, your case statement operates on $1, the first argument to the script (or the first positional parameter in Bash lingo). Logically, that's not what you want. You read the user's choice into a, so that's what you want to be caseing:

case "$a"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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