LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get values from property file using bash (https://www.linuxquestions.org/questions/programming-9/get-values-from-property-file-using-bash-4175430850/)

samadm 10-06-2012 10:58 AM

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

catkin 10-06-2012 11:39 AM

Code:

#!/bin/bash

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


grail 10-06-2012 12:47 PM

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

samadm 10-06-2012 01:12 PM

Thank you so much catkin and grail.... both of your codes worked for me!

Habitual 10-06-2012 01:27 PM

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/

catkin 10-06-2012 03:10 PM

Quote:

Originally Posted by catkin (Post 4798880)
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.


All times are GMT -5. The time now is 03:24 PM.