问题描述
我试图解析一个VNC服务器启动事件的输出,并在分析了命令替换使用的sed遇到了问题。具体地,远程VNC服务器的方式开始如以下
I am attempting to parse the output of a VNC server startup event and have run into a problem in parsing using sed in a command substitution. Specifically, the remote VNC server is started in a manner such as the following:
address1="[email protected]"
VNCServerResponse="$(ssh "${address1}" 'vncserver' 2>&1)"
在此启动事件产生的标准误差输出则为了提取服务器和显示信息进行解析。此时变量 VNCServerResponse
的内容有以下一些诸如:
The standard error output produced in this startup event is then to be parsed in order to extract the server and display information. At this point the content of the variable VNCServerResponse
is something such as the following:
New 'lxplus0186.cern.ch:1 (user1)' desktop is lxplus0186.cern.ch:1
Starting applications specified in /afs/cern.ch/user/u/user1/.vnc/xstartup
Log file is /afs/cern.ch/user/u/user1/.vnc/lxplus0186.cern.ch:1.log
此输出可通过以下方式以提取服务器和显示信息进行解析:
This output can be parsed in the following way in order to extract the server and display information:
echo "${VNCServerResponse}" | sed '/New.*desktop.*is/!d' | awk -F" desktop is " '{print $2}'
其结果是一些诸如以下:
The result is something such as the following:
lxplus0186.cern.ch:1
我想要做的就是在命令替换类似以下内容使用解析:
What I want to do is use this parsing in a command substitution something like the following:
VNCServerAndDisplayNumber="$(echo "${VNCServerResponse}" | sed '/New.*desktop.*is/!d' | awk -F" desktop is " '{print $2}')"
在试图做到这一点,我是$ P $,出现以下错误psented:
On attempting to do this, I am presented with the following error:
bash: !d': event not found
我不知道如何解决这个问题。这似乎是在sed在命令置换正在使用的方式有问题。我想AP preciate指导。
I am not sure how to address this. It appears to be a problem in the way sed is being used in the command substitution. I would appreciate guidance.
推荐答案
的问题是,双引号内的bash试图展开 D!
将它传递给前子shell。你可以解决这个问题通过去除双引号,但我也想提出一个简化,将脚本:
The problem is that within double quotes, bash is trying to expand !d
before passing it to the subshell. You can get around this problem by removing the double quotes but I would also propose a simplification to your script:
VNCServerAndDisplayNumber=$(echo "$VNCServerResponse" | awk '/desktop/ {print $NF}')
此简单地打印在包含字行的最后一个字段桌面。
This simply prints the last field on the line containing the word "desktop".
在一个较新的bash中,你可以使用一个herestring,而不是管道的回声
:
On a newer bash, you can use a herestring rather than piping an echo
:
VNCServerAndDisplayNumber=$(awk '/desktop/ {print $NF}' <<<"$VNCServerResponse")
这篇关于如何解决错误&QUOT;庆典!:D“:事件未找到&QUOT;在bash命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!