BASH script with a $ not saving correctly [duplicate] - linux

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

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!

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

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 foreach loop works differently when executed from .sh file [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 9 years ago.
The following 'oneliner' does what I need:
$ for i in {1..5}; do echo $i; done
1
2
3
4
5
However, when I place exactly the same code into for-each.sh file and execute it, I get different result. Why?
for-each.sh file:
#!/bin/bash
for i in {1..10}; do echo $i; done
Result after execution:
$ ./for-each.sh
{1..10}
EDIT
Uf. I'm sorry. Now I noticed that I executed the for-each.sh by sh ./for-each.sh command and not by ./for-each.sh. I didn't know the difference between bash, sh, dash, ... After reading stackoverflow.com/a/5725402/915756 I realized that I executed the file by dash which points to /bin/sh by default on my Debian machine.
If you're confident that it is indeed bash executing your script, you can explicitly turn on brace expansion (expansion of {...} expressions) as follows:
set -B # same as: set -o braceexpand
Make this your script's first command after your shebang.
(Conversely, set +B (set +o braceexpand) would turn brace extension OFF.)
Conceivably, your system is configured to have brace extension turned off by default.

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