开发者

Sed does not work in expect

开发者 https://www.devze.com 2022-12-23 00:20 出处:网络
I made this bash one-liner which I use to list Weblogic instances running along with their full paths.This works well when I run it from the shell.

I made this bash one-liner which I use to list Weblogic instances running along with their full paths.This works well when I run it from the shell.

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk开发者_如何学Python -F'=' '{print $2}' | sed 's/weblogic.policy//' | sed 's/security\///' | sort

I tried to incorporate this in an expect script

send "echo Weblogic Processes: ; /usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print \$2}' | sed 's/weblogic.policy//' | sed 's/security\///' | sort ; echo ; echo\r"

but I got this error sed: -e expression #1, char 13: unknown option to `s'

Please help


Probably the \ character in

sed 's/security\///'

needs an extra escape in expect context, e.g.

send "echo Weblogic Processes: ; /usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print \$2}' | sed 's/weblogic.policy//' | sed 's/security\\///' | sort ; echo ; echo\r"


Without careful counting or testing, I'd try adding another \ after "security\", or possibly deleting the existing one.

Also, you can combine the two seds into one: sed -e 's/weblogic.policy//' -e 's/security\///'


Use Tcl's {} operator instead of double quotes around the string you want to send. The {} syntax in Tcl is equivalent to single quote in bash, meaning "literal string, do not interpret its contents".

Inside the {} put exactly what you want Tcl/Expect to send to the subprocess, character for character, no extra quoting required.


I think this is too complex to send over to a remote host. Instead, put the commands in a small shell script and execute that. This way, you won't run into trouble because of quote expansion rules, escaping, etc.

Moreover, you should use ssh instead of expect to run scripts. expect is for running interactive commands like ftp which don't have suitable scripting abilities.


you can try removing the single quotes and run the command again.

send "....... sed s/weblogic.policy// | sed s/security\/// ..."

Its most probably quoting issues. If its not ok, try the suggestion by hlovdal

meanwhile, some of your longish commands can be combined

/usr/ucb/ps auwwx |grep weblogic| tr ' ' '\n'|awk '/security.policy/&&/domain/{gsub("weblogic.policy|security","",$2);print $2}|sort 
0

精彩评论

暂无评论...
验证码 换一张
取 消