使用正则表达式检查是否为数字或者url

2.使用正则检查是否为数字

if [[ "\(num1" =~ ^-?[0-9]+\) && "\(num2" =~ ^-?[0-9]+\) ]]; then
echo "$num1 $num2是数字"
else
echo "Usage: $0 数字1 数字2 2个数字"
exit 3
fi

-:负数
?:前面一个字符出现0次或1次

2.检查输入的是否是url地址

check_url() {
[[ \(url =~ ^[0-9a-zA-Z./:]+\) ]] || {
action "请输入有效的url地址" /bin/false
exit 1 }
}

3.检查url

url_status() {
status_code=curl -s -I $url |awk 'NR==1{print $2}'
[ -z "\(status_code" ] && { action "\)url is failed" /bin/false
exit 2 }

if [ \(status_code -lt 400 ];then action "\)url is ok!!!" /bin/true
else
action "$url is failed" /bin/false
fi
}

posted @ 2024-08-26 15:53  还得多长时间·  阅读(31)  评论(0编辑  收藏  举报
//雪花飘落效果