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=$OLDIFSbut when i try to run it i get this error: /root/transmission_r.sh: 1: /root/transmission_r.sh: #!/bin/sh: not found
61 Answer
Your script starts with:
#!/bin/shThis 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/shshows 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/bashIf
/bin/shdoes 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 (
\nor hexadecimal0a) instead of, for example, the Windows standard CRLF (\r\nor hexadecimal0d0ain the example above). On a Mac this would actually throw a different error:/bin/sh^M: bad interpreter: No such file or directoryMake sure file encodings do not mess up, such as an (invisible) Unicode BOM (
ef bb bfin 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.| 4