开发者

csh scripting for password authorisation of user

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开发者_C百科. This shell script will:

  1. ask for a password from the user.

    a. If the password is not correct, the system will exit with an appropriate error message.

  2. display a text menu and then get a response from the user.
  3. process the user input and redisplay the text menu until the user wants to quit.


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]

1 Wikipedia


Q: Why in the heck would anybody want to use csh in a bash world ;) csh is Soooo 1985 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