user450632
user450632

Reputation: 1

Syntax error: "(" unexpected in bash script

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.

Upvotes: 0

Views: 464

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295308

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/.

Upvotes: 3

Related Questions