Add lines to file in linux [duplicate] - linux

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.

Related

Replace Tilde with $HOME in bash script [duplicate]

This question already has answers here:
Tilde expansion in quotes
(3 answers)
How to manually expand a special variable (ex: ~ tilde) in bash
(19 answers)
Closed 4 years ago.
I've been digging through the internet and I see examples of replacing $HOME with ~, but I'm trying to go the other way (e.g. - replace ~ with $HOME and currently if I try to run this:
if [[ $directory_name = *~* ]]; then
echo "${$directory_name/\~/$HOME}"
fi
to replace it, I get this error:
${$directory_name/\~/$HOME}: bad substitution
I have #!/bin/bash at the top of my script file and when I run it I've been using something like this:
sh test-script.sh
I'm also doing this in terminal on a Mac, so I'm not sure if that has anything to do with it.
Again...new to bash scripting so while this seems logical, I could be going about this all wrong and missing something. Thanks!

export variable in shell script not working [duplicate]

This question already has answers here:
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 4 years ago.
I am trying to export few variables and invoke spark-submit using a shell script. However the export variables are not printing as expected. Please find below the code and help me in resolving the issue.
export BASE_LOCATION=/home/hduser/hdm
export EXT_LIB=$BASE_LOCATION/extlib
echo $EXT_LIB
export EXT_LIB_JARS=$EXT_LIB/common-csv-1.1.jar:$EXT_LIB/spark-csv_2.10-1.5.0.jar
echo $EXT_LIB_JARS
I was expecting an output as /home/hduser/hdm/extlib from echo $EXT_LIB.
But I am receiving output as /libe/hduser/hdm
Also echo $EXT_LIB_JARS is not giving desired results.
Please help me to resolve this issue.
Regards,
Adarsh K S
I'm getting the output you described when the BASE_LOCATION has $'\r' at the end (and $EXT_LIB apennds /lib to it, not /extlib).
#! /bin/bash
export BASE_LOCATION=/home/hduser/hdm$'\r'
export EXT_LIB=$BASE_LOCATION/lib
echo $EXT_LIB
Maybe you edited the script on MSWin and it inserted its line-endings into it? Or you extract the value from a file that comes from MSWin?
Just remove the \r's from the value.
Probably the file you are editing is for a different user or you are not logged in as the user for the conf file you are editing.
If it is then i would hazard a guess that there are many more lines and it is set somewhere after in the conf script as something else or even dynamically held until restart by setting from an install script.

Reading values from config file in linux [duplicate]

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

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