-1

This is my file (xyz.properties)

abcd.123=localhost:8180

Now I need this IP address in my shell script

vi create.sh
#!/bin/bash

How do I call abcd.123 from properties file to this shell script

!bin/bash

source = /xyz.properties

${abcd_123}

${"abcd_123"}

${abcd.123}

nothing works

this way is not working and my main idea is to use the variable everywhere BTW i cannot use abcd_123 in my properties file as there are so many dependencies on that variable

Community
  • 1
  • 1
vk927
  • 1
  • 2
  • You don't; you use a language that has (or in which you can write) a proper parser for this input. – chepner Mar 11 '16 at 21:39

2 Answers2

1

You can replace the dots and source the modified content:

$ source <(sed 's@\(.*\)\.\(.*\)=@\1_\2=@' xyz.properties)
$ echo $abcd_123
localhost:8180
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

in your bash script you need to "source" your properties file (you can use the "source" or "." [a dot]):

#!/bin/bash
source yourfile.properties

Edited. Change your names to use an underscore instead, then access them like so:

${"abcd_123"}
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • `name`: A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier. – Diego Torres Milano Mar 11 '16 at 19:44
  • Indeed, see my comments - unsure what shell OP is using...! – Stuart Mar 11 '16 at 19:46
  • in fact, OP mentions bash in the title, so my bad there. OP: remove the dot, and source the file as mentioned, you access your variables in the same fashion. – Stuart Mar 11 '16 at 19:49
  • I did as you said,but not working – vk927 Mar 11 '16 at 19:51
  • in server.properties zookeeper.connect=172.30.0.243:2181 – vk927 Mar 11 '16 at 19:51
  • #!/bin/bash source= ~/kafka_2.11-0.9.0.1/config/server.properties echo ${"zookeeper.connect"} – vk927 Mar 11 '16 at 19:52
  • /home/ec2-user/kafka_2.11-0.9.0.1/config/server.properties: line 117: zookeeper.connect=172.30.0.243:2181: command not found /home/ec2-user/kafka_2.11-0.9.0.1/config/server.properties: line 120: zookeeper.connection.timeout.ms=6000: command not found ./create_topic.sh: line 3: ${"zookeeper.connect"}: bad substitution – vk927 Mar 11 '16 at 19:54
  • if you're in bash land, remove the dots from your names. maybe change properties file to be: zookeeper_connect=1.2.3.4 and read like: ${zookeeper_connect} – Stuart Mar 11 '16 at 19:58