[SOLVED] Shell scripting - for...in loop and variables question
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Shell scripting - for...in loop and variables question
I'm trying to re-write a script that we use to monitor some switches in our lab. The number is growing, and I need to start using a loop to check the different stacks.
My problem is that I cannot get the variable name to change and get the value written to the new variable dependent upon the for loop's iteration.
Here is one of the snippets I'm trying to re-script:
Code:
#!/bin/bash
#Get the temp of Stack 1
unitNum=1
for host in $iplist
do
stack1unit${unitNum}=$(snmpget ${host} -v 2c -c public .1.3.6.1.4.1.45.1.6.3.7.1.1.5.5.${unitNum}0.0 | awk -F":" '{print $2}')
let unitNum=$unitNum+1
done
As you can see, I'm trying to take the value returned from the snmpget and assign it to a variable whose name is determined by the values of the iteration of the loop.
It should create variables named stack1unit1, stack1unit2, stack1unit3, and stack1unit4 whose values are obtained from the snmpget output.
As you can probably tell, I can't get it to work (snmp stuff works fine-I need no help on that part - it is the variable creation from the loop iteration that I can't get working). I have tried eval with no luck, and can't wrap my head around how indirectly referencing the variable could help me.
Also, if we do manage to create these variables properly, will they still be available once the loop terminates?
I wound up using Juako's read command - it just seemed to be the easiest.
However, I must admit I do not understand why it works. I know the read command will read a line at a time from standard input, but its the "<<<" that's got me fooled (its not east to google those characters
Can you guys explain to me the mechanics behind the "<<<"?
I wound up using Juako's read command - it just seemed to be the easiest.
However, I must admit I do not understand why it works. I know the read command will read a line at a time from standard input, but its the "<<<" that's got me fooled (its not east to google those characters
Can you guys explain to me the mechanics behind the "<<<"?
Thanks!
"<<<" is "send a string to standard input", called "herestring".
For the record, $(..) operates as a subshell. There's no way to access any variables set inside it from the outside. You have to arrange the command so that the substitution itself outputs all the values you want, or so that the value you want is produced outside of the subshell.
But as grail mentioned first, and others have repeated, you really, Really, REALLY should be using an array for this, rather than trying to generate dynamic scalar variable names.
Just use stack1unit[${unitNum}]=, and you will have entries of ${stack1unit[0]} ${stack1unit[1]} ${stack1unit[2]}, etc. that you can use later on. If you need the indexes to be text strings instead, you can even use an associative array (assuming bash 4+)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.