带有 grep 和 cut 的 bash 脚本 : command not found

标签 bash

我是 bash 新手,需要一些简单的脚本。它运行 jar,并且必须找到“响应代码:XXX”。我需要这个响应代码(只是 XXX)。我试过这个:

 URL=$1
echo $URL
callResult=`java -jar RESTCaller.jar $URL`
status=$?
if [ $status -eq 0 ]; then
    result=`$callResult >> grep 'RESPONSE CODE' | cut -d':' -f 2`
else
    echo error
fi

我得到./run.sh: line 7: RESPONSE: command not find

我做错了什么?

最佳答案

在这一行中:

result=`$callResult >> grep 'RESPONSE CODE' | cut -d':' -f 2`

您应该将输出通过管道传输到 grep,而不是重定向。将其更改为:

result=`$callResult | grep 'RESPONSE CODE' | cut -d':' -f 2`

此外,语法有点不对,最好尽可能避免反引号。这更好:

result="$(echo ${callResult} | grep 'RESPONSE CODE' | cut -d':' -f 2)"

关于带有 grep 和 cut 的 bash 脚本 : command not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17433825/

相关文章:

linux - 当我启动 bash 脚本时,如何找到 Ubuntu 中的所有用户并通过参数显示用户信息?

bash - 在 bash 中使用 .vimrc 设置

bash - 调整图片大小的shell脚本

bash - 从命令行将文本转换为 7 位 ASCII

bash - 将 bash 变量分配为两个单词字符串的列表

bash - 根据多列匹配汇总一列(部分部分)

linux - 使用 shell 脚本将 FTP 输出重定向到我的 linux 机器上的文件

bash - 在 bash 中粘贴带有扩展分隔符的文件

linux - 通过 bash 中的列表传递带空格的参数

linux - File(1) 将空字节分类为数据文件