views:

238

answers:

5

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

A: 

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 
ghostdog74
I guess there is something wrong with the syntax in your command.It dint work out
Sharjeel Sayed
+2  A: 

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"
hlovdal
The same is probably true for all other backslashes in the string.
Aaron Digulla
@Aaron, no just this one is problematic. All the others are escaping something special to expect. That backslash needs to be protected to pass to sed.
glenn jackman
A: 

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.

Aaron Digulla
I need expect since I have to run this on multiple servers remotely and I cannot use OpenSSH Public Key in my setup.
Sharjeel Sayed
+1  A: 

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

mpez0
A: 

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.

joefis