1
#!/bin/bash range=$(seq -f "cen%04g" 1052 1099) range1=$(seq -f "rh%04g" 1052 1099) check () { for node1 in ${range};do ping -q -c 1 -w 3 -s 10 $node1 >/dev/null if [ $? -eq 0 ];then $i_up+=(${node1}",") else $i_down+=(${node1}",") fi done } for i in "site1" "site2" "site3" do declare -a $i_down declare -a $i_up check & done 
  1. I'm not sure how can I name the array based on for loop input so that it can run sites(site1,site2,site3) in parallel and get the array input in separate for each site. Else running each will take longer time.
  2. I have range on series cen(1052 to 1099) and if its not found there, I need to check with different series(rh) with same numbers. eg: cen1052 isn't there it needs to check rh1052. how can do that.
3
  • If not a duplicate, heavily related to unix.stackexchange.com/questions/596343/…CommentedJul 3, 2020 at 11:30
  • 1
    for Q1 : you can fork new shell for all 3 sites, but you'll be forking array as well and won't be able to merge them. why not use temporary files site$i_down.txt and site$i_up.txt
    – Archemar
    CommentedJul 3, 2020 at 11:35
  • This sounds like a case for variable indirection (see this answer), although I am not sure if it works for non-scalar variables, too ...
    – AdminBee
    CommentedJul 3, 2020 at 12:00

1 Answer 1

0

I think you are looking for parset:

checkhost() { piing() { ping -q -c 1 -w 3 -s 10 "$1" >/dev/null } if piing "cen$1"; then echo "+cen$1" else if piing "rh$1"; then echo "+rh$1" else if piing "deb$1"; then echo "+deb$1" else echo "-cen$1" fi fi fi } export -f checkhost parset updown -j0 checkhost ::: {1052..1099} echo "${updown[@]}" 

It is unclear to me how the you need the sites to work. If, say, it was simply a domain name to append, you can do:

parset updown -j0 checkhost {1}.{2} ::: {1052..1099} ::: site1.com site2.com site3.com 

parset is part of GNU Parallel. After installing GNU Parallel you need to activate parset by:

  • (permanently) env_parallel --install followed by starting a new shell
  • (this session only) . $(which env_parallel.bash)

If this is not what you are looking for, I suggest you make a full example that works, but does so in serial.

1
  • sorry for the confusion. What the machines I'm looking may get new distributions(redhat,centos,debian etc). but their serial number will be intact.eg: machine on centos have a name "cen1052" will be 'rh1052' when it comes with redhat.So when I check I need to search cen1052 and if it's not there then rh1052 and if it's down then I can claim as cen1052 is down else it should say either cen1052 is up or rh1052. If cen1052 is available no need to look for rh1052 and if cen1052 is not available and i can find it with rh1052, then I'm good to move to next one which is cen1053.hope that make sense.
    – ARthur
    CommentedJul 6, 2020 at 14:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.