0

EDIT: I have fixed my script. It seems to be working. If anyone has any suggestions for improvement I would love the help. Apparently I needed to run it using bash instead of sh. Here is the updated script:

#!/bin/bash
for file in /home/corey/box/*/*
do
dir=$(basename $"(dirname "$file")")
sudo chmod 0777 /var/log/torrentwatch.log
sudo chmod -R 0777 /home/corey/box/*/*
if [[ "$file" = /home/corey/box/*/*.torrent ]]
then
echo "[$(date)]" "$file added to queue." >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 --auth=transmission:transmission -w /media/Media/Torrents/"$dir" -a "$file"
sleep 40 && rm "$file"
sleep 3 && sudo chmod -R 777 /media/Media && sudo chown -R debian-transmission:debian-transmission /media/Media/info
fi
done

The script is for adding torrent files to a folder and having them added to transmission. Here's the original version of the script:

#!/bin/bash
for file in /home/me/box/*/*
do
dir=$(basename $(dirname "$file"));
sudo chmod 0777 /var/log/torrentwatch.log
sudo chmod -R 0777 /home/me/box/*/*
if "$file" = "/home/me/box/*/*.torrent"; then
echo [`date`] "$file" added to queue. >> /var/log/torrentwatch.log
/usr/bin/transmission-remote localhost:9091 --auth=transmission:transmission -l -w /media/Media/Torrents/$dir -a "$file"
sleep 40 && rm "$file"
sleep 3 && sudo chmod -R 777 /media/Media && sudo chown -R debian-transmission:debian-transmission /media/Media/info
fi
done

The problem is that when I run the script I get

/home/me/box/TV/Name.of.file.torrent: Syntax error: "(" unexpected

I've tried running the script with bash, sh, and zsh, and none seem to work. I can't figure out what the problem is.

10
  • Your if statement is wrong -- if [[ $file = /home/me/box/*/*.torrent ]] would work. Your echo needs more quotes -- echo "[$(date)] $file added to queue" would be safer. You need to quote your expansions -- anywhere you use $dir needs to be inside double-quotes. Commented Aug 25, 2014 at 23:09
  • 1
    Indenting your code would make it much easier to read. Commented Aug 25, 2014 at 23:11
  • Not a response to Keith, but dir=$(basename $(dirname "$file")) attempts to get the filename (basename) of the file after it has already been stripped by dirname. Commented Aug 25, 2014 at 23:18
  • 1
    Following up to my earlier comment, obviously /home/me/box/TV/Name.of.file.torrent has execute permission (though there's no good reason for it to), and the script is trying to execute it. Without a #! line, it's executed by /bin/sh, which produces that error message. Fixing the if condition will avoid executing the *.torrent files. (And doing a chmod -x on any files (not directories!) that don't need to be executable is a good idea.) Commented Aug 25, 2014 at 23:21
  • 1
    @user450632, then you're running your script with sh, not bash. Since your shebang is correct (#!/bin/bash), I presume that you're explicitly starting it with sh yourscript.sh; don't do that. (It could also be that you have comments or other characters before your shebang; it needs to be at the very top of the file). sh doesn't have support for doing pattern matches built-in, which your code as currently formulated requires. Commented Aug 25, 2014 at 23:41

1 Answer 1

3

This is the immediate problem:

if "$file" = "/home/me/box/*/*.torrent"

It's running the following:

/home/me/box/TV/Name.of.file.torrent = "/home/me/box/*/*.torrent"

...which is to say, it's trying to start the .torrent file as a script (with its first argument being = and its second argument being /home/me/box/*/*.torrent), which generates a syntax error. Instead, use:

if [[ $file = /home/me/box/*/*.torrent ]]

There are other issues elsewhere in this script -- I strongly recommend running it through http://shellcheck.net/.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.