LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-29-2014, 10:31 AM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
Continuously append to same variable in for loop


Hello, I am trying to retrieve a list of ssg hosts on my network via ping. I want to put the list into a single variable if possible, but I can't seem to figure out how to do this.

What I used to do is put them into a file, but I wanted to see if there was a way to put them all into the same variable instead.

Here is the format that I am using to do this, which is incorrect, as each time the variable is going to become whatever host was last $i

Code:
[techs@ts-01 ~]$ for i in `cat /etc/storelist | awk '{print $1}'`
> do
> ping -c 1 $i-ssg > /dev/null 2>/dev/null
> pingRet=$?
> if [ $pingRet -eq 0 ]; then
> ssgHost=`echo $i`
> fi
> done
I'm hoping there is a command available that handles this sort of thing. I am killing my brain trying to figure out how to make it work by tossing variables around:

i.e.
Code:
oldVar=`echo $newVar`
newVar=`echo $i`
bothVar=`echo $oldVar && echo $newVar`
allVar=`echo $bothVar &&  # here I'm stuck in logic, intellect and patience
If this variable tossing can work, I cannot seem to see the solution. I was hoping to get some input on this.

Thanks in advance.
 
Old 08-29-2014, 10:48 AM   #2
westy500
LQ Newbie
 
Registered: Dec 2010
Location: UK
Distribution: Debian 7.5 (Wheezy)
Posts: 5

Rep: Reputation: 1
Variable arrays may help.

e.g.
Code:
a=0
for i in `cat /etc/storelist | awk '{print $1}'`
do
    ping -c 1 $i-ssg > /dev/null 2>/dev/null
    if [[ $? -eq 0 ]]
    then
        ssgHosts[$a]=$i
        let a=$a+1
    fi
done

echo ${ssgHosts[*]}
You can then use the array as you please...


Code:
for x in ${ssgHosts[*]}
do
    echo $x
done
Hope this helps.

Or, if you must use a single variable and not an array:

Code:
for i in `cat /etc/storelist | awk '{print $1}'`
do
    ping -c 1 $i-ssg > /dev/null 2>/dev/null
    if [[ $? -eq 0 ]]
    then
        ssgHosts="${ssgHosts} ${i}"
    fi
done

Last edited by westy500; 08-29-2014 at 10:52 AM. Reason: Added 3rd code block for using no array.
 
1 members found this post helpful.
Old 08-29-2014, 11:41 AM   #3
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by westy500 View Post
Variable arrays may help.

e.g.
Code:
a=0
for i in `cat /etc/storelist | awk '{print $1}'`
do
    ping -c 1 $i-ssg > /dev/null 2>/dev/null
    if [[ $? -eq 0 ]]
    then
        ssgHosts[$a]=$i
        let a=$a+1
    fi
done

echo ${ssgHosts[*]}
You can then use the array as you please...


Code:
for x in ${ssgHosts[*]}
do
    echo $x
done
Hope this helps.

Or, if you must use a single variable and not an array:

Code:
for i in `cat /etc/storelist | awk '{print $1}'`
do
    ping -c 1 $i-ssg > /dev/null 2>/dev/null
    if [[ $? -eq 0 ]]
    then
        ssgHosts="${ssgHosts} ${i}"
    fi
done
westy, thanks so much!

Not only does this solve several issues, current and future, but it heads me down a path for greater learning.

Have a great weekend.
 
Old 08-29-2014, 12:04 PM   #4
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
Do not read data using a for loop, especially as it already has white space in it seeing you are then using awk to retrieve data.

Second, awk is quite capable of reading from a file so cat is of zero help.

Lastly, if you simply wish to append new items to an array, increasing an index is not required unless it is not to be a uniform array.

So with that all said:
Code:
while read -r ip _
do
  if ping -c 1 "$i-ssg" &>/dev/null
  then
    ssgHosts+=( "$ip" )
  fi
done</etc/storelist
 
1 members found this post helpful.
  


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] append variable string in a specific line himu3118 Programming 4 06-08-2010 08:49 AM
Loop or check SQlite3 database continuously, when query found run script for first 3 Techno Guy Linux - Newbie 3 09-27-2009 07:46 PM
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM
Append variable string(s) at end of each line schaganti Linux - Newbie 2 10-19-2007 01:31 PM
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM

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

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