Using 2 arrays in a For loop with different number setS?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using 2 arrays in a For loop with different number setS?
Ok so i was just trying this out, but its obviously not working -.-
how can i add another letter or number for count?
like i want to have another number increase for the pcs arrays.... instead of i i want N
For example
Code:
for ((i = 0; n=0; i <${#server[@]} + n<${#pcs[@]}; i++ n++))
Not sure exactly what you are trying to accomplish. Maybe two loops? Or maybe a while loop instead of a for with whatever variables you want use to increment and test for.
Code:
for ((i=0; i<${#server[@]}; i++)) do;
for ((n=0; n<${#pcs[@]}; n++)) do;
...
done
done
Ok so i was just trying this out, but its obviously not working -.-
how can i add another letter or number for count?
like i want to have another number increase for the pcs arrays.... instead of i i want N
For example
Code:
for ((i = 0; n=0; i <${#server[@]} + n<${#pcs[@]}; i++ n++))
Something like that
Not sure what the "+" is doing there. May be you want "&&"? Also you need to use "," if you want multiple statements in control section of the for loop.
Eg.
Code:
#!/bin/bash
server=( "sa" "sb" "sc")
pcs=( "pa" "pb" )
for ((i = 0, n=0; i <${#server[@]} && n<${#pcs[@]}; i++, n++)) {
echo "----"
echo "i = $i"
echo "n = $n"
echo "server is ${server[i]}"
echo "pc is ${server[n]}"
}
Not sure exactly what you are trying to accomplish. Maybe two loops? Or maybe a while loop instead of a for with whatever variables you want use to increment and test for.
Code:
for ((i=0; i<${#server[@]}; i++)) do;
for ((n=0; n<${#pcs[@]}; n++) do;
...
done
done
Ahh, nested loops, maybe that is what OP is actually asking!
Ahh, nested loops, maybe that is what OP is actually asking!
Ya thats literally perfect. Now i just got to parallel process these variables. I have been trying for past day to get it and i just seem to be getting my ass kicked XD
Ya thats literally perfect. Now i just got to parallel process these variables. I have been trying for past day to get it and i just seem to be getting my ass kicked XD
Hmm.. you know that there is a whole bunch of existing well established solutions for cluster monitoring?
That i do know, but thats just a small part of the script. It does a bunch of other custom stuff that i need and it puts everything in 1 email every 24 hours so i can see all my client machines and servers. It makes life much easier, well thats the goal anyway but i seem to be stuck on parallel processing. XD
Anyone got a solution for the variables in parallel?
I would run one SSH session per host and inside that do each command so that the end result is captured outside of SSH as an array, perhaps simulate an array of arrays even. Then you could just append & to the end of each such line for parallel action. wait in bash is a little special and might help. Though GNU parellel might be of use, too, if you have to set a maximum limit for the number of parallel processes.
I would run one SSH session per host and inside that do each command so that the end result is captured outside of SSH as an array, perhaps simulate an array of arrays even. Then you could just append & to the end of each such line for parallel action. wait in bash is a little special and might help. Though GNU parellel might be of use, too, if you have to set a maximum limit for the number of parallel processes.
I have been trying just about everything and i cant seem to get it to go. But since you said ssh, i think that sounds like the issue.
You have a recommendation on how i might do this? or an example, I am still a coder n00b for the most part.
What you guys could do in a day still probably takes me a week or 2. Still learning how to get properly setup.
Last edited by finalturismo; 09-09-2022 at 03:07 AM.
Hmm.. you know that there is a whole bunch of existing well established solutions for cluster monitoring?
That i do know, but thats just a small part of the script. It does a bunch of other custom stuff that i need and it puts everything in 1 email every 24 hours so i can see all my client machines and servers. It makes life much easier, well thats the goal anyway but i seem to be stuck on parallel processing. XD
Quote:
Originally Posted by finalturismo
I have been trying just about everything and i cant seem to get it to go. But since you said ssh, i think that sounds like the issue.
You have a recommendation on how i might do this? or an example, I am still a coder n00b for the most part.
What you guys could do in a day still probably takes me a week or 2. Still learning how to get properly setup.
!!!
The existing well established tools evo2 is referring to almost certainly already do everything you need them to, but can also be configured/scripted for any additional functionality that they don't do out of the box, they already exist, are way more efficient, effective, stable, and contain far far fewer bugs than what you are currently struggling to write.
The most important thing to learn is using the right tools for the job.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.