4

I created a shell script "/etc/aaa" on Openwrt which contains the following code:

    #!/bin/sh
    echo "Hello World!"

I also used this command to ensure the proper permissions:

chmod 777 /etc/aaa

Upon executing with any of the below 2 commands

sh /etc/aaa

or

ash /etc/aaa

it works well and prints "Hello World". The problem occurs when I try to execute it with this command:

/etc/aaa

where I get this error:

-ash: /etc/aaa: not found

Can anyone please explain why this is happening? What am I missing here?

demonguy
  • 1,977
  • 5
  • 22
  • 34
  • Executable permission on the script is missing. – Etan Reisner Dec 03 '14 at 15:12
  • No, i've already used "chmod 777 /etc/aaa", i don't think it's related to permissions. Sorry i forgot to mention that – demonguy Dec 03 '14 at 15:12
  • 5
    DOS newlines? Possibly only on the first (shebang) line? – Etan Reisner Dec 03 '14 at 15:18
  • Another thing that can screw this up is a UTF-8 "Byte-Order Mark" (which doesn't make sense, but I digress) What does `file /etc/aaa` think about your script, if you have `file`? – ComputerDruid Dec 03 '14 at 15:25
  • @EtanReisner Sorry i don't understand what you mean. What is DOS newline? – demonguy Dec 03 '14 at 15:29
  • @ComputerDruid I don't have file command, but i tried to save /etc/aaa in both utf-8 encoding and windows-1252 encoding. But the problem stands still – demonguy Dec 03 '14 at 15:30
  • What does `xxd /etc/aaa` or `hexdump -C /etc/aaa` or `od -x /etc/aaa` say (whichever you have that works)? – Etan Reisner Dec 03 '14 at 15:34
  • 2
    Re: "DOS newlines" -- if your file was created in Windows without an editor that understands UNIX text files, its lines will end with a two-byte CRLF sequence. UNIX only uses a single-character newline, so the extra character is treated as data on UNIX, and thus (for instance) part of the filename on the shebang. This means that instead of looking for `/bin/sh`, you get your OS trying to run `/bin/sh$'\r'`, which doesn't exist, hence the error. – Charles Duffy Dec 03 '14 at 15:57
  • What happens when you CREATE an executable file in /etc - So, for clarity, NOT FTP a file to it, but create it from scratch ? – tvCa Dec 04 '14 at 13:56
  • Possible duplicate of [bash script always prints "Command Not Found"](http://stackoverflow.com/questions/7362504/bash-script-always-prints-command-not-found) – tripleee May 24 '16 at 19:20

2 Answers2

16

Thanks for Charles Duffy's comment. It's really the CR LF problem. Though i open the file in my linux virtual machine, since it has been modified in Windows. so enven in linux, the sublime text editor i use will think it's a windows file and use crlf as newline ending.

So ,just replace crlf with lf , the problem solved.

demonguy
  • 1,977
  • 5
  • 22
  • 34
5

Also worth checking that your shebang is correct, either: #!/bin/sh or #!/bin/bash

Michael
  • 131
  • 1
  • 6