tilde expansion in environment variable - linux

In my .sh file, I have this, cp $file $SOME_PATH, while $SOME_PATH is exported as export SOME_PATH="~/path/to/path/". But when I ran this .sh file, I got error message saying like no such "~/path/to/path/" exists.
I replaced ~ as $HOME, then the error was gone.
So what's up here with the tilde?
Thanks in advance.

use
SOME_PATH=~/path/to/path/
if you path have spaces, quote it
SOME_PATH=~/"path with spaces"

Remove the quotation marks on your export:
export SOME_PATH=~/path/to/path/

Related

Fatal: Failed to read password file: open ~/RPI/filename.sec: no such file or directory. But it exist [linux]

Trying to run .sh file. with --password "~/RPI.filename"in it. Should read the filename but getting an error. Even though the file exists
error:
Fatal: Failed to read password file: open ~/RPI/filename.sec: no such file or directory
list of directories and files:
~/RPI$ l
g.json m1/ m2/ filename.sec startm1.sh*
I have used it before and I know the method works but no idea what on earth is happening here.
Maybe your script is running in sh instead of bash ? I think "~" is bash syntax.
There are 1½ issues here...
First, quoting a tilde prevents tilde expansion; compare ls ~/RPI.filename and ls "~/RPI.filename"
=> leave the tilde unquoted if you don't want a literal '~'
Second, is it ~/RPI.filename or ~/RPI/filename?

How to include path inside bashrc in linux for envirnment

Im testing my code for automation of the installation of a software
In bashrc file below:
# User specific aliases and functions
export JAVA_HOME=/opt/jdk-9.0.1
export JRE_HOME=/opt/jdk-9.0.1/jre
export SCALA_HOME=/opt/scala-2.13.0
export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin
Here im trying to add $SCALA_HOME/bin to PATH.
this is the required output:
`export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin:/opt/scala-2.13.0`
`sed -i '1n;/^export PATH/i\export SCALA_HOME=/opt/scala-2.13.0' .bashrc`
the above code worked to append SCALA_HOME above path but for appending in the same line im not able to do
`sed -i "s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g" .bashrc
sed: -e expression #1, char 40: unknown option to `s'`
please help me get the correct sed command to append SCALA_HOME in the PATH
You can use this:
sed '/export PATH/ s/$/:\$SCALA_HOME\/bin/' .bashrc
's/\(export PATH=.*\)/\1:\$SCALA_HOME\/bin/'
To go through your expression:
s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g"
The \"export will look for "export in your file. Why do you expect a double quote before the export? It isn't there in the example. Likewise, PATH\": in the pattern will look for PATH": in the file. That double quote isn't there either. Your ,$ at the end of your pattern will also prevent it from matching anywhere.

unable to edit PATH in Ubuntu 16

I need to add google-cloud-sdk in PATH in ubuntu-16.
I try
gedit ~/.profile
inside .profile
export PATH = "$PATH:/$HOME/google-cloud-sdk"
but it give me error that
bash: export '=' : not a valid identifier
I am new to this. Please help.
As #mwweb correctly pointed out, assignments in BASH (and most other shells) cannot have spaces around the = operator. Just remove them:
export PATH="$PATH:$HOME/google-cloud-sdk"
Note that you likely want to remove the leading / from the path as well.

Check if directory exists not working

I have a textfile (qrs.txt) which contains dir names (one per line) and on my server in the same directory as the script I have those folders with corresponding names from the text file.
This is my script:
#!/bin/bash
while read p; do
if [ ! -d "$p" ];
then
echo "ERROR $p" >> log.txt
else
echo "GOOD" >> log.txt
fi
done < qrs.txt
qrs.txt:
1992300000183805
1992300001176204
1992300002145500
1992300003104507
1992300004104902
1992300005133703
1992300006117802
1992300007144501
1992300008172803
1992300009189005
1992300010146307
1992300011151700
1992300012190007
1992300013126802
1992300014111508
1992300015193908
When that if statement is inside the loop it always returns error which is incorrect because I can see the folders exist. When I take it out of the loop and check for just 1, it works fine... When I echo $p on the same line as error, I can see the file name its checking is indeed correct.
What am I missing here..?
EDIT:
Screenshot of qrs.txt in hex mode:
http://i.snag.gy/25mqJ.jpg
RESOLVED!
My qrs.txt was in [dos] format originally but once converted to unix format using ":set ff=unix" the script worked like a charm!
Your script works fine.
I copied your script to my local machine. When I put blh blah in the qrs.txt file, I got ERROR for each time I ran your script. I ran it four times. I changed the blh blah to a valid path and I received GOOD.
The directory 1992300000183805 for instance, may be not be a valid path. You need the fully qualified path name! For example, /home/user/1992300000183805.
ERROR blh blah
ERROR blh blah
GOOD
GOOD
EDIT
Looking at #chepner comments, I recreated your problem:
Open your qrs.txt file in vi or vim. You should see ^M at the end of your lines. To remove the ^M characters at the end of all lines in vi, use:
:%s/^M//g
This should fix your problem. If not, in vim type this:
:set ff=unix
save the file.
Re-open qrs.txt in vim, then run the regex above again, or manually delete the ^M.
Or you can use perl:
perl -pi -e "s/\r/\n/g;" <file>
OK so looking at your provided file it seems those are relative directory names -- as such current directory is very important when you execute the script. Do you execute the script from its own directory or from the parent directory to all the (sub)directories shown in your example?
In other words have you tried:
cd <parent directory>
/path/to/yourscript.sh
?
Not to mention the location of qrs.txt seems to be specified relative rather than absolute path. So if there's no qrs.txt in the current directory I don't think your script would work.

linux batch rename directories and strip # character from name

i have a directory with a lot of subdirectories with a # infront of them:
#adhasdk
#ad18237
I want to rename them all and remove the # caracter
I tried to do:
rename -n `s/#//g` *
but didn't seem to work.
-bash: s/#//g: No such file or directory
Any ideas on this.
Thanks
Just use
$ rename 's/^#//' *
use -n just to check that what you think it would happen really happens.
In you example you have the clue about the wrong quotes used (backticks) in the error message
-bash: s/#//g: No such file or directory
bash is trying to execute a command named s/#//g.
No that using g (global) and not anchoring the regular expression you will replace any #, not just the one in the first position.
I don't know whether it's just a typo when you typed it here, but that "rename" command should work if:
you leave off the "-n" and
you quote the substitution with regular single-quotes and not back-quotes
The "-n" tells it to not really do anything. The back-quotes are just wrong (they mean something but not what you want here).
The problem is that you use backticks (`). You should use normal quotes:
rename -n 's/#//g' *
for DIR in \#*/
do
echo mv "$DIR" "${DIR/#\#/}"
done
I had to rename all folders inside a given folder. Each folder name had some text inside round braces. The following command removed the round braces from all folder names:
rename 's/(.+)//' *
Some distros doesn't support regexp in rename. You have to install prename. Even more, sometimes you can't install prename and you have to install gprename to have binary prename.
If you have 'prename' then just change backtick character " ` " to single quote and everything should work.
So the solution should be:
prename -n 's/#//g' *
or
prename -n 'y/#//' *

Resources