bash - change directory inside a script wont work - bash

Try to find a solution for a problem but stucked with the following :
I have a path of a folder (I got full path and partial path).
Im tying to cd to that folder, but it keep saying "No such file or directory".
Thats the partial code :
for var in "$#" ; do
if [[ -d $var ]] ; then
if [ "$(ls -A $var)" ]; then
cd $var
Would appericiate any help :)
Thanks

I think this line is your problem:
if [ "$(ls -A $var)" ]; then
Why do you need this test at all? Your previous check looks for the existence of the directory already.

Related

Create new Directories with Bash Script

I'd like to create a script to run every hour (with crontab) to make a folder with the name of any file with the correct extension (minus the extension) and move that file into it. So the end result would be the script would execute, find every .mp4 file in /Directory, create a folder for each of them with the same name as the file (minus extension) in /Other/Directory, and move the file into the matching folder. I can understand not wanting to write something for someone for free, but if you could point me in the right direction, I would really appreciate it.
EDIT: Thanks to #Barmar for the help!
#!/bin/bash
cd "/home/kali/Videos"
for FILE in *;do
bn=$(basename $FILE .mp4)
mkdir /home/kali/Videos/$bn;done
mv $bn.mp4 /home/kali/Videos/$bn
The script you would be looking for is as follows:
#!/bin/bash
REPOSITORY="/home/kali/Videos"
cd "${REPOSITORY}"
### This approach is best for handling filenames that might have spaces or scpecial characters.
ls |
while [ true ]
do
read FILE
if [ -z "${FILE}" ] ; then break ; fi
if [ -f "${FILE}" ]
then
bn=`basename "${FILE}" ".mp4" `
mkdir "${REPOSITORY}/$bn"
mv "${FILE}" "${REPOSITORY}/$bn"
( cd "${REPOSITORY}/$bn" ; extract_images "./${FILE}" )
fi
done

Bash script - File directory does not exist

I'm creating a very simple bash script that will check to see if the directory exists, and if it doesn't, create one.
However, no matter what directory I put in it doesn't find it!
Please tell me what I'm doing wrong.
Here is my script.
#!/bin/bash
$1="/media/student/System"
if [ ! -d $1 ]
then
mkdir $1
fi
Here is the command line error:
./test1.sh: line 2: =/media/student/System: No such file or directory
Try this
#!/bin/bash
directory="/media/student/System"
if [ ! -d "${directory}" ]
then
mkdir "${directory}"
fi
or even shorter with the parent argument of mkdir (manpage of mkdir)
#!/bin/bash
directory="/media/student/System"
mkdir -p "${directory}"
In bash you are not allow to start a variable with a number or a symbol except for an underscore _. In your code you used $1 , what you did there was trying to assign "/media/student/System" to $1, i think maybe you misunderstood how arguments in bash work. I think this is what you want
#!/bin/bash
directory="$1" # you have to quote to avoid white space splitting
if [[ ! -d "${directory}" ]];then
mkdir "$directory"
fi
run the script like this
$ chmod +x create_dir.sh
$ ./create_dir.sh "/media/student/System"
What the piece of code does is to check if the "/media/student/System" is a directory, if it is not a directory it creates the directory

Command is_dir not found

I'm trying to check if a folder exists. If it doesn't, I create it.
I have this code:
if [ $(is_dir "$contaniningdir/run") = "NO"]; then
mkdir "$containingdir/run"
fi
However, I'm getting:
is_dir: command not found
So how what's the correct way of doing this?
You should use
if [ ! -d "$DIRECTORY" ]; then
# your mkdir and other stuff ...
fi
as per this question/answer.. Another relevant question/answer is here.
One of the comments also mentions an important notice:
One thing to keep in mind: [ ! -d "$DIRECTORY" ] will be true either
if $DIRECTORY doesn't exist, or if does exist but isn't a directory.
For more you should probably check that other question's page.
is_dir is a PHP function that you probably mixed with bash unintentionally :)
bash is capable of checking for the existence of a directory without external commands:
if [ ! -d "${containingdir}/run" ]; then
mkdir "${containingdir}/run"
fi
! is negation, -d checks if the argument exists and is a directory

