1. 程式人生 > >Shell程式設計之if簡單判斷兩個數字大小

Shell程式設計之if簡單判斷兩個數字大小

#指令碼編輯


 

#!/bin/bash

#定義變數 num1=$1
num2=$2   #判斷是否輸入兩個引數,若是,將兩個引數傳遞給下一個指令動作,若非兩個引數,則列印輸出內容輸出並且退出exit指令碼不執行下一個指令
if [ $# -ne 2 ] ;then
   echo 'please input num1 & num2:'
exit
fi   #以上判斷後,輸入的兩個引數將傳遞到如下指令判斷
if [ $num1 -gt $num2 ] ; then
   echo $num1 great than $num2   else if [ $num1 -lt $num2 ] ; then    echo $num1 less than $num2
  else
   echo $num1 equal $num2   fi  
fi  
  #驗證是否正確   [[email protected] scripts]# sh compare2.sh 66 please input num1 & num2: [[email protected] scripts]# sh compare2.sh 10 10 10 equal 10 [
[email protected]
scripts]# sh compare2.sh 10 1 10 great than 1 [[email protected] scripts]# sh compare2.sh 10 50 10 less than 50 [[email protected] scripts]# sh compare2.sh 6 7 8 9 10 please input num1 & num2: