LinuxQuestions.org
Review your favorite Linux distribution.
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 06-28-2013, 04:02 PM   #1
nitrohuffer2001
Member
 
Registered: Apr 2006
Posts: 165

Rep: Reputation: 19
Bash script - loop question


Hi Folks,

I have a loop that goes through an array and the output is funky.

sample:
array=( 19.239.211.30 )
for i in "${array[@]}"
do
echo $i
iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print '$i',$6}' >> $file
done

Output:
19.239.211.30
19.2390.2110.3 8746886

seems that when the $i after the awk is printed something happens to it.

Any ideas?

Thanks.
 
Old 06-28-2013, 04:44 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
When you use '$i" the shell is interpreting it as a literal $i and not the variable.

Try using "" instead of ''

--C
 
Old 06-28-2013, 04:48 PM   #3
nitrohuffer2001
Member
 
Registered: Apr 2006
Posts: 165

Original Poster
Rep: Reputation: 19
No Luck

Hi, thanks for the response.

When I try with "$i" i get this

array=( 19.239.211.30 )
for i in "${array[@]}"
do
echo $i
iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print "$i",$6}' >> $file
done


Output:
19.239.211.30

$i 9060072

so it looks like it prints what is in the " " exactly how it appears.

Thanks.
 
Old 06-28-2013, 06:56 PM   #4
nitrohuffer2001
Member
 
Registered: Apr 2006
Posts: 165

Original Poster
Rep: Reputation: 19
Solved

Answered my own question:

replace '$i' with ”‘${i}’” = win
 
1 members found this post helpful.
Old 06-29-2013, 02:42 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could negate the whole issue of guessing which quotes to use and pass the value to awk correctly:
Code:
iperf -c $i -P 10 -x CSV -f b -t 50 | awk -vip="$i" 'END{print ip,$6}' >> $file
Few things though:

1. I am guessing you will eventually fill the array as with one item the loop is pointless

2. If all you want to do is display the value, why do you need anymore than the simple echo you have already used?
 
1 members found this post helpful.
Old 07-01-2013, 12:21 PM   #6
nitrohuffer2001
Member
 
Registered: Apr 2006
Posts: 165

Original Poster
Rep: Reputation: 19
Hi,

Thanks for your reply.

Yes, the array will be filled up, I was just testing one for the sake of time. the end goal is to generate a list that looks like this:

19.232.211.30 8526965
19.232.235.30 8957593
19.232.243.30 8929368
19.232.219.30 11068447

Thanks again,
 
Old 07-03-2013, 12:30 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.


Instead of awk, I recommend cut, which is lighter.

Code:
iperf -c "$i" -P 10 -x CSV -f b -t 50 | cut -d ' ' -f "$i,6" >> "$file"
I'm guessing here that the output is space-delimited. Change the -d setting as necessary. (It usually helps to include a sample of the input text when you post, along with the desired output and the code you're using.)

Another option is to use the read built-in, and an array.

Code:
read -ra fields < <( iperf -c "$i" -P 10 -x CSV -f b -t 50 )
echo "${fields[i-1]} ${fields[5]}"
Remember that shell arrays are zero-indexed, so you have to subtract one.

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005/

How do I do string manipulations in bash?
http://mywiki.wooledge.org/BashFAQ/100

Last edited by David the H.; 07-03-2013 at 12:32 PM. Reason: added comment
 
Old 07-05-2013, 03:20 PM   #8
nitrohuffer2001
Member
 
Registered: Apr 2006
Posts: 165

Original Poster
Rep: Reputation: 19
Hi,

Thanks for the reply and the helpful advice. Next time I will send a more complete example with the desired output.

Again thanks for the help.
 
  


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] Friday BASH Script question - Check output during for loop Kustom42 Programming 6 05-05-2013 07:33 AM
Bash shell script with for loop question? beaven67 Programming 6 10-05-2012 09:57 AM
Bash Script Loop Question SoulShaker Linux - Server 5 06-17-2009 01:44 PM
Bash script Question while loop glennph93 Programming 6 05-25-2007 03:27 PM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM

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

All times are GMT -5. The time now is 06:07 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