-   Programming (
-   -   Need help with -> `su - vi-admin -c "vifp listservers | awk -F'\t' '{ print $1 }'"` (

Linville79 10-06-2011 02:58 PM

Need help with -> `su - vi-admin -c "vifp listservers | awk -F'\t' '{ print $1 }'"`
So I'm working on a script to collect information from various VMware servers. As part of that script, I'd like to output a header section to the log that lists each of the servers that will be queried.

This command works just fine if I run it from the bash CLI as vi-admin:

vifp listservers | awk -F'\t' '{ print $1 }
However, it breaks when I put it in a script that is run by root and try to su to vi-admin. Like this:


echo "Vmware:          "`su - vi-admin -c "vifp listservers | awk -F'\t' '{ print $1 }'"`
It throws an error about $1 being an unbound variable. I assume that this has something to do with awk not being run in a terminal session, causing $1 to not be stdin. I just can't work out the fix.

Any help would be much appreciated. I've spent most of the day working on this one echo statement.


grail 10-06-2011 07:38 PM

$1 in your awk script is a field variable local to awk.

$1 in your su script is now a bash variable as everything after -c is quoted so now it is interpreted prior to being
passed su.

Usual story on the command line ... escape escape escape

All times are GMT -5. The time now is 02:47 AM.