LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2012, 10:58 AM   #1
samadm
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
Unhappy Get values from property file using bash


Hi guys,

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,

hosts=<user1>@<server1>,<user2>@<server2>,<user3>@<server3>......

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
 
Old 10-06-2012, 11:39 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
#!/bin/bash

IFS=,
items=( $( cat input.txt | sed 's/hosts=//' ) )
for item in "${items[@]}"
do
    server=${item#*@}
    echo "Server: $server"
done
 
1 members found this post helpful.
Old 10-06-2012, 12:47 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
1 members found this post helpful.
Old 10-06-2012, 01:12 PM   #4
samadm
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you so much catkin and grail.... both of your codes worked for me!
 
Old 10-06-2012, 01:27 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Here's the mechanics of a similar process I am using to write my own "mass scp" script.
Code:
CONF=/home/jj/.ssh/c9
SECDIR=/home/jj/Documents/cirrhus9/Forensics
HOSTCONF="$CONF"/$1.hosts
HOSTSOURCE=$(sed '/^#/d' $HOSTCONF)
c9GROUP=$1
HOSTFILE=$2
...
dowork()
{
echo "$HOSTSOURCE" | while read line
do
    c9IP=$(echo "$line" | cut -d@ -f2 | sed -e 's/"//')
    c9KEY=$(echo "$line" | awk '{print $4}')
    echo "scp -i $c9KEY root@$c9IP:$HOSTFILE $SECDIR/$1" # /$(date '+%m-%d-%Y')/$c9HOST/$2"
done
}
Hope it helps.

Some resources:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://www.gnu.org/software/bash/man...ode/index.html
http://www.grymoire.com/Unix/Sh.html
http://tldp.org/LDP/abs/abs-guide.pdf
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://rute.2038bug.com/index.html.gz
http://bashscripts.org/forum/
 
Old 10-06-2012, 03:10 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by catkin View Post
Code:
#!/bin/bash

IFS=,
items=( $( cat input.txt | sed 's/hosts=//' ) )
for item in "${items[@]}"
do
    server=${item#*@}
    echo "Server: $server"
done
On further consideration the | sed 's/hosts=//' is not required.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Reading a property file through shell script??? sukhdip Linux - Newbie 8 10-05-2011 09:50 AM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 08:11 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM
How do you change the file Property from Read Only to .... Charlie Chan Linux - Newbie 1 05-16-2004 12:52 PM
vsFTPd file property options zix99 Linux - Networking 3 10-14-2003 10:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration