kbys.net
当前位置:首页 >> linux命令 SHELL编程:从键盘输入一个数,若大于0... >>

linux命令 SHELL编程:从键盘输入一个数,若大于0...

1、在shell编程中,输入一个数可以通过read命令来完成。 2、大于0输出该数,小于或等于0,输出0值,可以借助于if结构来判断。 3、示例如下 //read从键盘读取一个数保存到变量i中//并同时输出:“输入一个数”的字符提示read -p "输入一个数:" i;//...

#!/bin/bash echo "Please input a number:" #键盘读取赋值给变量num read num #判断num的值是否为数字,条件否则直接退出 expr $num + 0 1>/dev/null 2>&1 if [ $? -ne 0 ] then echo "${num} is not a number!" exit 0 fi #...

[lotto@logging ~]$ cat test.sh #!/bin/sh i=0 while [ 1 -gt 0 ] do echo -e "请输入数字:" read num if [ $num -gt 100 ] then echo "大于100" elif [ $num -lt 100 ] then echo "小于100" else echo "等于100,退出程序" break fi i=`expr $...

#!/bin/bashall=$*if [ $# -ne 10 ];then echo error|tee -a /tmp/err exit 1fisum=1for i in $alldo if [ $i -le 0 ];then echo error|tee -a /tmp/err exit 1 else sum=`expr $sum \* $i` fidoneecho $sum

#试试这个(保存并给予此文件x权限然后在终端运行): arg_cnt=$# arg_list=$* biggest=$1 if [ $arg_cnt -eq 0 ]; then echo "$RF 后面必须添加你想要做比较的数(以空格分隔)" exit 1 fi for each_arg in $arg_list do if [ $each_arg -gt $bigg...

1 #!/bin/bash 2 echo "**********please iput 5 number***********************" 3 echo "you must input one and press enter until input 5 number" 4 read x 5 max=$x 6 min=$x 7 avg=$x 8 i=1 9 while [ $i -lt "5" ] 10 do 11 let "i+=1" ...

echo -n "Enter three number:" read a b c if [ $a -gt $b ];then t=$a;a=$b;b=$t; fi if [ $a -gt $c ];then t=$a;a=$c;c=$t; fi if [ $b -gt $c ];then t=$b;b=$c;c=$t; fi echo "The small number is $a" echo "The big number is $c"

1、prime.sh实例 #!/bin/sh N=$1 if [ -z $1 ];then echo Usage:$0 num exit 0 fi for (( i=2; i

呵呵,这是鸟哥书上关于if...then语句的例子。 敲的时候仔细点就没事了。 个人觉得,其实这里用case语句做更方便,因为可以不用分别判断Y和y: case "$yn" in [Yy]) echo "ok, continue" ;; [Nn]) echo "oh, interrupt!" ;; *) echo "I don't kn...

没必要这么复杂 #!/bin/sh for i in `seq 100 ` do read var echo $var >> tempfile.tmp done echo "max number is :"`sort -n tempfile.tmp |head -n1 ` echo "min number is :"`sort -rn tempfile.tmp |head -n1 ` echo "sum of all number:"`...

网站首页 | 网站地图
All rights reserved Powered by www.kbys.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com