I’m trying to write a professional program to accept and process input via a Menu based system. The program should have no command line arguments. it will be writen in csh script called TaskMenu. This shell script will:
- ask for a password from the user.
a. If the password is not correct, the system will exit with an appropriate error message. - display a text menu and then get a response from the user.
- process the user input and redisplay the text menu until the user wants to quit.
Advertisement
Answer
To read a password, turn echo off, read the password, then re-enable echo. csh:
stty -echo echo -n "Enter password: " set password = $< stty echo
To create a menu, just echo the choices to the screen, then read a value back. csh:
echo 1 first item echo 2 second item echo 3 third ... echo -n "Enter Choice: " set choice = $<
These same two tasks in bash are:
Read password:
echo -n "Enter password: " read -s password
Make menu:
select choice in "first item" "second item" "third..." do test -n "$choice" && break; done
Notice how reading a password and making a menu are built-in to bash. In addition to being easier, some common things done in scripting are impossible in csh. Csh is not designed as a scripting language. It is much easier to use Bash, Python, Ruby, Perl or even the lowly /bin/sh for scripting anything over a few lines.
That said, here is a full csh script showing the password and menu methods:
#! /bin/csh set PW="ok" ### Read Password echo -n "enter passwd: " stty -echo set passwd = $< stty echo if ( "$passwd" == "$PW" ) then echo Allrighty then! else echo "Try again. Use '${PW}'." exit 1 endif ### Menu @ i = 1 set items = (one two three four five six seven) foreach item ( $items[*] ) echo "${i}) $item" @ i = $i + 1 end set choice = 0 while ( $choice < 1 || $choice > $#items ) echo -n "Select: " set choice = $< end echo "You chose ${choice}: $items[$choice]"
Notes
Though popular for interactive use because of its many innovative features, csh has never been as popular for scripting[1]