How to detect if a file is a folder in bash script on macOS?

I have created a macOS Service with Automator which actually will attach every file from the Finder to a new Thunderbird compose window and is just a simple bash script.
for f in "$#"
do
open -a /Applications/Thunderbird.app/ "$f"
done
This Service also would work for any folder, but for sure you can not attach a folder to a compose window. But my idea is now to let the script detect if the file is a document or a folder. If it is a document, attach it. If it is a folder, first zip compress it and than attach it. In the way:
if file is folder than
// zip compress folder
// attach *.zip to Thunderbird compose window
else // seems to be a document
// attach document to Thunderbird compose window
But how do I detect if the file is a folder and than compress it as zip file in the bash script?
if [[ -d "$file" ]]; then
# do your thing for the directory
else
# do the other thing for the file
fi
For more details, please see this related question: How do I tell if a regular file does not exist in Bash?
Code:
#!/bin/bash
if [ -d "$f" ]; then
upload_file="$f.zip"
# zip compress folder
zip "$f.zip" "$f"
elif [ -f "$f" ]; then # seems to be a document
upload_file="$f.zip"
else # Unknown file type
echo "Unknown file type." 1>&2
exit 1
fi
# attach file to Thunderbird compose window
open -a /Applications/Thunderbird.app/ "$upload_file"
exit 0
Explanation:
In bash "folders" are referred to as "directories." You should checkout the man page on test.
$ man test
The relevant section for you is:
NAME
test, [ -- condition evaluation utility
SYNOPSIS
test expression
[ expression ]
...
-d file True if file exists and is a directory.
-e file True if file exists (regardless of type).
-f file True if file exists and is a regular file.
To test if a file is a directory:
test -d "$f"
OR
[ -d "$f" ]
To test if a file is a regular file:
test -f "$f"
OR
[ -f "$f" ]
Edit: Quoted variables in sample code to avoid globbing and word splitting.
This command [ -f "$filename" ] will return true for files, while [ -d "$dirname" ] will return true for directories.
I would suggest using a check for file as well, because you could have things that are neither directories nor files.
I would approach it this way:
if [ -d "$fileDirectory" ]; then myCommandDirectories;
elif [ -f "$fileDirectory" ]; then myCommandFiles;
elif [ -z "$fileDirectory" ]; then myCommandEmptyArgument;
else myCommandNotFileDirectory; fi
On the code above, the syntax if [ -d ... ] would test if the argument is a directory, the syntax if [ -f ... ] would test if the argument is a file, the syntax if [ -z ... ] would test if the argument is unset or set to the empty string, and if the argument is none of those, you could still execute a certain command/script (in the exemple above myCommandNotFileDirectory).
Note: I included checking for an empty string, even if that was not asked on the question, because this is a "quality/error" control test I would normally do -- the variable "$fileDirectory" should never be empty on this context, and if it is, I would like to know that (it would show me that the script is not working properly), and thus I normally would redirect that command to a log file, like this:
elif [ -z "$fileDirectory" ]; then somecommand && echo "empty fileDirectory string ocurred" >> /var/log/mylog;

Reading a Directory and Verifying if it exists Bash

I have checked everywhere and tried many different "Solutions" on checking to see if the directory exists. Here's my code:
#!/bin/bash
echo -e "Where is the directory/file located?"
read $DIRECTORY
if [ -d "$DIRECTORY" ]; then
echo "Exists!"
else
echo "Does not exist!"
fi
What I am trying to do is have the user input a directory and for the script to check if it exists or not and return a result. This will ultimately tar/untar a directory. Regardless of whether the directory exists or not, it returns the answer "Does not exist!". (The input i'm trying is ~/Desktop, and from what I know that is 100% correct. Any concise answers are much appreciated :).
Your script can be refactored to this:
#!/bin/bash
read -p 'Where is the directory/file located?' dir
[[ -d "$dir" ]] && echo 'Exists!' || echo 'Does not exist!'
Basically use read var instead of read $var
Better not to use all caps variable names in BASH/shell
Use single quotes while using ! in BASH since it denotes a history event

Resources