LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with -> `su - vi-admin -c "vifp listservers | awk -F'\t' '{ print $1 }'"` (http://www.linuxquestions.org/questions/programming-9/need-help-with-%60su-vi-admin-c-vifp-listservers-%7C-awk-f%5Ct-%7B-print-%241-%7D-%60-906788/)

Linville79 10-06-2011 01: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:
Code:

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:

Code:

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.

Thanks!

grail 10-06-2011 06: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 12:27 PM.