标签:开发 linux 结果 bin div 方式 class 面试题1 sed
企业面试题11:
开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。
注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。
我的脚本=========================
#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 }
合格linux运维人员必会的30道shell编程面试题及讲解-11
标签:开发 linux 结果 bin div 方式 class 面试题1 sed
原文地址:http://www.cnblogs.com/oliver-blogs/p/7723559.html