[SOLVED] For loop with embedded "growing" variable
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I've been trying to grep for a value in numerous files with a for loop. What I want is for the result of each loop to be saved as a variable +1 like below:
A=$(0)
for i in `cat zone_files.txt`
do
echo ***$i***
databaseip[$A]="$(grep postgres_host settings/$i | awk '{print $2}')"
let "A++"
done
For some reason the IP addresses I am trying to save are not saving to variables:
$databaseip0
$databaseip1
$databaseip2
$databaseip3
Can anyone see what I am missing?
The Numberic value seems to work ok. if I add "echo $databaseip$A" I get 3 which is the correct number of the variable but it is missing the IP.
My zones_files.txt file just shows a list of 3 zones. the for loop looks for configuration files in those zones under "/settings/" and just greps for the database IP. Manually this works. But for some reason I can't get the IP's assigned to the variables numerically.
For some reason the IP addresses I am trying to save are not saving to variables:
$databaseip0
$databaseip1
$databaseip2
$databaseip3
seems to indicate that databaseip0, databaseip1, databaseip2... are separate variables...
=>So you need to choose what strategy you prefer.
If you want databaseip to be an array, you don't need to change anything but you will enumerate its data with:
Code:
echo "${databaseip[@]}"
So it's normal if databaseip0, databaseip1, databaseip2, databaseip3 don't exist, hence don't contain your data
For some reason the IP addresses I am trying to save are not saving to variables:
$databaseip0
$databaseip1
$databaseip2
$databaseip3
seems to indicate that databaseip0, databaseip1, databaseip2... are separate variables...
=>So you need to choose what strategy you prefer.
If you want databaseip to be an array, you don't need to change anything but you will enumerate its data with:
Code:
echo "${databaseip[@]}"
So it's normal if databaseip0, databaseip1, databaseip2, databaseip3 don't exist, hence don't contain your data
Btw, replace
Code:
A=$(0)
with
Code:
A=0
This works now thank you, I can see the values. I was using A=0 but in my messing about, was trying a few different options.
echo "${databaseip[@]}" is displaying what I need to see. What I would like to do is assign each IP to a variable like databaseip0 (for zone 0), databaseip1 (for zone 1) etc...
So that I can then populate another file with those IP's basically.
databaseip[@] shows me the list of IP's and appends them like this:
***qa-iaas-z1.yml***
echo "${databaseip[@]}" = IP0 IP1 IP2 IP3
It would seem you are not used to working with arrays. In bash you would recall each item at the position want, like so:
Code:
${databaseip[@]} -- all items in array (use double quotes if whitespace is to be preserved)
${databaseip[0]} -- refers to first items in array
${databaseip[1]} -- refers to second
...
As for your current code, it could be a deal simpler:
Code:
while read -r _ filename _
do
databaseip+=($(awk '/postgres_host/{print $2}' "settings/$filename"))
done<zone_files.txt
Maybe you are asking for separate variables databaseip0, databaseip1, databaseip2 because you are not aware that your databaseip array values can already be accessed independently.
If so, note that you can achieve what you need with what you already have ie databaseip.
Here are some ways you can access your data:
Code:
for elem in ${databaseip[@]}; do echo "$elem"; done;
Code:
for (( i=0; i<${#databaseip[@]}; i++ )); do echo "${databaseip[$i]}"; done;
EDIT: ok it seems you use a throwaway variable to ignore field 1 and field3+
Does your command work? My tests are not very successful but I don't know the files structure under /settings...
EDIT: ok it seems you use a throwaway variable to ignore field 1 and field3+
Does your command work? My tests are not very successful but I don't know the files structure under /settings...
I have not tested it, as I have no idea of file format, but assuming as you said that there are at least 2 fields, the logic should be sound
Let me know where you are having an error? (you can of course use set -xv to see what may be going wrong)
I have not tested it, as I have no idea of file format, but assuming as you said that there are at least 2 fields, the logic should be sound
Let me know where you are having an error? (you can of course use set -xv to see what may be going wrong)
I'm not the OP
I'm like you, I don't know how files under settings folder are decomposed...
@flukeyLinux: only you can test the command and see if it meets your need.
ok I ended up going with, creating a zones array and asking for a variable. so the script runs like ./script $environment. It then loops through each zone looking for the environment inputted and looks for the IP's I was referring to using jq.
I needed to append that zone data to another file (getting away from zones) so used an if statement to see if similar data already existed, if it did, exit if not, append.
if grep -q "environment-settings" settings/$env_name-$zone.yml;
then
echo -e "Environment-settings already exist for $env_name. Nothing to do"
echo "Exiting..."
exit 0
else
echo "Appending contents to settings/$env_name.yml:"
cat contents >> settings/$env_name.yml
rm contents
fi
Thought I would reply in case someone else was having a similar issue. Thanks for your help!
Last edited by flukeyLinux; 11-27-2018 at 11:40 AM.
Ok, glad everything's fine for you now, even if, as evoked by grail, your solution doesn't seem to be directly related to the original question asked...
Please use [CODE] tags and please mark your thread as [SOLVED].
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.