shell脚本练习

回复 收藏
本帖最后由 夏夏 于 2014-12-17 15:31 编辑

写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数值,然后再次要求用户输入数值。直到用户输入"end"停止。
2014-12-16 17:21 举报
已邀请:
0

川娃子在大连

赞同来自:

本帖最后由 川娃子在大连 于 2014-12-18 16:38 编辑
  1. #!/bin/bash
  2. # -------------------------------------------------------------------------------
  3. # Filename:    excbyxiaxia.sh
  4. # Revision:    1.0
  5. # Date:        2014/12/16
  6. # Author:      FUQIANG LI
  7. # Email:       <a href="mailto:317377106@qq.com">317377106@qq.com</a>
  8. # Notes:       aminglinux shell exercises of 20141216 by xiaxia
  9. # -------------------------------------------------------------------------------
  10. while :
  11. do
  12.         read -p "Please input a number:(end for exit) " number

  13.         num=` echo $number |sed -r 's/[0-9]//g'|wc -c `
  14.         if [ $number == "end" ]
  15.                 then
  16.                         exit
  17.                 elif [ $num -ne 1  ]
  18.                         then
  19.                         echo "what you input is not a number!Try again!"
  20.                 else
  21.                         echo "your input number is: $number"
  22.         fi
  23. done



0

MR_K

赞同来自:

  1. #!/bin/bash
  2. #written by yangkun at 2014-12-16 20:00
  3. while :;do
  4.   read -p "Please input a number:" i
  5.   m=`echo $i|sed s/[0-9]//g|wc -c`
  6.   if [ $i == "end" ];then
  7.       exit 0
  8.   elif [ $m -eq 1 ];then
  9.       echo $i
  10.   else
  11.       continue
  12.   fi
  13. done
0

丁张龙

赞同来自:

你让用户输入一个数,用户就会按提示输入啊,用户怎么知道要输入end呢?
0

夏夏

赞同来自:

丁张龙 发表于 2014-12-16 21:44
你让用户输入一个数,用户就会按提示输入啊,用户怎么知道要输入end呢?

字符
0

cherrypi

赞同来自:

  1. #! /bin/bash
  2. #执行后打印“please input a number:",用户输入数值,打印该数值
  3. #用户继续输入数值,直到输入”end“才结束。
  4. #+------华丽的分割线----------+#
  5. #Date:2014-12-16 23:00
  6. #Author:jianghongxi
  7. read -p "Please input a number: " a
  8. echo "The number you have input is : $a"
  9. read -p "Please input another number: " b
  10. echo "The number you have input is : $b"
  11. while (($b!="end"));do
  12.     read -p "Please input another number:" b
  13.     if (($b!="end"));then
  14.         echo "The number you have input is : $b"
  15.     else
  16.         break
  17.     fi
  18. done
0

wuhen

赞同来自:

本帖最后由 wuhen 于 2014-12-17 11:06 编辑
  1. #!/bin/bash
  2. #The author is ren
  3. #Written by 2014-12-17
  4. while :;
  5. do
  6.         read -p "Please input a number:(input end stop) " n
  7.         num=` echo $n |sed -r 's/[0-9]//g'|wc -c `
  8.         if [ $n == "end" ]
  9.                 then
  10.                         exit
  11.                 elif [ $num -ne 1 ]
  12.                         then
  13.                         echo "your input is not a number!Try again!"
  14.                         echo "your input is: $n"
  15.                 elif [ $num -eq 1 ]
  16.                         then
  17.                         echo "your input is a number!Try again!"
  18.                         echo "your input is: $n"
  19.                 else
  20.                 continue
  21.         fi
  22. done


[/code]
0

wuhen

赞同来自:

本帖最后由 wuhen 于 2015-2-28 18:23 编辑
  1. #!/bin/bash
  2. while :;
  3. do
  4. read -p "Please input a number(input end for stop):" n
  5. if [ $n == "end" ]
  6. then
  7.    exit 0
  8. elif [ $n != "end" ]
  9. then
  10.     echo $n
  11. else
  12.    break
  13. fi
  14. done

0

成都-小熊

赞同来自:

#! bin/bash
##write by lxx 12-17-10:33

while :;do
read -p "please input a number: " a
b=`echo $a|sed s/[0-9]//g|wc -c`
if [ $a == "end" ];then
        exit 0
