在ssh中使用管道变量执行egrep命令没有这样的文件或目录错误

时间:2022-11-05 20:36:50

Ok, here I'm again, struggling with ssh. I'm trying to retrieve some data from remote log file based on tokens. I'm trying to pass multiple tokens in egrep command via ssh:

好的,我在这里,再次与ssh挣扎。我正在尝试基于令牌从远程日志文件中检索一些数据。我试图通过ssh在egrep命令中传递多个令牌:

IFS=$'\n'
commentsArray=($(ssh $sourceUser@$sourceHost "$(egrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log)"))
echo ${commentsArray[0]}
echo ${commentsArray[1]}
commax=${#commentsArray[@]}
echo $commax

where $v is something like below but it's length is dynamic. Meaning it can have many file names seperated by pipe.

其中$ v类似于下面,但它的长度是动态的。这意味着它可以通过管道分隔许多文件名。

UserComments/propagateBundle-2013-10-22--07:05:37.jar|UserComments/propagateBundle-2013-10-22--07:03:57.jar

The output which I get is:

我得到的输出是:

oracle@172.18.12.42's password:
bash: UserComments/propagateBundle-2013-10-22--07:03:57.jar/New: No such file or directory
bash: line 1: UserComments/propagateBundle-2013-10-22--07:05:37.jar/nouserinput: No such file or directory


0

Thing worth noting is that my log file data has spaces in it. So, in the code piece I've given, the actual comments which I want to extract start after the jar file name like : UserComments/propagateBundle-2013-10-22--07:03:57.jar/

值得注意的是我的日志文件数据中有空格。所以,在我给出的代码片段中,我想要提取的实际注释在jar文件名之后开始,如:UserComments / propagateBundle-2013-10-22--07:03:57.jar /

The actual comments are 'New Life Starts here' but the logs show that we are actually getting it till 'New' and then it breaks at space. I tried giving IFS but of no use. Probably I need to give it on remote but I don't know how should I do that. Any help?

实际的评论是“新生活从这里开始”,但是日志显示我们实际上已经到了'新'然后它在太空中断了。我试过给IFS但没用。可能我需要远程提供,但我不知道该怎么做。有帮助吗?

2 个解决方案

#1


1  

Your command is trying to run the egrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log on the local machine, and pass the result of that as the command to run via SSH.

您的命令正在尝试在本地计算机上运行egrep“$ v”/$INSTALL_DIR/$PROP_BUNDLE.log,并将其结果作为通过SSH运行的命令传递。

I suspect that you meant for that command to be run on the remote machine. Remove the inner $() to get that to happen (and fix the quoting):

我怀疑你的意思是在远程机器上运行该命令。删除内部$()以使其发生(并修复引用):

commentsArray=($(ssh $sourceUser@$sourceHost "egrep '$v' '/$INSTALL_DIR/$PROP_BUNDLE.log'"))

#2


0  

You should use fgrep to avoid regex special interpretation from your input:

您应该使用fgrep来避免输入的正则表达式特殊解释:

commentsArray=($(ssh $sourceUser@$sourceHost "$(fgrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log)"))

#1


1  

Your command is trying to run the egrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log on the local machine, and pass the result of that as the command to run via SSH.

您的命令正在尝试在本地计算机上运行egrep“$ v”/$INSTALL_DIR/$PROP_BUNDLE.log,并将其结果作为通过SSH运行的命令传递。

I suspect that you meant for that command to be run on the remote machine. Remove the inner $() to get that to happen (and fix the quoting):

我怀疑你的意思是在远程机器上运行该命令。删除内部$()以使其发生(并修复引用):

commentsArray=($(ssh $sourceUser@$sourceHost "egrep '$v' '/$INSTALL_DIR/$PROP_BUNDLE.log'"))

#2


0  

You should use fgrep to avoid regex special interpretation from your input:

您应该使用fgrep来避免输入的正则表达式特殊解释:

commentsArray=($(ssh $sourceUser@$sourceHost "$(fgrep "$v" /$INSTALL_DIR/$PROP_BUNDLE.log)"))