Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling an array not all at once

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
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
 
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:
[b]while read[/b] ip[teal];[/teal] [b]do[/b] ping -c [purple]3[/purple] -w [purple]6[/purple] [i][green]"$ip"[/green][/i] [teal]||[/teal] arrayname[teal]+=([/teal] [i][green]"$ip"[/green][/i] [teal]);[/teal] [b]done[/b] [teal]<[/teal] ip[teal].[/teal]txt


Feherke.
feherke.ga
 
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
 
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:
[b]set[/b] -A prtlst [navy]$([/navy]enq -isWA [teal]|[/teal] awk [i][green]'{print $1}'[/green][/i] [teal]|[/teal] grep -v [i][green]" "[/green][/i] [teal]|[/teal] grep -v QUEUED [teal]|[/teal] grep -v Queue [teal]|[/teal] grep -v [i][green]'[/green][/i][lime]\-[/lime][i][green]----'[/green][/i][teal])[/teal] 
[b]set[/b] -A unresponsive

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

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

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

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top