Reading values from config file in linux [duplicate] - linux

This question already has answers here:
Need bash shell script for reading name value pairs from a file
(8 answers)
Reading key/value parameters from a file into a shell script
(1 answer)
How do I grab an INI value within a shell script?
(32 answers)
*export* all variables from key=value file to shell
(1 answer)
Closed 5 years ago.
I am working in linux and have a config file with a lot of single lines formatted like this:
Variable1=Value1
Variable2=Value2
Variable3=Value3
I need something I can run on command line that will echo the value for the respective variable. I have been playing with sed all day, but having a heck of a time. I'm not sure if that's even the best way. Any help would be super.

$ cat a.sh
Variable1=Value1
Variable2=Value2
Variable3=Value3
$ source a.sh
$ echo "$Variable1"
Value1
Note, source will overwrite the value of Variable1 for the current shell.

Search for the variable name and the equal sign, remove them, and print the result.
$ sed -n '/^Variable1=/{s/^Variable1=//;p}' config.txt
Value1

Related

Add lines to file in linux [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
I want to add a line to a file maintaining the exact pattern
Line i want to add:
export PATH=$PATH:$JAVA_HOME/bin
I dont want to add the values of the variables to the file
What I did:
echo "export PATH=$PATH:$JAVA_PATH/bin" | sudo tee -a /home/admin/Vishal/test.sh
My Output:
Contains numerous paths instead of export PATH=$PATH:$JAVA_HOME/bin
The immediate problem is that you need single quotes instead of double. But really, you should not be editing your script file. Instead, make it accept a parameter which tells it whether or not to update the PATH.
case $1 in --update-path) PATH=$PATH:$JAVA_HOME/bin;; esac
If you run /home/admin/Vishal/test.sh --update-path it will add the Java directory; without the option, it won't.

Space between lines in shellscript being taken as a command [duplicate]

This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a bash script which is a follows
#!/bin/bash
#getting the environment variable from commandline
environment=$1
echo $environment
Now when I run the script with bash ./bashScript.sh Hello , I get the following errors on line
: command not found line 2
: command not found line 5
I see that both of these lines are space and bash script is thus giving me an error
To solve it I write my script as
#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment
But it looks kind of messy
Is there any other way to achieve this. Thanks for help in advance.
Your comment add the precision that your lines use DOS end of lines (\r\n) when you dump it with od -xc file. To avoid it, you should make sure that your editor uses Unix end of lines (\n).
To fix it on an existing text file, you can use tr:
tr -d '\r' < dos_file > unix_file

BASH script with a $ not saving correctly [duplicate]

This question already has answers here:
How to echo a variable containing an unescaped dollar sign in bash
(4 answers)
Closed 6 years ago.
I have a bash script, which I run doing the following sudo ./test
The bash script needs to create a repo, and save the following data in it.
So, this is the bash script:
#!/bin/bash
echo "Inputing data... "
echo "[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/" > /etc/yum.repos.d/mongodb-org.repo
However, because the releasever has a $ in front of it, the script save the text like this:
[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat//mongodb-org/3.2/x86_64/
Instead of like this:
[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat/$c/mongodb-org/3.2/x86_64/
Any idea how I can treat the $releasever as text rather than a variable?
I tried putting double quotes around it however that still does not work. I am new to bash scripting so any help appreciated.
Thanks!
Use single quotes to prevent variable expansion.
Using double quotes:
sh-4.1$ baseurl1="https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/"
sh-4.1$ echo $baseurl1
https://repo.mongodb.org/yum/redhat//mongodb-org/3.2/x86_64/
Using single quotes:
sh-4.1$ baseurl2='https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/'
sh-4.1$ echo $baseurl2
https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
Use the back-slash character to indicate that you don't want to call a bash variable, e.g.:
echo \$HELLO

How to call variable from properties file in shell scripting bash [duplicate]

This question already has answers here:
How do I grab an INI value within a shell script?
(32 answers)
Closed 6 years ago.
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
You can replace the dots and source the modified content:
$ source <(sed 's#\(.*\)\.\(.*\)=#\1_\2=#' xyz.properties)
$ echo $abcd_123
localhost:8180
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"}

Use return value of a shell command as a value in shell script? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capturing multiple line output to a bash variable
For example I want to run ls command and take the return list as a value kept in a array in shell script.
Something like
run
#ls
fileA
fileB
fileC
kept this return list in a variable that keeps a array
variable A = ["fileA","fileB","fileC"];
I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.
#!/bin/bash
variableA=$(ls)
echo $variableA
That should be your shell script assuming that you have bash
Then all you'd need to do is chmod +x shell_script to make it executable.
If you use ls > contents.file the result of ls is saved to a file called contents.file.
Remember, > rewrites the entire file while >> appends to the last line.
variableA=$(ls)
echo "$variableA"

Resources