Linux sh script throws "#!/bin/sh: not found"

2

2

i have a script

#!/bin/sh

# Automatically remove a torrent and delete its data after a specified period of
# time (in seconds).

TARGET=/var/www/transmission/completed
USER=name
PASS=pass
BIN="/usr/bin/transmission-remote"

# The default is 5 minutes (in seconds).
CUTOFF=`expr 100 \* 3`

##############################################
### You shouldn't need to edit below here. ###
##############################################

# Tokenise over newlines instead of spaces.
OLDIFS=$IFS
IFS="
"

for ENTRY in `$BIN -n $USER:$PASS -l | grep 100%.*Done.*Finished`; do

    # Pull the ID out of the listing.
    ID=`echo $ENTRY | sed "s/^ *//g" | sed "s/ *100%.*//g"`

    # Determine the name of the downloaded file/folder.
    NAME=`$BIN -n $USER:$PASS -t $ID -f | head -1 |\
         sed "s/ ([0-9]\+ files)://g"`

    # If it's a folder, find the last modified file and its modification time.
    if [ -d "$TARGET/$NAME" ]; then
        LASTMODIFIED=0
        for FILE in `find $TARGET/$NAME`; do
             AGE=`stat "$FILE" -c%Y`
             if [ $AGE -gt $LASTMODIFIED ]; then
                 LASTMODIFIED=$AGE
             fi
        done

    # Otherwise, just get the modified time.
    else
        LASTMODIFIED=`stat "$TARGET/$NAME" -c%Y`
    fi

    TIME=`date +%s`
    DIFF=`expr $TIME - $LASTMODIFIED`

    # Remove the torrent if its older than the CUTOFF.
    if [ $DIFF -gt $CUTOFF ]; then
        date
        echo "Removing $NAME with ID:$ID"
        $BIN -n $USER:$PASS -t $ID --remove-and-delete
    fi

done

IFS=$OLDIFS

but when i try to run it i get this error: /root/transmission_r.sh: 1: /root/transmission_r.sh: #!/bin/sh: not found

Nojus741

Posted 2015-08-01T10:58:53.650

Reputation: 33

...so, what does ls /bin/sh give you? – Arjan – 2015-08-01T11:14:12.560

it gives me: /bin/sh – Nojus741 – 2015-08-01T11:18:59.647

1Any funny characters on that first line then? – Arjan – 2015-08-01T11:21:08.830

http://i.imgur.com/V6V4OJw.png – Nojus741 – 2015-08-01T11:22:25.920

4Ahh, got it, file encoding was set to UTF-8, i set it to without BOM and it works now. – Nojus741 – 2015-08-01T11:23:32.503

Also, line endings might fool you. – Arjan – 2015-08-01T11:24:36.107

Answers

13

Your script starts with:

#!/bin/sh

This is not a comment, but a shebang to tell your operating system to use /bin/sh to execute the script. But apparently Ubuntu cannot find it.

  • If ls /bin/sh shows no result, then I guess that needs to be fixed. As a temporary solution, you might be lucky that your script also works with, for example, bash:

    #!/bin/bash
    
  • If /bin/sh does exist (like it should), then somehow Ubuntu cannot interpret that first line. Dump it as hex:

    head -n 1 myscript.sh | hexdump -C
    
    00000000  ef bb bf 23 21 2f 62 69  6e 2f 73 68 0d 0a        |...#!/bin/sh..|
    

    And with the result:

    • Make sure line endings are using the Unix format, LF (\n or hexadecimal 0a) instead of, for example, the Windows standard CRLF (\r\n or hexadecimal 0d0a in the example above). On a Mac this would actually throw a different error:

      /bin/sh^M: bad interpreter: No such file or directory
      
    • Make sure file encodings do not mess up, such as an (invisible) Unicode BOM (ef bb bf in the example output above). In the above example your shell is not seeing a comment or the shebang, but is trying to run a command that starts with 3 invisible characters. In this case your shell would probably still execute the next lines, or might even complete the script successfully. On a Mac the first line actually throws:

      ./myscript.sh: line 1: #!/bin/sh: No such file or directory
      

To dump the error message to see the invisible characters in that message, one needs to redirect the error output to the standard output:

./myscript.sh 2>&1 | hexdump -C

00000000  2e 2f 6d 79 73 63 72 69  70 74 2e 73 68 3a 20 6c  |./myscript.sh: l|
00000010  69 6e 65 20 31 3a 20 ef  bb bf 23 21 2f 62 69 6e  |ine 1: ...#!/bin|
00000020  2f 73 68 3a 20 4e 6f 20  73 75 63 68 20 66 69 6c  |/sh: No such fil|
00000030  65 20 6f 72 20 64 69 72  65 63 74 6f 72 79 0a     |e or directory.|

Arjan

Posted 2015-08-01T10:58:53.650

Reputation: 29 084

transmission_r.sh: 1: transmission_r.sh: #!/bin/bash: not found – Nojus741 – 2015-08-01T11:19:26.690

1To remove it, open it in vi and do a ":setlocal nobomb" – ndrix – 2016-03-24T04:02:19.317

it was CRLF endings in my case (with /bin/sh: 1: script.sh: not found error. wat). – artin – 2019-05-08T11:26:42.147