Curl 无效的算术运算符-运行检查时
我试图评估运行卷曲检查的回报 我的剧本:Curl 无效的算术运算符-运行检查时,curl,integer,operator-keyword,Curl,Integer,Operator Keyword,我试图评估运行卷曲检查的回报 我的剧本: #!/bin/bash length=$(curl -s -I www.google.com | grep Length | awk {'print $2'}) if [[ $length -le 300 ]] then echo "length is too small" fi 输出为258: mira:~ david$ curl -s -I www.google.com | grep Length |
#!/bin/bash
length=$(curl -s -I www.google.com | grep Length | awk {'print $2'})
if [[ $length -le 300 ]]
then
echo "length is too small"
fi
输出为258:
mira:~ david$ curl -s -I www.google.com | grep Length | awk {'print $2'}
258
所以我想运行echo命令,因为它小于300
我得到一个错误:
mira:~david$./test.sh”)语法错误:算术运算符无效
(错误标记为“mira:~david$
非常感谢您的帮助。问题是返回车厢
\r
来自curl:
bash -x test.sh
++ grep Length
++ curl -s -I www.google.com
++ awk '{print $2}'
+ length=$'258\r'
+ '[' $'258\r' -le 300 ']'
: integer expression expected
您可以通过使用printf而不是在awk上打印来解决此问题:
#!/bin/bash
length=$(curl -s -I www.google.com | grep Length | awk '{printf "%d",$2}')
if [[ "$length" -le 300 ]]
then
echo "length is too small"
fi
结果:
tiago@dell:~$ bash test.sh
length is too small
tiago@dell:~$ bash -x test.sh
++ grep Length
++ curl -s -I www.google.com
++ awk '{printf "%d",$2}'
+ length=258
+ [[ 258 -le 300 ]]
+ echo 'length is too small'
length is too small
PS.
bash-x
对调试shell脚本非常有帮助。谢谢-我快秃顶了!