Sed does not work in expect

2019-09-05 07:53发布

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

5条回答
Ridiculous、
2楼-- · 2019-09-05 08:24

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 
查看更多
我命由我不由天
3楼-- · 2019-09-05 08:32

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

查看更多
Animai°情兽
4楼-- · 2019-09-05 08:33

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楼-- · 2019-09-05 08:40

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.

查看更多
一夜七次
6楼-- · 2019-09-05 08:44

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"
查看更多
登录 后发表回答