0
#!/bin/bash
echo "Please input a number"
read num
expr $num "+" 10 &> /dev/null
if [ $? -eq 0 ]
then
echo "your input_num is a $num"
else
echo "$num not a num"
fi
我的只是一次判断。
学习铭哥的一下。
echo "Please input a number"
read num
expr $num "+" 10 &> /dev/null
if [ $? -eq 0 ]
then
echo "your input_num is a $num"
else
echo "$num not a num"
fi
我的只是一次判断。
学习铭哥的一下。
0
#!/bin/bash
while [ 1 ]
do
echo "Please input a number"
read num
expr $num "+" 10 &> /dev/null
if [ $? -eq 0 ]
then
echo "your just input_num is $num"
exit 0
else
echo "$num not a num"
fi
done
按照铭哥的那个执行 退出就可以了。呵呵
while [ 1 ]
do
echo "Please input a number"
read num
expr $num "+" 10 &> /dev/null
if [ $? -eq 0 ]
then
echo "your just input_num is $num"
exit 0
else
echo "$num not a num"
fi
done
按照铭哥的那个执行 退出就可以了。呵呵
0
- #!/usr/bin/env python
- # coding=utf-8
- import sys
- while True:
- try:
- input = raw_input('请输入一个数字:')
- except KeyboardInterrupt:
- sys.exit('\n')
- if input.isdigit():
- print input
0
#!/bin/bash
n=1
while [ ! -z "$n" ]
do
read -p "please input a number:" m
n=`echo $m|sed 's/[0-9]//g'`
if [ -n "$n" ]
then
echo "the character you input is not a number ,please retry"
else
echo $m
fi
done
n=1
while [ ! -z "$n" ]
do
read -p "please input a number:" m
n=`echo $m|sed 's/[0-9]//g'`
if [ -n "$n" ]
then
echo "the character you input is not a number ,please retry"
else
echo $m
fi
done
0
用case特别方便,就是多点,if的话更麻烦了
read -p "please input number : " i
case i in
0)
echo '0'
;;
1)
echo '1'
;;
`````````````````````
*)
read -p "dplease input number : " i
;;
esac
read -p "please input number : " i
case i in
0)
echo '0'
;;
1)
echo '1'
;;
`````````````````````
*)
read -p "dplease input number : " i
;;
esac
0
while :
do
read -p "Please input a number: " n
case n in
1)
echo $n
exit
;;
2)
echo $n
exit
;;
3)
echo $n
exit 0
;;
*)
read -p "Please input a number: " n
;;
esac
done
我这个脚本哪有错啊?用了exit退不出循环;
do
read -p "Please input a number: " n
case n in
1)
echo $n
exit
;;
2)
echo $n
exit
;;
3)
echo $n
exit 0
;;
*)
read -p "Please input a number: " n
;;
esac
done
我这个脚本哪有错啊?用了exit退不出循环;
0
#!/bin/bash
read -p "please input a number: " num
expr $num + 1 &> /dev/null
until [ $? -eq 0 ]
do
echo "Not a number,input again: "
read num
expr $num + 1 &> /dev/null
done
echo $num
read -p "please input a number: " num
expr $num + 1 &> /dev/null
until [ $? -eq 0 ]
do
echo "Not a number,input again: "
read num
expr $num + 1 &> /dev/null
done
echo $num
0
#!/bin/bash while true;do read -p "please input : " n if [[ $n =~ ^[1-9][0-9]*$ ]];then echo "$n" exit 10 else echo "不是数字继续输入......." continue fi done
0
#!/bin/bash while true;do read -p "please input : " n if [[ $n =~ ^[1-9][0-9]*$ ]];then echo "$n" exit 10 else echo "不是数字继续输入......." continue fi done
0
read -p "Please input num:" n
m=`echo $n|sed 's/[0-9]//g'`
while [ -n "$m" ];
do
read -p "Again! input num" n
m=`echo $n|sed 's/[0-9]//g'`
done
echo "Is num!!"
m=`echo $n|sed 's/[0-9]//g'`
while [ -n "$m" ];
do
read -p "Again! input num" n
m=`echo $n|sed 's/[0-9]//g'`
done
echo "Is num!!"
0
如果用户什么也不输入,也可通过验证。。
我的修正:
summer123 发表于 2015-8-11 10:46
#!/bin/bash
while [ 1 ]
do
如果用户什么也不输入,也可通过验证。。
- [root@master shell]# num=""
- [root@master shell]# expr $num "+" 10
- 10
- [root@master shell]# echo $?
- 0
我的修正:
- #!/bin/bash
- while :
- do
- read -p "please input a number :" num
- if [ ! -z $num ];then
- expr $num "+" 1 &> /dev/null
- if [ $? -eq 0 ];then
- echo "your input number is $num"
- break
- else
- echo "$num isn't a number."
- fi
- else
- echo " nothing character input, retry. "
- fi
- done
0
expr $num "+" 10 &> /dev/null
这一句红色部分的双引号可以不加,效果一样!
summer123 发表于 2015-8-11 10:46
#!/bin/bash
while [ 1 ]
do
expr $num "+" 10 &> /dev/null
这一句红色部分的双引号可以不加,效果一样!
0
- #!/bin/bash
- while :
- do
- read -p "请输入一个数字:" num
- n1=`echo -n $num|sed 's/[0-9]//g'`
- if [ -z $n1 ]; then
- echo "你输入的数字是:"$num
- break
- else
- echo "你输入的不是一个纯数字!"
- continue
- fi
- done
0
- #!/bin/bash
- flag=1
- read -p "输入一个数字" n
- while [ $flag -ne 0 ]
- do
- m=`echo $n | sed 's/[0-9]//g'`
- if [ -n "$m" ]; then
- read -p "请重新输入" n
- else
- echo $n
- flag=0
- fi
- done
0
#!/bin/bash
#写一个脚本,让用户输入一个数字,然后判断是否是数字,如果是数字,则打印数字,否>则一直让用户输入,直到是数字为止。
while :
do
read -p "please input your number: " num
if [ -z $num ]
then
echo "please input your number!"
continue
fi
n=`echo $num |sed -r 's/[0-9]//g'`
if [ ! -z $n ]
then
echo "you should input a real number"
continue
fi
break
done
echo $num
#写一个脚本,让用户输入一个数字,然后判断是否是数字,如果是数字,则打印数字,否>则一直让用户输入,直到是数字为止。
while :
do
read -p "please input your number: " num
if [ -z $num ]
then
echo "please input your number!"
continue
fi
n=`echo $num |sed -r 's/[0-9]//g'`
if [ ! -z $n ]
then
echo "you should input a real number"
continue
fi
break
done
echo $num
0
#!/bin/bash
while :
do
read -p "Please input a number:" n
n1=`echo $n | sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "Please input a number"
continue
fi
break
done
echo $n
while :
do
read -p "Please input a number:" n
n1=`echo $n | sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "Please input a number"
continue
fi
break
done
echo $n
0
- #!/bin/bash
- while :
- do
- read -p "Please input a number: " n
- n1=`echo $n|sed 's/[0-9]//g'`
- if [ -z $n1 ] && [ ! -z $n ] #判断用户不输入的情况
- then
- echo $n
- exit 0
- else
- continue
- fi
- done
0
一天一个shell小习题。搞定喽{:4_98:}
n=1
while [ ! -z "$n" ]
do
read -p "Please input a number:" num
n=`echo $num |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $num
fi
done
n=1
while [ ! -z "$n" ]
do
read -p "Please input a number:" num
n=`echo $num |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $num
fi
done
0
#!/bin/bash
read -p "please enter a number:" n
while :
do
pd=`echo $n|sed 's/[0-9]//g'`
if [ -z $pd ]
then
echo 'your number:' $n
break
else
read -p "please try again:" n
fi
done
read -p "please enter a number:" n
while :
do
pd=`echo $n|sed 's/[0-9]//g'`
if [ -z $pd ]
then
echo 'your number:' $n
break
else
read -p "please try again:" n
fi
done
0
#!/bin/bash
##用户输入,直到输入为数字,然后echo出来
m=1
while [ -n "$m" ]
do
read -p "Please input a number:" n
m=`echo $n |sed 's/[0-9]//g'`
if [ -z $n ]
then
break
fi
done
echo $n
##用户输入,直到输入为数字,然后echo出来
m=1
while [ -n "$m" ]
do
read -p "Please input a number:" n
m=`echo $n |sed 's/[0-9]//g'`
if [ -z $n ]
then
break
fi
done
echo $n
0
#!/bin/bash
while :
do
read num
if echo $num | egrep -q '^[0-9]+$'; then
echo 1
else
echo 0
fi
done
while :
do
read num
if echo $num | egrep -q '^[0-9]+$'; then
echo 1
else
echo 0
fi
done
0
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z $n ]
then
echo "你需要输入东西"
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "你只能输入一个纯数字"
continue
fi
break
done
echo $n
while :
do
read -p "Please input a number: " n
if [ -z $n ]
then
echo "你需要输入东西"
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]
then
echo "你只能输入一个纯数字"
continue
fi
break
done
echo $n
0
#!/bin/bash
while :
do
read -p "input a num: " aa
if [[ ${aa} =~ ^[0-9]+$ ]]
then
echo "you input a num"
exit 0
fi
done
while :
do
read -p "input a num: " aa
if [[ ${aa} =~ ^[0-9]+$ ]]
then
echo "you input a num"
exit 0
fi
done
0
- #!/bin/bash
- ##written by wangyl
- ##2016-04-06
- while :;
- do
- read -p "请输入一个数字:" num
- expr $num + 0 > /dev/null 2>$1
- if [ "$?" == 0 ];then
- echo $num
- else
- echo "这不是一个数字!"
- fi
- done
0
while :
do
read -p "please input a number:" a
m=`echo $a|sed 's/[ 0-9]//g'`
if [ -z $m ]
then
echo $a
exit
fi
done
do
read -p "please input a number:" a
m=`echo $a|sed 's/[ 0-9]//g'`
if [ -z $m ]
then
echo $a
exit
fi
done
0
本帖最后由 beacon 于 2016-4-8 14:40 编辑
#!/bin/bash
while :
read -p "please input a number: " n
m=`echo $n | sed 's/[1-9]//g'`
do
if [ -z $m ]
then
echo $n
exit
else
echo "pleases input again !"
fi
done
#!/bin/bash
while :
read -p "please input a number: " n
m=`echo $n | sed 's/[1-9]//g'`
do
if [ -z $m ]
then
echo $n
exit
else
echo "pleases input again !"
fi
done
0
#!/bin/bash
n=1
while [ -n "$n" ]
do
read -p "please input your number: " m
n=`echo $m|sed 's/[0-9]//g'`
echo $m
done
~
n=1
while [ -n "$n" ]
do
read -p "please input your number: " m
n=`echo $m|sed 's/[0-9]//g'`
echo $m
done
~
0
本帖最后由 duyanbin 于 2016-4-9 06:46 编辑
#!/bin/bash
#
num_check() {
while true ; do
read -p "Please input a number ==>"
if [[ $REPLY =~ ^-?[0-9]+$ ]] ; then # "=~" 表示匹配它后面的正则表达式 "-?^[0-9]+$"匹配数字(包括负数)
echo "The number is $REPLY"
break
else
echo "It's not a number,please input again"
continue
fi
done
}
num_check
#!/bin/bash
#
num_check() {
while true ; do
read -p "Please input a number ==>"
if [[ $REPLY =~ ^-?[0-9]+$ ]] ; then # "=~" 表示匹配它后面的正则表达式 "-?^[0-9]+$"匹配数字(包括负数)
echo "The number is $REPLY"
break
else
echo "It's not a number,please input again"
continue
fi
done
}
num_check
0
- #!/bin/bash
- while :; do
- read -p "Please input a number: " a
- b=`echo $a |sed 's/[0-9]//g' | wc -c`
- if [ $b = 1 ];then
- echo $a
- exit
- else
- continue
- fi
- done
0
#!/bin/bash
while :
do
echo "Please input a number: " n
read n
n1=`echo $n|sed 's/[0-9]//g'`
if [ -z $n1 ]
then
echo $n
exit 0
else
continue
fi
done
while :
do
echo "Please input a number: " n
read n
n1=`echo $n|sed 's/[0-9]//g'`
if [ -z $n1 ]
then
echo $n
exit 0
else
continue
fi
done
0
#!/bin/bash
read -p "please input a num:" num
m=`echo $num|sed 's/[0-9]//g'`
if [ -n "$m" ]
then
echo "you input is not a number!"
else
echo $num
fi
#!/bin/bash
read -p "please input a num:" num
m=`echo $num|sed 's/[0-9]//g'`
if [ -n "$m" ]
then
echo "you input is not a number!"
else
echo $num
fi
编辑回复