elif [ $b -eq 1 ];then
        echo $a
else
        continue
fi
done
0

韩瑜

赞同来自:

#1/bin/bash
##this is Han Yu written in 2015-12-16
while :;do
read -p "Please input a number:" a
     b=`echo $a|sed s/[0-9a-zA-Z]//g|wc -c`
     if [ $a == "end" ];then
         exit 0
     elif [ $b -eq 1 ];then
         echo $a
     else
         continue
     fi
done
{:7_172:}没思路,我是盗版的阿坤的
0

王肖强

赞同来自:

  1. #!/bin/bash

  2. ## Script Filename : print_test.sh.
  3. ## Writen by Wangxiaoqiang 2014/12/17.

  4. while :;do
  5.   read -p "Please input a number : " number
  6.   [ "$number" == "end" -o "$number" == "END" -o "$number" == "End" ] && break
  7.   key=`echo ${number} | grep -Po '[\d]+'`
  8.   [ -z "$key" ] || [ "$key" != "$number" ] && continue || echo $key ; continue
  9. done
0

honglang1020

赞同来自:

  1. #!/bin/bash
  2. cat<< EOF
  3. input "END" quit
  4. EOF
  5.   read -p "Please input a number:" num
  6. while [ $num != END ];do
  7.   [ `echo $num|sed 's/[0-9]//g'|wc -c` -ne 1 ] && echo "gunnima" && exit 3
  8.   echo $num
  9.   read -p "Please input a number,again:" num
  10. done
0

649969462

赞同来自:

[root@localhost sbin]# cat 3.sh
#!/bin/bash

while :; do
  read -p "Please input a number: " n
  echo $n
  if [ $n == end -o $n == END ]; then
      exit
  fi
done

再问个问题, 执行我这个脚本的话,如果输入空中空格,他会报个错, 说是参数太多,如果哪位同学有解决办法,麻烦请留言。
0

649969462

赞同来自:

[root@localhost sbin]# cat 3.sh
#!/bin/bash

while :; do
  read -p "Please input a number: " n
  echo $n
  if [ "$n"x == "end"x -o "$n"x == "END"x ]; then
      exit
  fi
done

这样可以解决当你输入空或空格的报错。这里谢谢 5期张先 同学
0

白罂粟

赞同来自:

本帖最后由 白罂粟 于 2014-12-19 14:24 编辑
  1. #!/bin/bash
  2. #Author:LYNN Date:2014-12-19
  3. while :;do
  4.     read -p "input a number or 'end' or 'END':" n
  5.     echo $n
  6.     if [[ ! $n =~ ^[0-9]*$ ]];then
  7.          if [[ $n =~ ^[eE][nN][dD]$ ]];then
  8.             exit
  9.         else
  10.             echo "must be a number or 'end' or 'END'"
  11.         fi
  12.    fi
  13. done


0

xiaotuanyu120

赞同来自:

a="go" while [ ! -z $a ] do
         read -p "Please input a number:" a ;
         if [ "$a" != "end" ];
         then
                 echo $a;
         else
                 exit 0
         fi
done

0

t236xuchunfang

赞同来自:

本帖最后由 t236xuchunfang 于 2014-12-20 21:26 编辑

#!/bin/bash
while :

do
   read -p "Please input a number: " n
    if [ "$n" == "end" ]
    then

     exit

fi

    m=`echo $n|sed 's/[0-9]//g'`
       if [ -n "$m" ]
      then         echo "It's not number, Please input a number."
      else
     echo $n
fi
done
0

一起营销

赞同来自:

#!/bin/bash

while true
do
    echo -n "Please input a number:"
    read number
    echo $number\t"(input "end" can exit)"
    if [ $number == "end" ]
    then
        exit
    fi
done
0

649969462

赞同来自:

听了铭哥的复习课, 现在又重新写了一个,
#!/bin/bash

while :;
do
  read -p "Input a number please: " n

  if [ "$n" == "end" -o "$n" == "END" ]; then
    exit
  fi

  m=`echo $n |sed 's/[0-9]//g'`
  if [ -z "$m" ]; then
    echo "$n"
  else
    continue
  fi


done
0

路过

赞同来自:

#! /bin/bash
while :;
  do
read -p "Please input a number:" n
if [ $n = end ]
  then
   break
else
   echo $n
   continue
  fi
done
0

maria

赞同来自:

  1. #!/bin/bash

  2. while :
  3. do
  4.     read -p "Please input a number:" num;
  5.     numm=`echo $num | sed 's/[0-9]//g'`;
  6.     if [ "$numm" = "end" ]
  7.     then
  8.         echo "Bye!"
  9.         exit 0;
  10.     elif [ "$numm" != "" ]
  11.     then
  12.         echo "Your input is not a number!try again!";
  13.     else
  14.         echo "The number is $num!";
  15.     fi
  16. done
0

soul

赞同来自:

#!/bin/bash
while true
do
read -p "Please input a number:" a
case "$a" in
        1)
           echo $a
           ;;
        2)
           echo $a
           ;;
        3)
           echo $a
           ;;
        4)
           echo $a
           ;;
        end)
           exit
           ;;
        *)
           echo "input a number 1-4 or end(exit)"
           ;;
esac
done
0

zkq_315

赞同来自:

while [ 0 -eq 0 ]
do
        read -p "Please input a number:" num
        if [ "$num" == "end" ];then
                exit 100
        else
                echo "$num"
        fi
       
done
0

lyhabc

赞同来自:

#!/bin/bash
while :
do
read -p "Please input a number: " n
bb=$(echo $n|sed 's/[0-9]//g')
echo "bb :  $bb"
if [ -z $bb ]
then
    echo "is a number"
        echo $n
        continue
else
    echo "is not a number"
        exit
fi
if [ $n == "end"] ; then exit ;fi

done
0

youlianqing

赞同来自:

#! /bin/bash
while : ;do
read -p "please input a number :" num
if [ $num = end ];then
exit
else
echo "$num"
fi
done
0

huanglin

赞同来自:

#!/din/bash
unset var
while["please input a number:"]
do
   echo -n "please input a numder:"
   read var
    if["$var"=="end"]
     then
      brak
    fi
   echo "var is $var"
done
0

自己定义

赞同来自:

  1. #!/bin/bash
  2. while :; do
  3. read -p "please input a number: " x
  4. if [ $x == "end" ];then
  5.         exit
  6. else
  7.         b=`echo $x |sed 's/[0-9]//g'|wc -c`
  8.         #echo $b
  9.         if [ $b -gt 1  ];then
  10.                 echo "you should input a number"
  11.         else
  12.                 echo $x
  13.         fi
  14. fi
  15. done
0

linuxjishuren

赞同来自:

循环吗
0

jxcia2018

赞同来自:

  1. #!/bin/bash
  2. ##written by lin
  3. while :;
  4. do
  5. read -p "please input nmuber:" num
  6. echo $num
  7. if [ $num == "end" ]
  8. then break
  9. fi
  10. done
0

漠林sky

赞同来自:

学习了
0

hanhan0871

赞同来自:


报错了 提示"do"有问题
0

hanhan0871

赞同来自:


这个可以,完美!
0

hanhan0871

赞同来自:


直观,便于理解,可读性高
0

luckytodd

赞同来自:

#!/bin/bash
while :
do
  read -p "please input a num (input "end" to exit) " number
  num=`echo $number|sed 's/[0-9]//g'|wc -c`
  if [ "$num" -eq 1 ]
     then
        echo "you input num is " $number
  elif [ "$num" -ne 1 ]
     then
       case $number in
   end)
      exit
      ;;
     *)
      echo "what you input is not number"
      ;;
      esac
  fi
done
0

googleqicq

赞同来自:

while :
do
        read -p "Please input a number:(input end stop) " n
        num=`echo $n |sed -r 's/[0-9]//g'`
        if [ $n == "end" ]
                then
                        exit
                elif [ -n "$num" ]
                        then
                        echo "your input is not a number!Try again!"
                        echo "your input is: $n"
                elif [ -z "$m" ]
                        then
                        echo "your input is a number!Try again!"
                        echo "your input is: $n"
                else
                continue
        fi
done
0

kw是id

赞同来自:

#!/bin/bash
read -p "please input a number: " n
a=`echo $n|sed 's/[0-9]//g'`
if [ -n "$a" ]
then
   echo "please input only numbers"
   if [ $a == "end" ]
   then
     exit
   fi
else
   echo $n
fi

0

金银花

赞同来自:

while [ 1 ]

do

read -p "请输入一个数字:" n

echo $n

if [ $n == "end" ]

then

exit 0

fi

done

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: