Linux-shell之判斷大小

來源:互聯網
上載者:User

標籤:shell

實戰1: 輸入2個整數,判斷大小

三種方式實現:

1  定義變數          2  指令碼傳參             3 read讀入

第一步,給使用者提示,讓其輸入整數

第二步,判斷第一個值和第二個值不為空白

第三步,判斷兩個數為整數

第四步,第一個值與第二個值對比

 [[email protected] scripts]# cat test21.sh #!/bin/bashcat <<EOF  1-10...   zhengshuEOFread -p "panduan daxiao:" a b[ ${#a} -eq 0 ]&&{      echo "diyigecanshu"      exit 1}[ ${#b} -eq 0 ]&&{      echo "diergecanshu"      exit 1}expr $a + 1 &>/dev/nullRE_A=$?expr $b + 1 &>/dev/nullRE_B=$?if [ $RE_A -ne 0 -o $RE_B -ne 0 ]  then      echo "one of you input is not int"      exit 1fi if [ $a -eq $b ];       then      echo "$a = $b"  elif [ $a -gt $b ]      then      echo "$a > $b"  else      echo "$a < $b"fi [[email protected] scripts]# sh test2test20.sh  test21.sh  test2.sh   [[email protected] scripts]# sh test21.sh   1-10...   zhengshupanduan daxiao:e eone of you input is not int[[email protected] scripts]# sh test21.sh   1-10...   zhengshupanduan daxiao:2 342 < 34[[email protected] scripts]# sh test21.sh   1-10...   zhengshupanduan daxiao:3 33 = 3[[email protected] scripts]#

 

指令碼傳參方式實現

我這裡是對上面進行了改變。進到vim裡面

%s#$a#$1#g       %s#$b#$2#g    在刪除幾行,就實現了指令碼傳參的方式

 

 [[email protected] scripts]# cat test22.sh #!/bin/bash[ ${#1} -eq 0 ]&&{      echo "diyigecanshu"      exit 1}[ ${#2} -eq 0 ]&&{      echo "diergecanshu"      exit 1}expr $1 + 1 &>/dev/nullRE_A=$?expr $2 + 1 &>/dev/nullRE_B=$?if [ $RE_A -ne 0 -o $RE_B -ne 0 ]  then      echo "one of you input is not int"      exit 1fi if [ $1 -eq $2 ];       then      echo "$1 = $2"  elif [ $1 -gt $2 ]      then      echo "$1 > $2"  else      echo "$1 < $2"fi [[email protected] scripts]# sh test22.sh 3 done of you input is not int[[email protected] scripts]# sh test22.sh 3 43 < 4[[email protected] scripts]# sh test22.sh 3 33 = 3[[email protected] scripts]#

 

定義變數的方式

這裡我用以下命令替換了以下原有的變數,進行測試

 

 [[email protected] scripts]# sh test23.sh 3 < 4[[email protected] scripts]# sed -i "s#3#4#g" test23.sh [[email protected] scripts]# sh test23.sh 4 = 4[[email protected] scripts]# sed -i "s#4#dd#g" test23.sh [[email protected] scripts]# sh test23.sh one of you input is not int[[email protected] scripts]# cat test23.sh #!/bin/basha=ddb=dd[ ${#a} -eq 0 ]&&{      echo "diyigecanshu"      exit 1}[ ${#b} -eq 0 ]&&{      echo "diergecanshu"      exit 1}expr $a + 1 &>/dev/nullRE_A=$?expr $b + 1 &>/dev/nullRE_B=$?if [ $RE_A -ne 0 -o $RE_B -ne 0 ]  then      echo "one of you input is not int"      exit 1fiif [ $a -eq $b ];       then      echo "$a = $b"  elif [ $a -gt $b ]      then      echo "$a > $b"  else      echo "$a < $b"fi [[email protected] scripts]#

本文出自 “晴空” 部落格,謝絕轉載!

Linux-shell之判斷大小

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.