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-25-2013, 08:34 AM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Problem printing column on remote host via script using awk


Hello everyone,

Ultimately what I'm trying to do is run ifconfig to print out the physical hardware, then grab the MAC address and put it into a file in /tmp (So later in the script I can stick the MAC into /etc/sysconfig/network-scripts/ifcfg-eth0, eth1 and eth2).

Code:
#!/bin/sh
ssh root@10.201.7.183 "\
ifconfig eth0 > /tmp/eth0file; \
ifconfig eth1 > /tmp/eth1file; \
ifconfig eth2 > /dev/null 2>&1 > /tmp/eth2file; \
awk '{print \$5}' /tmp/eth0file | head -1 > /tmp/eth0file; \
awk '{print \$5}' /tmp/eth1file | head -1 > /tmp/eth1file; \
awk '{print \$5}' /tmp/eth2file | head -1 > /tmp/eth2file"
In the above method, the first part works (Sending ifconfig to /tmp/ethfile. But when I try to separate the MAC from the rest of the ifconfig information I keep ending up with an empty file.

The funny thing is when I log on to the remote host and do this from the local command line it works fine. I think awk is causing the problem. I've had problems with using awk remotely this way before. But I could be wrong.

I wonder if anyone can recommend an alternative to awk in this case. Or perhaps my entire method for putting the MAC hardware into the eth files can be done better. Thanks!
 
Old 10-25-2013, 09:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You mention that above works locally, which I doubt very much.

This construct will not work, you'll end up with an empty file:
Code:
awk '{ print $5 }' /tmp/eth0file | head -1 > /tmp/eth0file
The reason for this is that bash will look at the whole command, sees that the output is redirected to a file called eth0file and creates this file (now empty). Then the awk part is executed, which uses the same file as input, but it is now empty.

As a rule: Never use the same file name as input and output.

A possible solution for your problem:
Code:
#/bin/bash
ssh root@10.201.7.183 "\
ifconfig eth0 | awk '/eth0/ { print \$5 }' > /tmp/eth0file ; \
ifconfig eth1 | awk '/eth1/ { print \$5 }' > /tmp/eth1file ; \
ifconfig eth2 | awk '/eth2/ { print \$5 }' > /tmp/eth2file"

Last edited by druuna; 10-25-2013 at 09:02 AM.
 
1 members found this post helpful.
Old 10-25-2013, 10:00 AM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
You mention that above works locally, which I doubt very much.

This construct will not work, you'll end up with an empty file:
Code:
awk '{ print $5 }' /tmp/eth0file | head -1 > /tmp/eth0file
The reason for this is that bash will look at the whole command, sees that the output is redirected to a file called eth0file and creates this file (now empty). Then the awk part is executed, which uses the same file as input, but it is now empty.

As a rule: Never use the same file name as input and output.

A possible solution for your problem:
Code:
#/bin/bash
ssh root@10.201.7.183 "\
ifconfig eth0 | awk '/eth0/ { print \$5 }' > /tmp/eth0file ; \
ifconfig eth1 | awk '/eth1/ { print \$5 }' > /tmp/eth1file ; \
ifconfig eth2 | awk '/eth2/ { print \$5 }' > /tmp/eth2file"
Thank you druuna. This was a much better solution and it works great.

Also, thanks for the new rule I'll add to my best practices list
 
  


Reply



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 fill column from previuos line column akeka Programming 4 01-30-2013 07:16 PM
[SOLVED] Why is my awk script producing extra spacing between the first and second column? mystupidquestion Programming 2 10-09-2011 12:51 PM
[SOLVED] Problem in using AWK to modify column Rode Linux - Newbie 11 07-30-2011 09:13 PM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM

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

All times are GMT -5. The time now is 07:27 PM.

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