LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2011, 01:58 PM   #1
Linville79
Member
 
Registered: Nov 2006
Location: Indiana, U.S.A.
Distribution: Red Hat Enterprise Linux, Fedora, CentOS
Posts: 134

Rep: Reputation: 16
Exclamation 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!
 
Old 10-06-2011, 06:38 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
$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
 
  


Reply

Tags
awk stdin syntax, bash scripting, pipe, print, variables



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk achieve "do not print" cristalp Programming 9 09-12-2011 06:55 AM
[SOLVED] How can I pass variable to "awk" and use print ? 915086731 Linux - Software 11 08-11-2011 09:35 PM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
How do you do ps ax | grep mozilla | awk '{print "kill " $1}' pppaaarrrkkk Programming 4 03-19-2008 07:17 PM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration