Replace Tilde with $HOME in bash script [duplicate] - string

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!

Related

How to fix command not found in Linux shell scripting [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.
In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

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.

simple script to remove files in linux bash [duplicate]

This question already has an answer here:
Find "command not found" when executed in bash loop
(1 answer)
Closed 3 years ago.
I made a simple script in linux bash just like bellow:
#!/bin/bash
PATH=/tmp_with_zip_files
FILETYPE=zip
i=1
for filename in $PATH/*.$FILETYPE;
do
echo "rm $filename";
if [ -f $filename ];
then rm $filename;
fi
i=$((i+1))
done
echo "$i files removed"
But, when i run script i have error, because script doesnt work correctly. It's mean from console i have a message:
zip_delete.sh: line 11: rm: command not found
Why linux bash script not recognize linux command rm?
Lol I think it's because you're overwriting the default $PATH variable (which is the variable that tells bash where to look for executables). During execution, it can't find the rm program in PATH because it's pointing to only /tmp_with_zip_files
Use a different variable name for your purposes like chicken_nuggets.
WARNING DON'T DO THE FOLLOWING LMAO PATH=$PATH:/tmp_with_zip_files you could delete a bunch of things from PATH and that would suck really bad
The PATH variable holds the path to OS commands (like rm), don't use that as a variable, name it something else, like path_to_files.

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

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.

Resources