2010-09-12 133 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;)