要读取密码,请关闭 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