2010-09-12 139 views
1

我正试图编写一个专业的程序来接受和处理通过菜单系统的输入。该程序应该没有命令行参数。它将写在名为TaskMenu的csh脚本中。此shell脚本将:csh用户的密码授权脚本

  1. 要求用户输入密码。
    a。如果密码不正确,系统将退出 并显示相应的错误消息。
  2. 显示文本菜单,然后得到用户的响应。
  3. 处理用户输入并重新显示文本菜单 ,直到用户想要退出。
+0

附:我不知道如何写在csh脚本中 – GuzzyD 2010-09-12 07:09:45

回答

4

要读取密码,关闭回声,读取密码,然后重新启用回声。 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或甚至低级脚本来编写任何脚本都容易得多。

这就是说,这里是显示密码菜单方法全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

1

问:到底为什么会有人想在bash世界使用CSH)CSH是SOOOO 1985;)