用于用户密码授权的 csh 脚本

发布于 2024-09-18 23:18:06 字数 214 浏览 7 评论 0原文

我正在尝试编写一个专业程序来通过基于菜单的系统接受和处理输入。该程序不应有命令行参数。它将被写入名为 TaskMenu 的 csh 脚本中。此 shell 脚本将:

  1. 向用户询问密码。
    一个。如果密码不正确,系统将退出 并带有适当的错误消息。
  2. 显示文本菜单,然后获取用户的响应。
  3. 处理用户输入并重新显示文本菜单 直到用户想要退出。

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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

メ斷腸人バ 2024-09-25 23:18:06

要读取密码,请关闭回显,读取密码,然后重新启用回显。 csh:

stty -echo 
echo -n "Enter password: "
set password = 
lt;
stty echo

要创建菜单,只需将选项回显到屏幕上,然后
读回一个值。 csh:

echo 1 first item
echo 2 second item
echo 3 third ...
echo -n "Enter Choice: "
set choice = 
lt;

bash 中的这两个相同的任务是:

读取密码:

echo -n "Enter password: "
read -s password

创建菜单:

select choice in "first item" "second item" "third..." do
test -n "$choice" && break; 
done

注意 bash 中如何内置读取密码和创建菜单。除了更容易之外,一些在脚本中完成的常见事情在 csh 中是不可能的。 Csh 并不是被设计为脚本语言。使用 Bash、Python、Ruby、Perl 甚至低级的 /bin/sh 来编写几行脚本要容易得多。

也就是说,这里是一个完整的 csh 脚本,显示了密码和菜单方法:

#! /bin/csh

set PW="ok"

### Read Password
echo -n "enter passwd: "
stty -echo
set passwd = 
lt;
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 = 
lt;
end
echo "You chose ${choice}: $items[$choice]"

注释

虽然在交互式使用中很受欢迎
由于其诸多创新
功能,csh从未如此
流行于脚本编写[1]

1 维基百科

To read a password, turn echo off, read the password, then re-enable echo. csh:

stty -echo 
echo -n "Enter password: "
set password = 
lt;
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 = 
lt;

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 = 
lt;
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 = 
lt;
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

隐诗 2024-09-25 23:18:06

问:到底为什么有人想在 bash 世界中使用 csh ;) csh 是 Soooo 1985 ;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文