0
#!/bin/bash
##########################################################
#Author:17-HuangYi
#Email:caocao1990@163.com
#FileName:while.sh
#Version:1.0
#Created:2016-07-20
##########################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:~/bin
export PATH
function read_num(){
cat<
============================================
please input a number:
============================================
HY
}
while true
do
read_num
read -p "please:" num
expr $num + 1 &>/dev/null
if [ $? -eq 0 ];then
echo "this is number: $num"
break
fi
done
##########################################################
#Author:17-HuangYi
#Email:caocao1990@163.com
#FileName:while.sh
#Version:1.0
#Created:2016-07-20
##########################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:~/bin
export PATH
function read_num(){
cat<
please input a number:
============================================
HY
}
while true
do
read_num
read -p "please:" num
expr $num + 1 &>/dev/null
if [ $? -eq 0 ];then
echo "this is number: $num"
break
fi
done
0
#!/bin/bash
for i in [ -n "$n" ]
do
read -p "Please enter the Numbers:" m
n=`echo $m |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $m
exit
用for循环但是存在一个问题,不会一直循环。所有还是用while 循环比较好。
#!/bin/bash
n=0
while [ -n "$n" ]
do
read -p "Please enter the Numbers:" m
n=`echo $m |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $m
exit
fi
done
这样写可以解决需求 但是有一个问题,如果我不输入任何东西,直接回车,就会退出shell
原因在于我使用sed 将数字替换为空,为空就认为输入的是数字,不为空就有其他字符
for i in [ -n "$n" ]
do
read -p "Please enter the Numbers:" m
n=`echo $m |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $m
exit
用for循环但是存在一个问题,不会一直循环。所有还是用while 循环比较好。
#!/bin/bash
n=0
while [ -n "$n" ]
do
read -p "Please enter the Numbers:" m
n=`echo $m |sed 's/[0-9]//g'`
if [ -z "$n" ]
then
echo $m
exit
fi
done
这样写可以解决需求 但是有一个问题,如果我不输入任何东西,直接回车,就会退出shell
原因在于我使用sed 将数字替换为空,为空就认为输入的是数字,不为空就有其他字符
0
GaryHuang0113 - 世上无难事,只怕有心人
#!/bin/bash
if [ ! -n "$1" ]
then
echo "Please input a number" ;
exit 0
fi
result=`echo $1 | grep '^[0-9]*$'`
if [ -n "$result" ]
then
echo "$1 Is a Number"
else
echo "$1 Is Not Number"
fi
if [ ! -n "$1" ]
then
echo "Please input a number" ;
exit 0
fi
result=`echo $1 | grep '^[0-9]*$'`
if [ -n "$result" ]
then
echo "$1 Is a Number"
else
echo "$1 Is Not Number"
fi
0
#!/bin/bash
while :
do
read -p "please input a number:" num
num2=`echo $num|sed 's/[0-9]//g'`
echo "num2=$num2"
if [ -n "$num2" ]
then
echo "your input is not number,please try again."
continue
else
echo "your input : $num"
break
fi
done
while :
do
read -p "please input a number:" num
num2=`echo $num|sed 's/[0-9]//g'`
echo "num2=$num2"
if [ -n "$num2" ]
then
echo "your input is not number,please try again."
continue
else
echo "your input : $num"
break
fi
done
0
- echo "请输入一个数字"
- aaaa(){
- read nu
- #取出输入是否只有一个值
- w=`echo $nu |wc -L`
- #将输入的内容去掉数字,把值赋给变量
- n=`echo $nu |sed 's/[0-9]//g'`
- if [ -z "$n" ]&&[ $w = "1" ]
- then
- echo "输入正确,是一个数字"
- else
- echo "输入错误,请输入一个数字"
- aaaa
- fi
- }
- aaaa
0
- #! /bin/bash
- while :
- do
- read -p "Please input a number: " n
- if [ -z `echo $n|sed 's/[0-9]//g'` ]
- then
- echo "Your number is $n"
- exit
- fi
- done
0
#!/bin/bash
while true
do
read -p "请你输入一个数字 " num
a=`echo $num |sed 's/[0-9]//g'`
if [ -z $a ];then
echo "你输入的是一个数字"
exit
else
echo "你输入的不是数字,重新输入"
fi
done
while true
do
read -p "请你输入一个数字 " num
a=`echo $num |sed 's/[0-9]//g'`
if [ -z $a ];then
echo "你输入的是一个数字"
exit
else
echo "你输入的不是数字,重新输入"
fi
done
0
#!/bin/bash
while :
do
read -p "please enter a number: " n
a=`echo "$n"|sed 's/[0-9]//g'`
if [ -z "$a" ]
then
echo $n
exit
fi
done
while :
do
read -p "please enter a number: " n
a=`echo "$n"|sed 's/[0-9]//g'`
if [ -z "$a" ]
then
echo $n
exit
fi
done
0
4 while :
5 do
6 read -p "please input a num:" n
7 m=`echo $n|sed 's/[0-9]//g'`
8 if [ ! -z $m ]
9 then
10 continue
11 else
12 echo "you num is $n"
13 exit
14 fi
15 done
5 do
6 read -p "please input a num:" n
7 m=`echo $n|sed 's/[0-9]//g'`
8 if [ ! -z $m ]
9 then
10 continue
11 else
12 echo "you num is $n"
13 exit
14 fi
15 done
0
#!/bin/bash
#Cityzen Wang
#2017-04028
while :
do
read -p "Please input a number: " m
n=`echo $m | sed 's/[0-9]//g'`
if [ -n "$n" ];then
echo "numbers only"
else
echo $m
exit
fi
done
0
#!/bin/bash
while :
do
read -p "please input number:" m
expr $m "+" 1 &>/dev/null
if [ $? -eq 0 ]
then
echo $m
exit
fi
continue
done
编辑回复