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.
I am a newbie to bash programming. I am trying to run a 'for' loop by reading values from a property file. The values inside the file are similar to below,
I would like to run a for loop for all the servers (1,2,3..etc). For that, I need to get the list from the file first and this is where I am facing difficulty. This list wont be static and keeps changing. So, I need my script to get the server list whenever I run the script
My final goal is to do something like below:
for i in <get-server-list-from-file>
do
some commands
done
If you have some control over how the data is entered in the property file, namely single quotes around the items right of the equal sign, you could do the following:
Code:
$ cat property.file
hosts='<user1>@<server1>,<user2>@<server2>,<user3>@<server3>'
$ cat samadm.sh
#!/bin/bash
. property.file
IFS=',@'
set -- $hosts
for i
do
echo "$i"
done
Basically this now allows you to use the parameter values to refer to the user and server information, ie. $1 = <user1> and $2 = <server1>, etc
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.