语法错误:"("在bash脚本中意外

时间:2014-08-25 23:04:17

标签: bash syntax-error sh

编辑:我修复了我的脚本。它似乎工作。如果有人有任何改进建议我会很乐意帮助。显然我需要使用bash而不是sh来运行它。 这是更新的脚本:

#!/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

该脚本用于将torrent文件添加到文件夹并将其添加到传输中。这是脚本的原始版本:

#!/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

问题在于,当我运行脚本时,我得到了

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

我尝试使用bash,sh和zsh运行脚本,似乎没有工作。我无法弄清楚问题是什么。

1 个答案:

答案 0 :(得分:3)

这是当前的问题:

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

正在执行以下操作:

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

...也就是说,它试图将.torrent文件作为脚本启动(第一个参数为=,第二个参数为/home/me/box/*/*.torrent),生成语法错误。相反,使用:

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

此脚本中的其他地方还有其他问题 - 我强烈建议您通过http://shellcheck.net/运行它。