Command not found in Bash's IF-ELSE condition when using [! -d $DIR ]
Add space between [ and !. And before ] as well.
#!/bin/bash
DIR="test_dir/";
if [ ! -d "$DIR" ]; then
# If it doesn't create it
mkdir $DIR
fi
It's also a good idea to quote your variable:
mkdir "$DIR"
Bash Script conditional statement for directory creation - command not found error
You would have seen your mistake immediately, if you would not just have looked at command not found, but what bash printed before that: It outputs the command which was not found.
A conditional statement in bash has the form
if COMMAND
then
....
In your case, COMMAND is something like
[$TODAY == 01 && $MONTH == 02]
which is actually two commands, connected by &&. Let's assume that TODAY holds the string 05
. In this case, you would have the command
[05 == 01
which means: Call a program stored in the file named [05
, with the parameters ==
and 01
. Since there likely is no file of this odd name in your PATH, you get command not found.
There are several ways to solve this, but IMO, the most readable is
if [[ $TODAY == 01 && $MONTH == 05 ]]
In this case, we have [[
as command (and not [05
), and since this is a syntactic element of bash instead of an external command, it does not terminate by the &&
, but stretches up to the closing ]]
.
Bash Scripting [!: not found errors, and how to write an or statement
Another issue is that at the start of the script you do
files = `ls`
but then you cd to a different directory and try to loop round $files deleting them - of course this will not work since you have changed directories.
move the ls line to after you have changed directory.
What is the meaning of `! -d` in this Bash command?
-d
is a operator to test if the given directory exists or not.
For example, I am having a only directory called /home/sureshkumar/test/.
The directory variable contains the "/home/sureshkumar/test/"
if [ -d $directory ]
This condition is true only when the directory exists. In our example, the directory exists so this condition is true.
I am changing the directory variable to "/home/a/b/".
This directory does not exist.
if [ -d $directory ]
Now this condition is false. If I put the !
in front if the directory does not exist, then the if condition is true. If the directory does exists then the if [ ! -d $directory ]
condition is false.
The operation of the ! operator is if the condition is true, then it says the condition is false. If the condition is false then it says the condition is true. This is the work of ! operator.
if [ ! -d $directory ]
This condition true only if the $directory does not exist. If the directory exists, it returns false.
Bash Script If statements comparing user input, command not found
you should review the logic, but you can try this:
There is a space missing between if
[[
, also you need a space between [[
and variables inside it
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
#!/bin/bash
echo "Enter your name"
read name
echo "What hour is it?"
read hour
echo "${name}-${hour}"
if [[ "$hour" -gt 23 ]]
then
echo "That hour is not correct"
fi
if [[ "$hour" -ge 0 || "$hour" -le 7 ]]
then
echo "It's very early ${name}, you should be asleep"
fi
if [[ "$hour" -ge 8 || "$hour" -le 22 ]]
then
echo "Good day $name"
fi
if [[ "$hour" -eq 23 ]]
then
echo "Time for bed $name"
fi
if [[ -f "fandmf13.txt" ]]
then
cat fandmf13.txt
else
echo "File not found"
fi
Script gives if: command not found
I can solve this by using do eval "$HI"
. Of course, the problem is that if I use double quotes, then expansions are done at the first run. I don't want it to be expanded, but evaluated at every run.
EDIT: I got around this by using \$
, or even better, using single quotes (this did mean I had to escape all the single quotes though).
EDIT 2: After consideration, I just decided to use functions without eval
-- dealing with escaping was not worth it.
How do I check if a directory exists in a Bash shell script?
To check if a directory exists:
if [ -d "$DIRECTORY" ]; then
echo "$DIRECTORY does exist."
fi
To check if a directory does not exist:
if [ ! -d "$DIRECTORY" ]; then
echo "$DIRECTORY does not exist."
fi
However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check.
E.g. running this:
ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
rmdir "$SYMLINK"
fi
Will produce the error message:
rmdir: failed to remove `symlink': Not a directory
So symbolic links may have to be treated differently, if subsequent commands expect directories:
if [ -d "$LINK_OR_DIR" ]; then
if [ -L "$LINK_OR_DIR" ]; then
# It is a symlink!
# Symbolic link specific commands go here.
rm "$LINK_OR_DIR"
else
# It's a directory!
# Directory command goes here.
rmdir "$LINK_OR_DIR"
fi
fi
Take particular note of the double-quotes used to wrap the variables. The reason for this is explained by 8jean in another answer.
If the variables contain spaces or other unusual characters it will probably cause the script to fail.
\r': command not found - .bashrc / .bash_profile
When all else fails in Cygwin...
Try running the dos2unix
command on the file in question.
It might help when you see error messages like this:
-bash: '\r': command not found
Windows style newline characters can cause issues in Cygwin.
The dos2unix
command modifies newline characters so they are Unix / Cygwin compatible.
CAUTION: the dos2unix command modifies files in place, so take precaution if necessary.
If you need to keep the original file, you should back it up first.
Note for Mac users: The dos2unix
command does not exist on Mac OS X.
Check out this answer for a variety of solutions using different tools.
There is also a unix2dos
command that does the reverse:
It modifies Unix newline characters so they're compatible with Windows tools.
If you open a file with Notepad and all the lines run together, try unix2dos filename
.
Bash script prints Command Not Found on empty lines
Make sure your first line is:
#!/bin/bash
Enter your path to bash if it is not /bin/bash
Try running:
dos2unix script.sh
That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF)
to \n (LF)
.
More details about the dos2unix
command (man page)
Another way to tell if your file is in dos/Win format:
cat scriptname.sh | sed 's/\r/<CR>/'
The output will look something like this:
#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>
This will output the entire file text with <CR>
displayed for each \r
character in the file.
bash if [ false ]; returns true instead of false -- why?
You are running the [
(aka test
) command with the argument "false", not running the command false
. Since "false" is a non-empty string, the test
command always succeeds. To actually run the command, drop the [
command.
if false; then
echo "True"
else
echo "False"
fi
Related Topics
Using Bash Environment Variables from Within a Perl Script
Boost with Qt Creator and Linux
Gdb: Redirect Target Stdout Temporarly
Unable to Pass Wget a Variable with Quotes Inside the Variable
Copy Files from Windows to Windows Subsystem for Linux (Wsl)
Is It Safe to Delete the Journal File of Mongodb
How to Display Modified Date Time with 'Find' Command
What Are Ld-Linux.So.2 and Linux-Gate.So.1
How to Detect If a Git Clone Failed in a Bash Script
How to Automate the Installation of Eclipse Plugins with Command Line
Gcc: Putchar(Char) in Inline Assembly
Is Stack Memory Contiguous Physically in Linux