Bash script always prints “Command Not Found”

2019-01-01 10:41发布

Every time I run a script using bash scriptname.sh from the command line in Debian, I get Command Not found and then the result of the script. So the script works but there is always a Command Not Found statement printed on screen.

I am running the script from the /var folder.

Here is the script:

#!/bin/bash

echo Hello World

I run it by typing the following:

bash testscript.sh

UPDATE - the problem appears to the blank lines. Each blank line is resulting in a command not found. Why would this occur?

16条回答
还给你的自由
2楼-- · 2019-01-01 11:29

I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)

As @artooro added in comment:

To fix that issue run chmod +x testscript.sh

查看更多
何处买醉
3楼-- · 2019-01-01 11:35

You can use bash -x scriptname.sh to trace it.

查看更多
刘海飞了
4楼-- · 2019-01-01 11:38

Had the same problem. Unfortunately

dos2unix winfile.sh
bash: dos2unix: command not found

so I did this to convert.

 awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh

and then

bash unixfile.sh
查看更多
登录 后发表回答