# Script file "command not found"



## windstyle (Jul 16, 2014)

Good day.  I'll try to explain as best as possible even if I am using an automatic translation. I have a problem on a system with FreeBSD sh() scripts. The files are loaded via a ZIP archive, unpacked and set via `chmod` and property owner in 0755. At this point, the file should be executable, and instead of trying to run it returns an error command not found. To make the script executable has identified that it is necessary to open it (with vi() or another editor) and save it again, even without any change in its entirety. At the next attempt to launch, the execution is successful.

The question is why? I cannot understand what I do is that simply saving the file before this becomes executable when it is not. 

Thanks, I'm going crazy.


----------



## SirDice (Jul 16, 2014)

windstyle said:
			
		

> At this point, the file should be executable, and instead of trying to run it returns an error "command not found"


Look at the first line of the script, the so-called "she-bang". It's very likely it's pointing to an interpreter that either doesn't exist or is in a different place on FreeBSD.


----------



## wblock@ (Jul 16, 2014)

Re-saving a script should not change that.  Maybe `man csh | less -p rehash`.


----------



## windstyle (Jul 16, 2014)

Thank you for your response. The first line of the script reads 
	
	



```
#!/bin/sh
```
 as an interpreter that is correct. Moreover, I repeat that the file is changed from "command not found" to the "executable" only by opening it with a text editor and save, without changing any part thereof.


----------



## ondra_knezour (Jul 16, 2014)

Does the file work as expected when run like `/bin/sh /full/path/to/file`? edit: Meant without the open and save step, just after unziping from archive.

May be some strange non-printable characters in such file, which may be silently converted during display in the editor? Different line endings between UNIX and Windows systems comes on my mind as first usual suspect.


----------



## bsdkeith (Jul 16, 2014)

After 
	
	



```
chmod +x myscript.sh
```
 try 
	
	



```
./myscript.sh
```
 from the directory of your script, or 
	
	



```
sh myscript.sh
```


----------



## worldi (Jul 16, 2014)

Maybe it's a newline issue (CRLF vs LF)? Does `file yourscriptsname` mention something about "CRLF"?


```
% file testscript.sh
testscript.sh: POSIX shell script, ASCII text executable, with CRLF line terminators
% ./testscript.sh 
./testscript.sh: Command not found.
% cp testscript.sh testscript.sh.crlf
% tr -d '\r' <testscript.sh.crlf >testscript.sh
% file testscript.sh
testscript.sh: POSIX shell script, ASCII text executable
% ./testscript.sh
Woohoo! :)
%
```


----------



## wblock@ (Jul 16, 2014)

@worldi is right, I'd bet on it.  I thought of that just now and came back to this thread... too late.


----------



## windstyle (Jul 17, 2014)

Hi.

Thank you to all for the answer. I will try the variety of solutions and supply feedback to you. About the Windows Vs Linux CRLF, the script was written on freebsd FreeBSD for breebsd FreeBSD, but during the time the file was network-copied to *W*indows and another time from *W*indows to freebsd FreeBSD; *I* don't *k*now if this can alter the file, probably. *I* will check.

Thanks a lot, see you later.


----------



## bsdkeith (Jul 17, 2014)

Files (Linux or BSD) written to a Windows machine & back tend to get their permissions changed.


----------



## windstyle (Jul 31, 2014)

OK, before some test, yes. The problem was caused by a WinSCP file copy from Linux to Windows and from Windows to Linux.

Thanks to all, bye bye.


----------

