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.
How to get rid of 'command not found' from bash scripts on Mac OSX?
#!/bin/sh
(or #!/bin/bash
for Bash) needs to be the first line of your script. And use quotes.
#!/bin/bash
curl 'ec2-14-43-7-17.compute-1.amazonaws.com:10000' -d "$1"
And also run dos2unix script
to make sure your script is in UNIX file format. As @shellter has pointed out with comment, it seems that your script is not in UNIX format.
Note: dos2unix script
will convert the file in-place.
Cat in sh file doesn't echo empty lines
http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
In a=$(cat myfile.sh)
your variable gets assigned
the standard output of the command, with any trailing newlines deleted
And that is where your extra lines went.
command not found when running script
first give execute permission to script.sh
chmod +x script.sh
then ./script.sh
Empty Bash line throws error
'\r'
is Windows EOL marker which don't work weel in unix-like OSes.
try
dos2unix ./crons/BackupNeos.sh
Which will convert EOL to correct one.
Alternative approach using sed
:
sed -i 's/^M$//' ./crons/BackupNeos.sh
bash script gives command not found error when using if statement
Add a space after the [
:
if [ `echo $(s3cmd ls s3://my-bucket/) | grep -c "file.txt" ` -gt 0 ]
Your backticked command expand to 1
, so right now you have
if [1 -gt 0 ]
which means the shell is trying to run [1
as a command, and no such command exists.
bash command not found - is my PATH wrong
Your PATH
looks reasonable. PATH
problems are a common source of errors like this, but not the only one.
The error message ": No such file or directory" suggests that the script file has DOS/Windows-style line endings (consisting of a carriage return followed by linefeed) instead of unix-style (just linefeed). Unix programs (including shells) tend to mistake the carriage return for part of the line, causing massive confusion.
In this instance, it sees the shebang line as "#!/bin/bash^M" (where "^M" indicates the carriage return), goes looking for an interpreter named "/bin/bash^M", can't find it, and prints something like "/bin/bash^M: No such file or directory". Since the carriage return makes the terminal return to the beginning of the line, ": No such file or directory" gets printed on top of the "/bin/bash" part, so it's all you see.
If you have the dos2unix
program, you can use that to convert to unix-style line endings; if not, there are a variety of alternate conversion tools. But you should also figure out why the file has Windows/DOS format: did you edit it with a Windows editor, or something like that? Whatever caused it, you should make sure it doesn't happen again, because Windows/DOS format files will cause problems with most unix programs.
sh shell conditionals and output : not found:
- You have carriage returns in your script; that generates the "not found" messages and is probably why both branches of your
if
are getting generated. - Your dates are comparable as strings, no need to use
-eq
to compare them as numbers.
Related Topics
How to Convert Dos/Windows Newline (Crlf) to Unix Newline (Lf)
Can't Call C Standard Library Function on 64-Bit Linux from Assembly (Yasm) Code
Can Windows Containers Be Hosted on Linux
How to Get Docker Linux Container Information from Within the Container Itself
How to Setup Public-Key Authentication
Difference Between ${} and $() in Bash
How to Pipe Initial Input into Process Which Will Then Be Interactive
Assembly Segmentation Fault After Making a System Call, At the End of My Code
How to Get Full Path of a File
How to Change Permissions For a Folder and Its Subfolders/Files
Difference Between Clock_Realtime and Clock_Monotonic
Adding a New Entry to the Path Variable in Zsh
What Is Better "Int 0X80" or "Syscall" in 32-Bit Code on Linux
Maximum Number of Processes in Linux
How to Loop Over Directories in Linux
How to Open a Socket and Pass It to Another Process in Linux