shell 检测一串数字是否是IP地址和端口号
时间:2010-03-27 来源:huaihe0410
#!/bin/bash
#输入并判断WDDPS_Middleware的IP地址
#
readip () {
read -p "请输入访问WDDPS_Middleware的IP(如:192.168.88.96): " IP
}
chkip () {
echo "$IP" | grep -Eq '[^0-9.]|^\.|\.$' && usage
[ $(echo -e "${IP//./\n}" | wc -l) -ne 4 ] && usage
for i in ${IP//./ } ; do
[ $((i/8)) -lt 32 ] || usage
done
}
usage () {
echo "IP地址格式不正确!"
readip
chkip
}
if [ "$1" ]; then
IP=$1
else
readip
fi
chkip
#输入并判断WDDPS_Middleware端口号
#
readport ()
{
read -p "请输入访问WDDPS_Middleware的端口号(如:80):" port
}
chkport () {
echo "$port" |grep -Eq '[^0-9]' && usage2
if [ $port -ge 65536 ];then
usage2
fi
}
usage2 () {
echo "Port格式错误!"
readport
chkport
}
if [ "$1" ];then
$port=$1
else
readport
fi
chkport
sed -i "s/192.168.88.96:8080/$IP:$port/" agent.ini
shell中判断一个数是否大于小于某个数用 -ge -le 相当于(>=; <=)
如判断一个数是否大于等于50,小于等于1024,使用下面语句:
if [ [ $i -ge 50 ] && [ $i -le 1024 ] ]; then
......
操作符 比较
-lt 小于:less than
-le 小于等于:equal or less than
-eq 等于:equal
-ge 大于等于:equal or greater than
-gt 大于:greater than
-ne 不等于:not equal
#输入并判断WDDPS_Middleware的IP地址
#
readip () {
read -p "请输入访问WDDPS_Middleware的IP(如:192.168.88.96): " IP
}
chkip () {
echo "$IP" | grep -Eq '[^0-9.]|^\.|\.$' && usage
[ $(echo -e "${IP//./\n}" | wc -l) -ne 4 ] && usage
for i in ${IP//./ } ; do
[ $((i/8)) -lt 32 ] || usage
done
}
usage () {
echo "IP地址格式不正确!"
readip
chkip
}
if [ "$1" ]; then
IP=$1
else
readip
fi
chkip
#输入并判断WDDPS_Middleware端口号
#
readport ()
{
read -p "请输入访问WDDPS_Middleware的端口号(如:80):" port
}
chkport () {
echo "$port" |grep -Eq '[^0-9]' && usage2
if [ $port -ge 65536 ];then
usage2
fi
}
usage2 () {
echo "Port格式错误!"
readport
chkport
}
if [ "$1" ];then
$port=$1
else
readport
fi
chkport
sed -i "s/192.168.88.96:8080/$IP:$port/" agent.ini
shell中判断一个数是否大于小于某个数用 -ge -le 相当于(>=; <=)
如判断一个数是否大于等于50,小于等于1024,使用下面语句:
if [ [ $i -ge 50 ] && [ $i -le 1024 ] ]; then
......
操作符 比较
-lt 小于:less than
-le 小于等于:equal or less than
-eq 等于:equal
-ge 大于等于:equal or greater than
-gt 大于:greater than
-ne 不等于:not equal
相关阅读 更多 +