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.
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$dirneeds to be inside double-quotes.dir=$(basename $(dirname "$file"))attempts to get the filename (basename) of the file after it has already been stripped bydirname./home/me/box/TV/Name.of.file.torrenthas 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 theifcondition will avoid executing the*.torrentfiles. (And doing achmod -xon any files (not directories!) that don't need to be executable is a good idea.)sh, notbash. Since your shebang is correct (#!/bin/bash), I presume that you're explicitly starting it withsh 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).shdoesn't have support for doing pattern matches built-in, which your code as currently formulated requires.