Linux script can't find /bin/sh - its a Windows problem


A bit off my usual topics, but it might be useful to others.

I have been trying to set up a monitor on Riverbed loadbalancers to do LDAPS lookups.

On its own, the script ran fine. It was written on notepad++ on my windows box, and copied over to the loadbalancer.

Running it from the home directory worked, yet not from the scripts location. I kept getting this error:
[sfordham@loadbalancer]$ sudo /usr/local/zeus/zxtm/conf/scripts/ldapstest.sh --ipaddr=(ipaddress) --port=636 --node=(nodeipaddress)
[sudo] password for sfordham:
sudo: unable to execute /usr/local/zeus/zxtm/conf/scripts/ldapstest.sh: No such file or directory
Naturally I checked, double checked and checked again that the right files were where they should be. Everything looked ok, and the same script ran fine when running from the home directory. Weird, right?

I ran strace, and got the same kind of error:
[sfordham@loadbalancer ~]$ sudo strace -f -e trace=file /usr/local/zeus/zxtm/conf/scripts/ldapstest.sh --ipaddr=(ipaddress) --port=636
execve("/usr/local/zeus/zxtm/conf/scripts/ldapstest.sh", ["/usr/local/zeus/zxtm/conf/script"..., "--ipaddr=(ipaddress)", "--port=636"], [/* 16 vars */]) = -1 ENOENT (No such file or directory)
strace: exec: No such file or directory
[sfordham@loadbalancer ~]$
Clearly it can't find something.

It's a pretty simple bash script, starts with the usual shebang (#!/bin/sh) which is a symbolic link to bash. That all checked out and a deeper strace showed that all the paths were there.
I then stumbled across this post (http://unix.stackexchange.com/questions/27054/bin-bash-no-such-file-or-directory), which says that if you write a script in Windows, it has a tendancy to add an extra carriage return. True enough, this is what happened:
[sfordham@loadbalancer zxtm]$ sudo head -1 conf/scripts/ldapstest.sh | od -c
0000000   #   !   /   b   i   n   /   s   h  \r  \n
0000013
[sfordham@loadbalancer zxtm]$
Here you can see the \r carriage return after #!/bin/sh.

I created a new file, copied the contents from the old file into the new one (simple copy and paste), and this removed the extra return.

Now the script runs fine.

Lesson learned - don't use Windows to write Linux scripts!

CCIE #49337, author of CCNA and Beyond, BGP for Cisco Networks, MPLS for Cisco Networks, VPNs and NAT for Cisco Networks.

Related Posts

Previous
Next Post »