藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月26日 11:18


請教簡單Shell問題

我想請教一下,怎樣當n2=0的時候報錯,還有就是如何直接得到運行結果啊?
我剛開始學習Shell,麻煩了

#!/bin/sh
#Filename:calc.sh

"Caculator:"
while true
do
echo -n "a)+  s)-  m)*   d)/  q)=exit     "
echo -n "input:"

read op
case $op in
a) op="+" ;;
s) op="-" ;;
m) op="*" ;;
d) op="/" ;;
q) exit ;;
*) echo "wrong ,input again..."
continue ;;
esac
echo "input two numbers:"
read n1 n2

echo -n "$n1""$op""$n2"
RESULT=$[$n1$op$n2]
echo "="$RESULT
echo -n "continue (y/n)? "
read answer
case $answer in
n) break;;
y) continue;;
*) echo "wrong input!! error!!!"
break;;
esac
done


多謝 多謝

還有一個問題
Create a bash shell script that reads in a number from the user.
If the number is 1, print out the date.
If the number is 2, list the files in the current directory.
If the number is 3, print out who is currently logged onto the system.
If the number is anything else, print out an error message and exit.
不知道該如何做
謝謝!

-->

:mrgreen:上面用了case還不知道怎麼做......

[code] function opt(){ read x; case $x in 1) date ;; 2) ls ;; 3) w ;; *) echo "Error";; esac; }
[/code]

真的是初學阿,

還有就是,例如輸入ls後,能夠得知ls的參數的數量,不知道這個shell應該怎麼寫?

-->


[code]echo "ls -l -x -a" |awk -F'-' '{print NF-1}'[/code]:?::mrgreen:


% read x
ls -l -x
% $x
g.py Tuc.dat
% echo $x|awk -F'-' '{print NF-1}'
2


這也行啊

ls --help |grep '^ *-[a-zA-Z]'


把ls做一個變量就OK了,,



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com