INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Filling an array not all at once

Filling an array not all at once

(OP)
Hello everyone,

using ksh so far I know that if I would like to fill an Array I can e.G. use something like this:

CODE

set -A arrayname $(cat file)
set -A arrayname $(some command) 

But what if I do not wish to fill the Array all at once ?

Let's say I have a list of Servers. Now I ping each one and only if the ping fails the Name of the Server shall be inserted into an Array.

What's an elegant way of doing it ?

Regards,
Thomas

RE: Filling an array not all at once

Hi

Not sure if I understand your intentions correctly, as my solution has nothing to do with your posted code.
This is how I would solve such task :

CODE --> Ksh 93

while read ip; do ping -c 3 -w 6 "$ip" || arrayname+=( "$ip" ); done < ip.txt 

Feherke.
feherke.ga

RE: Filling an array not all at once

(OP)
Hi feherke,

sorry, here's a little bit more Detail:

I'm using the following command to generate a list (Array) of remote Printers:

CODE

set -A prtlst $(enq -isWA | awk '{print $1}' | grep -v " " | grep -v QUEUED | grep -v Queue | grep -v '\-----') 

Next Comes the ping of each Print Server:

CODE

numbprt=$(echo ${prtlst[*]} | wc -w)

counter=0

while [ $counter -lt $numbprt ];
do
ping -c 4 ${prtlst[$counter]} > /dev/null 2> /dev/null
if [ $? -eq 0 ];
then
echo "${prtlst[$counter]} is reachable"
else
echo "${prtlst[$counter]} is NOT reachable"
fi
counter=`expr $counter + 1`
done 

So what I'd like to do now is to create and to append to an additional Array if a Printer of $prtlst is not responding to the ping.

I don't like using external files like this:

CODE

echo ${prtlst[$counter]} >> unresponsive_prt.txt 

Regards,
Thomas

RE: Filling an array not all at once

Hi

Oops. When I read that "I have a list of Servers" for some reason I had the feeling you are talking about a file. I agree, neither I like to use temporary files when not necessary.

Anyway, to loop over the prtlst array and add each unresponsive one to a separate array, the basics are the same :

CODE --> Ksh 93

set -A prtlst $(enq -isWA | awk '{print $1}' | grep -v " " | grep -v QUEUED | grep -v Queue | grep -v '\-----') 
set -A unresponsive

for server in "${prtlst[@]}"
do
    ping -c 4 "$server" > /dev/null 2> /dev/null
    if [ $? -eq 0 ]
    then
        echo "$server is reachable"
    else
        echo "$server is NOT reachable"
        unresponsive+= ( "$server" )
    fi
done

echo "From ${#prtlst[@]} servers the following ${#unresponsive[@]} are unresponsive : ${unresponsive[@]}" 
Warning ! The above code was tested only as separate pieces.

Note that if your ping supports some kind of timeout option, I suggest to use it. ( If not available, the GNU coreutils package contains a timeout tool to run commands only for certain time, but I am afraid that will not be available either on your system. )

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close