ricorico94
Posts: 2
Joined: Thu Aug 22, 2019 4:00 pm

bash script command not found

Thu Aug 22, 2019 4:09 pm

Hi,
I' using a PI 3B+ with a brand new installation of Stretch Lite (latest release from April 2019).
I use it for Domoticz and so I had built bash scripts which were working since 2-3 years. Now, they don't work anymore. To be honest, they were working ni a previous install and then one day, they stopped working. That's why I decided to reinstall from scratch to have a clean system.. but it's still not working.

For instance, i have this script:

Code: Select all

#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>
and this folder:

Code: Select all

[email protected]:~/domoticz/scripts/bash $ dir
cinema.sh      HomieStop.sh  volet6monter.sh  voletsnew.sh
HomieStart.sh  test.sh       volet6select.sh  volets.sh
I had made them executable:
[email protected]:~/domoticz/scripts/bash $ ls -l
total 32
-rwxr-xr-x 1 pi pi 986 Nov 13 2016 cinema.sh
-rwxr-xr-x 1 pi pi 106 Feb 2 2016 HomieStart.sh
-rwxr-xr-x 1 pi pi 116 Feb 2 2016 HomieStop.sh
-rwxr-xr-x 1 pi pi 44 Feb 28 22:30 test.sh
-rwxr-xr-x 1 pi pi 1143 Feb 2 2016 volet6monter.sh
-rwxr-xr-x 1 pi pi 1132 Feb 2 2016 volet6select.sh
-rwxr-xr-x 1 pi pi 975 Feb 28 22:27 voletsnew.sh
-rwxr-xr-x 1 pi pi 951 Feb 1 2016 volets.sh
If I run the command, I get an error message:

Code: Select all

[email protected]:~/domoticz/scripts/bash $ test.sh
-bash: test.sh: command not found
[email protected]:~/domoticz/scripts/bash $ sudo test.sh
sudo: test.sh: command not found
Any idea on what to do/test ?

br,
Ricorico94

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5862
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: bash script command not found

Thu Aug 22, 2019 4:13 pm

That should've been './test.sh' (note the './')

When you run a command, it will look in each directory in your $PATH. Since test.sh is in your current directory, which isn't in $PATH, it won't find it.

You can use a full path, like /home/pi/test.sh, which would work, or you could give a relative path from where you are, like "./test.sh", but just "test.sh" would never work unless you set up $PATH accordingly.

User avatar
rpdom
Posts: 14985
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: bash script command not found

Thu Aug 22, 2019 4:22 pm

Do you really have <CR> characters on the end of each line? That is typical of a file written by Windows Notepad. Linux will not understand the CR characters. It expects each line to be terminated by just an LF character. That will cause a script to fail with a file not found error as it will look for "/bin/sh<CR>" instead of "/bin/sh".

Notepad++ for Windows can save a file with "Unix/Linux line endings". I believe Wordpad can do it too.

ricorico94
Posts: 2
Joined: Thu Aug 22, 2019 4:00 pm

Re: bash script command not found

Thu Aug 22, 2019 8:06 pm

Thanks for the tips.
Regarding the <CR>, I had indeed seen that and corrected, editing directly (using winscp) on the raspberry. But I was not trying to launch the script with the "./".
Now, indeed the test.sh script is working that way, but my other scripts are still not working again.. That must be because of something with domoticz or the way I handle some calls inside which must have changed for some reason at some point...

Thanks again for your explanations as it helps me investigating further !

br,
Ricorico94

jbudd
Posts: 952
Joined: Mon Dec 16, 2013 10:23 am

Re: bash script command not found

Thu Sep 05, 2019 2:04 pm

#!/bin/sh

echo Hello World
The hashbang line specifies the script should be interpreted by /bin/sh which is Dash not Bash. Maybe your more complex scripts have Bash specific code?

Return to “Other programming languages”