setting a variable, using SSH and awk?

hi there

I am trying to get a value from a remote machine into a local variable. To get this value i want to use awk but im having trouble getting it to run, am i escaping in the right places here and using the right quotes (i must have tried a million combinations :()

# VAR=`ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk {'print \$1'}"`
Sun Microsystems

As you can see, it returns "Sun Microsystems" where, I expected it to return "Sun". It is ignoring my AWK, or at least ignoring the $1 within it

If i run it straight, without putting it into a variable, it works fine (i.e by removing the VAR=` from the front and the ` from the end)

like this

# ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk {'print \$1'}"
Sun

Could anybody help me out with this?

Cheers

VAR=`ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk '{print \$1}' "`

But you should be able to do it all with ONLY one awk - no need for grep/head/sed/etc...
What's the sample output of 'dmidecode'?

Try to simplify the command first, see if that helps.

VAR=$(ssh server1 "dmidecode | awk '/Manufacturer:/ { print \$2; exit }'")

Hi thanks for the reply, i certianly looks neater than my code :b:.

---------- Post updated at 09:52 PM ---------- Previous update was at 09:33 PM ----------

I wonder if i could ask a question about the simplification you have done above (which by the way is fab). I have a similar piece of code that uses sed, head, grep and culminates in an awk if statement, is there also a way of simplifying this or would i need to pipe it to another awk statement ?

I am really interested in how you are doing this ? i didn't realise it could be trimmed down this much

this is my current code (which i need to variabize as per your solution above)

VAR=`ssh server1 "/usr/platform/\`uname -i\`/sbin/prtdiag|head -1|sed s/.\*Sun\" \"//|nawk '{ if (\$3 == \"M
2\") { print \$2\$3; } else { print \$2; } }'"`

can this go into one awk command? surely not

Yes it can.

Like before. head -1 = awk exit after first line and the sed like the awk pattern:

(untested - but generally correct!...)

VAR=$(ssh server1 "/usr/platform/\$(uname -i)/sbin/prtdiag | nawk '/.*Sun/ { if (\$3 == \"M2\") print \$2\$3 else print \$2; exit }'")

The "if" part could be simplified a little further, perhaps.

thats perfect, thankyou .... I had to put a few curlies around the if statement to get it working but thank you very much for your help :slight_smile: