企業實踐題11:shell
開發shell腳本分別實現以腳本傳參以及read讀入的方式比較2個整數大小。以屏幕輸出的方式提醒用戶比較結果。bash
注意:一共是開發2個腳本。當用腳本傳參以及read讀入的方式須要對變量是否爲數字、而且傳參個數作判斷。 spa
個人腳本=========================code
#read讀入方式
#!/bin/bash read -p "pls input num1: " num1 read -p "pls input num2: " num2 [ -z $num1 -a -z $num2 ] && { echo "USAGE: pls input two num {num1 num2}" exit 1 } [ "`echo "$num1"|sed -r ' s#[^0-9]##g'`" = "$num1" ] || { echo "first arg must be int." exit 2 } [ "`echo "$num2"|sed -r ' s#[^0-9]##g'`" = "$num2" ] || { echo "second arg must be int." exit 3 } [ $num1 -lt $num2 ] && { echo "$num1 < $num2" } [ $num1 -gt $num2 ] && { echo "$num1 > $num2" } [ $num1 -eq $num2 ] && { echo "$num1 = $num2" }
#腳本傳參方式 #!/bin/bash [ $# -ne 2 ]&&{ echo "USAGE: num1 num2" exit 1 } [ "`echo "$1"|sed -r ' s#[^0-9]##g'`" = "$1" ]||{ echo "first arg must be int." exit 2 } [ "`echo "$2"|sed -r ' s#[^0-9]##g'`" = "$2" ]||{ echo "second arg must be int." exit 2 } [ $1 -lt $2 ]&&{ echo "$1<$2" exit 0 } [ $1 -eq $2 ]&&{ echo "$1=$2" exit 0 } [ $1 -gt $2 ]&&{ echo "$1>$2" exit 0 }