Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将terminal命令分配给bash变量时出错_Regex_Bash - Fatal编程技术网

Regex 将terminal命令分配给bash变量时出错

Regex 将terminal命令分配给bash变量时出错,regex,bash,Regex,Bash,我正在尝试制作一个小的shell程序来获取幸运饼干的报价。当我在终端上运行它时,它几乎可以正常工作 curl -s http://www.fortunecookiemessage.com | grep -oP '(<div class=\"quote\").*(</div>)' | sed 's/.*link\">\(.*\)<\/a>.*/\1/' 我需要帮助来解决这个问题。此外,有时提取的引号的格式为quote。这不是所有的时间,但有时只是。我想知道,如

我正在尝试制作一个小的shell程序来获取幸运饼干的报价。当我在终端上运行它时,它几乎可以正常工作

curl -s http://www.fortunecookiemessage.com | grep -oP '(<div class=\"quote\").*(</div>)' | sed 's/.*link\">\(.*\)<\/a>.*/\1/'
我需要帮助来解决这个问题。此外,有时提取的引号的格式为quote

。这不是所有的时间,但有时只是。我想知道,如果sed命令中的正则表达式出现了标记,我必须对其进行哪些更改才能删除这些标记

我的set-x输出

bash-3.2# sh run.sh 
++ curl -s http://www.fortunecookiemessage.com
++ grep -oP '(<div class=\"quote\").*(</div>)'
++ sed 's/.*link\">\(.*\)<\/a>.*/\1/'
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
    [-e pattern] [-f file] [--binary-files=value] [--color=when]
    [--context[=num]] [--directories=action] [--label] [--line-buffered]
    [--null] [pattern] [file ...]
(23) Failed writing body
+ sentence=
+ echo

您可以使用perl oneliner这样做:

perl -Mojo -E 'say g(q(http://www.fortunecookiemessage.com))->dom(q(div[class=quote]))->all_text'

但是您需要已经安装了套件。

您可以使用perl oneliner这样做:

perl -Mojo -E 'say g(q(http://www.fortunecookiemessage.com))->dom(q(div[class=quote]))->all_text'
但是您需要安装套件。

您的grep似乎不支持-p选项。将grep命令更改为:

grep -Eo '<div class="quote".*</div>'
您的grep似乎不支持-p选项。将grep命令更改为:

grep -Eo '<div class="quote".*</div>'

谢谢大家。阿努巴瓦的评论帮助我解决了这个问题。最后的答案是

sentence=$(curl -s http://www.fortunecookiemessage.com | grep -Eo '(<div class=\"quote\").*(</div>)' | sed 's/.*link\">\(.*\)<\/a>.*/\1/' | sed 's/<[^>]*>//g')

谢谢大家。阿努巴瓦的评论帮助我解决了这个问题。最后的答案是

sentence=$(curl -s http://www.fortunecookiemessage.com | grep -Eo '(<div class=\"quote\").*(</div>)' | sed 's/.*link\">\(.*\)<\/a>.*/\1/' | sed 's/<[^>]*>//g')

在交互模式和非交互模式下运行Bash时,您可能设置了不同的环境,这可能是因为在这两种情况下您没有启动相同的grep

比较在终端中运行哪个grep时的输出和添加到脚本中的同一命令的输出。如果它们不同,请在终端中运行该命令时使用该命令的输出在脚本中指定grep的完整路径


或者,正如@anubhava所建议的,更改您的命令参数以摆脱牵连选项。

在交互模式和非交互模式下运行Bash时,您可能设置了不同的环境,可能是在这两种情况下都没有启动相同的grep

比较在终端中运行哪个grep时的输出和添加到脚本中的同一命令的输出。如果它们不同,请在终端中运行该命令时使用该命令的输出在脚本中指定grep的完整路径


或者,正如@anubhava所建议的,更改您的命令参数,以摆脱“牵连”选项。

删除=”周围的空格。所以使用:句子=$curl-shttp://www.fortunecookiemessage.com |grep-oP“…也就是说,一般来说,从XML中提取内容的更好方法是使用实际的XML感知查询工具——xmlstarlet sel、xmllint-xpath等,这是xmlstarlet的-H-html派上用场的地方之一。如果你回答了grep-oP'@anubhava,那么oP可以接受它并得到他们的+2,而不是仅仅发布一条感谢你的评论。删除=。所以使用:句子=$curl-shttp://www.fortunecookiemessage.com |grep-oP“…也就是说,一般来说,从XML中提取内容的更好方法是使用实际的XML感知查询工具——xmlstarlet sel、xmllint-xpath等,这是xmlstarlet的-H-html派上用场的地方之一。如果你回答了grep-oP'@anubhava,那么oP可以接受它并得到他们的+2,而不是仅仅发布一条感谢你的评论。实际上没有必要将它作为一个新的答案发布,因为我已经在我的答案中发布了它。实际上没有必要发布它作为一个新的答案,因为我已经张贴在我的答案。