Reputation: 1
HI I`m trying to execute something like this:
ssh USER@SERVER 'command=`ps -aux | grep xyz | grep -v grep | awk '{ print $11}'` && echo $command'
and I get
awk: cmd. line:1: {
awk: cmd. line:1: ^ unexpected newline or end of string
Is there any way to get it working?
Please, help me! Thanks a lot!
Upvotes: 0
Views: 1079
Reputation: 267
There's a bunch that's confusing here. First: you can restructure your remote command entirely within awk:
awk 'BEGIN { cmd="ps aux"; while( (cmd | getline) >0) {if($0 ~ /xyz/) print $11}}'
If you happen to know which field you expect "xyz" to occur in, you can test that directly and eliminate your negative test (which I've omitted above). We can add that back if you insist:
awk 'BEGIN { cmd="ps xyz"; while( (cmd | getline) >0) {if($0 ~ /xyz/ && $0 !~ /awk/) print $11}}'
But say I'm interested in what user clamav
is up to:
awk 'BEGIN { cmd="ps aux"; while( (cmd | getline) >0) {if($1 ~ /clamav/) print $11}}'
Another issue is that you're defining and echoing an environment variable remotely, which introduces a bunch of complications. Why not exploit SendEnv
instead. Generally this is LANG LC_*
, but the latter is liberal as to what it allows:
export LC_command="awk 'BEGIN { cmd=\"ps aux\"; while( (cmd | getline) >0) {if($1 ~ /clamav/) print $11}}'"
ssh -o USER@HOST 'echo $LC_command'
Other options include providing a script or shell function which can be executed remotely, which avoids a lot of SSH & shell quote fumbling.
Any more clarification on what you're hoping to accomplish?
Upvotes: 0
Reputation: 113864
Try:
ssh USER@SERVER 'ps -p "$(pgrep -d, xyz)" -o comm='
-p "$(pgrep -d, xyz)"
selects for commands whose names include the string xyz
.
-o comm=
selects the output format that you want.
Instead of the original command, try:
ssh USER@SERVER 'command=`ps -aux | grep xpdf | grep -v grep | awk '\''{ print $11}'\''` && echo $command'
Observe, that inside quoted command string, '
has been replaced with '\''
.
This is not one single-quoted string:
'command=`ps -aux | grep xyz | grep -v grep | awk '{ print $11}'` && echo $command'
It consists of the following:
A single quoted string:
'command=`ps -aux | grep xyz | grep -v grep | awk '
An unquoted string:
{ print $11}
A single-quoted string:
'` && echo $command'
There is no way to put single-quotes inside single quoted strings. One has to end the first single-quoted string with a closing single-quote, follow it with an escaped single-quote, and follow this with a single-quote to open a single-quoted string.
Upvotes: 3