【问题标题】:csh scripting for password authorisation of user用于用户密码授权的 csh 脚本
【发布时间】:2010-09-12 06:48:02
【问题描述】:

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

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

【问题讨论】:

  • P.S.我不知道如何在 csh 脚本中写出来

标签: linux csh


【解决方案1】:

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

stty -echo 
echo -n "Enter password: "
set password = $<
stty echo

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

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

这两个在 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 = $<
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]"

注意事项

虽然流行于交互式使用 因为它的许多创新 功能,csh 从未像现在这样 流行于脚本[1]

1Wikipedia

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 2015-06-16
      • 2013-04-09
      • 1970-01-01
      相关资源
      最近更新 更多