Sed does not work in expect

2019-09-05 08:30发布

问题:

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 -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

回答1:

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\///'



回答2:

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"


回答3:

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.



回答4:

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.



回答5:

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