基本SHELL編程

來源:互聯網
上載者:User

用SHELL寫的,那時候稍微瞭解了一下的,現在完全忘記了,不過看還是看的懂的,就是不會寫了,呵呵。都是老師布置的簡單題

 

1. 使用者選擇加減乘除,輸入兩個數字和結果,判斷使用者輸入的結果是否正確。

 #! /bin/bash<br /># this is the first homework<br />echo "input 1 to start and other numbers to exit:"<br />read sig<br />while [ $sig -eq 1 ]<br />do<br />echo -n "Please choose(+,-,*,/)"<br />read operation<br />echo -n "please input the 2 numbers to calculate:"<br />read a b<br />declare c<br />echo -n "please input the result:"<br />read result<br />mul=*<br />if [ "$operation" == "$mul" ]<br />then c=`expr $a /* $b`<br /> elif [ $operation = + ]<br /> then c=`expr $a + $b`<br /> elif [ $operation = - ]<br /> then c=`expr $a - $b`<br />elif [ $operation = / ]<br />then c=`expr $a / $b`<br />else<br />echo "error"<br />exit 1<br />fi<br />if [ $result -eq $c ]<br />then echo "you are right!"<br />else echo "you are wrong,the right answer is $c!"<br />fi<br />echo "input 1 to start again and any other numbers to exit"<br />read sig<br />done<br />echo "goodbye!" 

 

2. 實現將輸入的三個數由下到大排列 

#! /bin/bash<br /># this is the second homework<br />function order<br />{<br />if [ $c -gt $a ]<br />then<br /> t=$c<br /> c=$a<br /> a=$t<br />fi<br />if [ $b -gt $a ]<br />then<br /> t=$a<br /> a=$b<br /> b=$t<br />fi<br />if [ $c -gt $b ]<br />then<br /> t=$c<br /> c=$b<br /> b=$t<br />fi<br />}</p><p>echo -n "please input 3 numbers:"<br />read a b c<br />order a b c<br />echo "$a $b $c"<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.