Search code examples
shelltcsh

tsch: case statement isn't evaluating


EDIT: I originally thought my variable wasn't accepting input from the keyboard. I've corrected my question to more accurately reflect the issue.

In the script below, the case statement isn't evaluating.

I'm pretty new to tcsh... I just want input_line to accept characters a-d as input from the keyboard, which I'm not sure it is, and run the switch statement correctly.

I'm reading Sobell's "A Practical Guide to Linux Commands, Editors, and Shell Programming." My syntax seems correct based on the book's chapter 9 on tcsh, but, at this point, I have not idea what I'm doing wrong.

Searching through SO didn't yield similar questions, so either this is painful obvious to many, or no one's asked yet.

Can some one more experience take a look and indicate my error?

#!/bin/tcsh 
# menu interface to simple commands 
set input_line = ''
echo "\n        COMMAND MENU\n"
echo " a. Current date and time"
echo " b. Users currently logged in"
echo " c. Name of the working directory"
echo " d. Contents of the working directory\n"
echo -n "Enter a, b, c, or d: "
set input_line = "$<"
# 
switch ($input_line)
    case of [aA*]:
            date
            breaksw
    case of [bB*]:
            who
            breaksw
    case of [cC*]:
            pwd
            breaksw
    case of [dD*]:
            ls
            breaksw
    default:
            echo "There is no selection: $input_line"
            breaksw
ends

Just to be clear, I can get a script which does the exact same thing in bash to work correctly, but I'm focused on tcsh.


Solution

  • This line:

    set input_line = "$<"
    

    is working just fine. The problem is that you have the syntax of the case labels wrong. Change case of to case and it should work.

    (I'm curious why you thought the of was necessary.()