卷曲变量不起作用

时间:2015-02-02 14:49:53

标签: bash curl

我试图从curl请求中获取特定字段。它通过shell工作得很好,但是一旦我尝试分配一个变量,它就会停止工作。

这是独立的命令。

curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -E '  <title>' | awk -F '[<>]' '{print $3}'

这是脚本中的一小部分。它回应了一个空白变量。

weatheralert=$(curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -E '  <title>' | awk -F '[<>]' '{print $3}')
echo "Current Weather Alert: $weatheralert"

1 个答案:

答案 0 :(得分:1)

请注意,tab标记之前存在<title>个字符,而不是3或4个空格。因此,请复制粘贴制表符或使用grep -P '\t<title>'命令。

$ weatheralert=$(curl --silent 'http://alerts.weather.gov/cap/wwaatmget.php?x=MNC131&y=0' | grep -P '\t<title>'|awk -F '[<>]' '{print $3}')
$ echo "Current Weather Alert: $weatheralert"
Current Weather Alert: There are no active watches, warnings or advisories