Bash script not finding existing directory [duplicate] - bash

This question already has an answer here:
Why isn't tilde (~) expanding inside double quotes? [duplicate]
(1 answer)
Closed 24 days ago.
I am using the standard check if a directory exists.
#!/bin/bash
if [ -d "~/.junk" ]; then
echo "yeet"
[ -d "~/.junk" ] evaluates to false, however, when I cd into ~ and type ls -a, .junk is indeed one of the directories.
What could be happening?
I am using Linux Mint Vera

when you use the "~" in 'string' such in this case, the character is not treated as a special character and is interpreted literally, so you should replace by $HOME
And change the param -d to -e
-e - True if the file exists (regardless of type).
-d - True if the file is a directory.
#!/bin/bash
if [ -e "$HOME/.junk" ]; then
echo "yeet"
fi

Related

read input in bash scrip that is a directory with spaces in the path [duplicate]

This question already has answers here:
How to loop over files in directory and change path and add suffix to filename
(6 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I can't find a way to put an entry in read that contains spaces? I want to put directories in the "Enter directory to be cleaned:" read. I think it is reading the spaces as separate variables. Input would be something like /home/user/apple sauce The "cleaning" is just removing special characters from filenames.
#!/bin/bash
read -p "Enter directory to be cleaned: " directory
echo "Current Directory Structure"
/usr/bin/ls -la $directory
read input
if [ "$input" == "y" ]
then
echo "Cleaning files..."
for file in $directory; do mv $file $(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g'); done &
else
stop
fi
Another issue I am facing is the cleanup is repeating the entire directory when it creates the new filename. If I run that for file in *; do mv "$file" $(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g'); done & command in the directory itself, it just creates the new filename. If I specify the directory it writes out the whole directory:
++ sed -e 's/[^A-Za-z0-9._-]/_/g'
++ echo '/home/apples/scratch/test1/test:something?'
+ mv '/home/apples/scratch/test1/test:something?' _home_apples_scratch_test1_test_something_
I want it to just change the filename but having issues. Any help is thankful.
I think it is reading the spaces as separate variables
It does not, as you can easily verify with this:
read -p 'Enter string:' x
echo "Entered: >>>$x<<<"
If you dislike quoting your variables (to avoid word splitting), you may consider switching from bash to Zsh. Where you have to write "$x" in bash, you would simply write $x in Zsh.
Hence, you would have to write
for file in "$directory"
but this would loop just one iteration, with file bound to the content of the variable directory. For looping over the entries in this directory, you would do a
for dirent in "$directory"/*

Hi everyone! can someone tell me what " if [ $# -ne 1 ]"means in a shell script? [duplicate]

This question already has answers here:
Special variables in Unix shells? [closed]
(4 answers)
Is there a list of 'if' switches anywhere?
(5 answers)
Closed 5 years ago.
I have been trying to figure out the following line of code as well:
if [ ! -e $1 ]
thanks
Lets break it down:
$# is the number of remaining arguments
[ is the test command
-ne is the numeric "not equals" operator.
So if [ $# -ne 1 ] is testing if there is exactly one argument (left).
In your second example:
! means not
-e tests if a file exists
$1 is the first remaining argument
Therefore if [ ! -e $1 ] tests that there is no file or directory whose path is given as the first (remaining) argument.
Note that this may fail if the argument is a pathname containing whitespace or globing meta-characters. Quoting is needed to stop word splitting and globbing potentially mangling the pathname; i.e. if [ ! -e "$1" ]

alias for cp with arguments [duplicate]

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 5 years ago.
How do I modify bashrc to include a readme file with the pwd to original source every time I use cp or mv?
It should be something like this:
alias cp="pwd $1 > readme & cp $1 $2"
or
alias cp="pwd $1 > readme | cp $1 $2"
But instead of the path of the source, it gives me the path of the directory I am in.
You can't have aliases with arguments. Since you probably don't have $1 defined, pwd $1 just expands to pwd.
Also, pwd doesn't actualy take any positional arguments. If you want the source to appear in readme, use echo.
Create a function
cp() {
echo $1 > readme
/bin/cp $1 $2
}
Also,
& does not mean AND – it sends processing to background
| does not mean OR – it pipes output of left side to input of right side

Bash Script : How to pass a wildcard before my filename in "if" exist file? [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi

bash variable expansion in if statement [duplicate]

This question already has answers here:
Tilde expansion in quotes
(3 answers)
Closed 7 years ago.
Checking for the existence of a directory which should resolve to ~/code/devtools/deploy-mix.
This if statement doesn't pass -- though I can cd ~/code/devtools/deploy-mix. Plz help with bash syntax :)
GIT_DIR="$HOME/code"
if [ -d "${GIT_DIR}/devtools/deploy-mix" ]; then
echo "found $GIT_DIR/devtools/deploy-mix"
fi
output:
sh -x script
'[' -d '$HOME/code/devtools/deploy-mix' ']'
I'd love to give out some internet karma for this one and I'm ok if you decide to call me a noob (sometimes the coding angle doesn't sit on your shoulder).
Replace ~ by $HOME or remove quotes.
Examples:
GIT_DIR="$HOME/code"
GIT_DIR=~/'code'

Resources