0

Bit of a script newbie, I have a mistake in my script that needs fresh eyes.

I am writing a script that will download multiple directories one only and extract content inside them. The script does this by

  • creating an array and using variables from that array
  • using an sqlite database to ensure that downloading only happens once - essential as this script will run every 30 minutes
  • run winrar and ffmpeg to extract the files

The following script works (it runs), but does not do what I want - here's the script

#!/bin/sh
set -x
cd /cygdrive/q/private/deluge/zeroday/
Progs=( * )
for show in "${Progs[@]%*/}"; do
  cd ~/scripts/sqlite/
  exists=$( sqlite3 dir.db "select count(*) from zero where episode=\"${show}\"" )
  if (( exists > 0 )); then
    echo "Show already downloaded"
    else
      ~/scripts/synctorrents1.sh
     cd /cygdrive/h/Rippers/Zeroday/
     dire=( * )
     for rip in "${dire[@]%*/}"; do
       cd /cygdrive/h/Rippers/Zeroday/${rip}/
       "/cygdrive/c/Program Files/WinRAR/unrar" x *.rar
       "/cygdrive/c/Program Files/get_iplayer/FFmpeg/ffmpeg-2.2.3-win32-static/bin/ffmpeg.exe" -i "${rip}.mkv"$
       mv ${rip}.mp4 /cygdrive/h/Rippers/MB3_Watch/
       rm -f ${rip}.mkv
       rm -rf /cygdrive/h/Rippers/Zeroday/${rip}/
       done
  fi
done
exit 0

The script calls another script, which does the downloading using LFTP. My problem is when I ask the script to download one directory, it downloads all directories. Here is that script -

#!/bin/sh
login="xxxx"
pass="xxxx"
host="xxxx"
remote_dir="/media/sdi1/home/macburp/private/deluge/zeroday/"
local_dir="/cygdrive/h/Rippers/Zeroday"

cd /cygdrive/q/private/deluge/zeroday/
Progs=( * )
trap "rm -f /tmp/synctorrent.lock" SIGINT SIGTERM
if [ -e /tmp/synctorrent.lock ]
then
echo "Synctorrent is running already."
exit 1
else
touch /tmp/synctorrent.lock
for show in "${Progs[@]%*/}"; do
lftp -p 22 -u $login,$pass sftp://$host << EOF
set mirror:use-pget-n 7
mirror -c -P1 --log=synctorrents.log $remote_dir/${show}/ local_dir/${show}/
quit
EOF
done
rm -f /tmp/synctorrent.lock
trap - SIGINT SIGTERM
exit 0
fi

This script (obtained from elsewhere) is picky - it doesn't want to work when the script is properly indented, and does not like being edited.

I see that I now need to combine the two scripts, so I've tried this -

#!/bin/sh
set -x
login="xxxx"
pass="xxxxxx"
host="xxxxxxxxxx"
remote_dir="/media/sdi1/home/macburp/private/deluge/zeroday/"
local_dir="/cygdrive/h/Rippers/Zeroday"

cd /cygdrive/q/private/deluge/zeroday/
Progs=( * )
for show in "${Progs[@]%*/}"; do
cd ~/scripts/sqlite/
exists=$( sqlite3 dir.db "select count(*) from zero where episode=\"${show}\"" )
if (( exists > 0 )); then
echo "Show already downloaded"
else
trap "rm -f /tmp/synctorrent.lock" SIGINT SIGTERM
if [ -e /tmp/synctorrent.lock ]
then
echo "Synctorrent is running already."
exit 1
else
touch /tmp/synctorrent.lock
lftp -p 22 -u $login,$pass sftp://$host << EOF
set mirror:use-pget-n 7
mirror -c -P1 --log=synctorrents.log $remote_dir/${show}/ $local_dir/${show}/
quit
EOF
cd /cygdrive/h/Rippers/Zeroday/
dire=( * )
for rip in "${dire[@]%*/}"; do
cd /cygdrive/h/Rippers/Zeroday/${rip}/
"/cygdrive/c/Program Files/WinRAR/unrar" x *.rar
"/cygdrive/c/Program Files/get_iplayer/FFmpeg/ffmpeg-2.2.3-win32-static/bin/ffmpeg.exe" -i "${rip}.mkv" -c:v $
mv ${rip}.mp4 /cygdrive/h/Rippers/MB3_Watch/
rm -f ${rip}.mkv
rm -rf /cygdrive/h/Rippers/Zeroday/${rip}/
done
exit 0
fi

This fails with the following error -

/home/Paul/scripts/sqlite/syncsqlremoterip5.sh: line 44: syntax error: unexpected end of file

Any thoughts oh how to put this right or on how to improve the script would be welcome.

Macburp
  • 1
  • 1
  • Your topmost `for` and `if` are not closed. – keltar Feb 24 '15 at 12:31
  • If this is running under bash then be sure to start the file with `#!/bin/bash` (or wherever your bash interpreter is placed) Also, check your 'here' file. The line that ends `<< EOF` is passing a file of commands terminated by EOF to the `lftp` command. Make sure your FTP path is being correctly expanded. – Component 10 Feb 24 '15 at 12:32
  • Keltar, I can't see for looking now - can you suggest where I should put code to fix? thanks. – Macburp Feb 24 '15 at 12:58
  • Component 10, the ftp download does work in previous versions of the script, I'm not at all certain that the EOFs used here are in the right place or all that efficient. Any thoughts on that. Thanks – Macburp Feb 24 '15 at 13:00
  • 1
    You will learn that if you use proper indentation, you won't get errors like this. Use a programmer's editor to handle indentation for you. – glenn jackman Feb 24 '15 at 14:07
  • @Macburp you have two `do`, but only one `done`. Sorry but I don't know what else to say to make it more obvious. – keltar Feb 25 '15 at 04:11

1 Answers1

0
#!/bin/sh
set -x;
login="xxxx";
pass="xxxxxx";
host="xxxxxxxxxx";
remote_dir="/media/sdi1/home/macburp/private/deluge/zeroday/";
local_dir="/cygdrive/h/Rippers/Zeroday";

cd /cygdrive/q/private/deluge/zeroday/;
Progs=( * );
for show in "${Progs[@]%*/}"; do
    cd ~/scripts/sqlite/;
    exists=$( sqlite3 dir.db "select count(*) from zero where ;episode=\"${show}\"" )
if (( exists > 0 )); then
    echo "Show already downloaded";
else
    trap "rm -f /tmp/synctorrent.lock" SIGINT SIGTERM;
**fi**
if [ -e /tmp/synctorrent.lock ]; then
    echo "Synctorrent is running already.";
    exit 1;
else
    touch /tmp/synctorrent.lock;
    lftp -p 22 -u $login,$pass sftp://$host << EOF;
    set mirror:use-pget-n 7;
    mirror -c -P1 --log=synctorrents.log $remote_dir/${show}/$local_dir/${show}/;
    quit;
    EOF;
**fi**
cd /cygdrive/h/Rippers/Zeroday/;
dire=( * );
for rip in "${dire[@]%*/}"; do
cd /cygdrive/h/Rippers/Zeroday/${rip}/;
"/cygdrive/c/Program Files/WinRAR/unrar" x *.rar;
"/cygdrive/c/Program Files/get_iplayer/FFmpeg/ffmpeg-2.2.3-win32-static/bin/ffmpeg.exe" -i "${rip}.mkv" -c:v $;
mv ${rip}.mp4 /cygdrive/h/Rippers/MB3_Watch/;
rm -f ${rip}.mkv;
rm -rf /cygdrive/h/Rippers/Zeroday/${rip}/;
done

I'm assuming that this is how your code is supposed to be laid out. You need closing "fi"'s for your "if"'s. Also, check out TLDP, it helped me a lot when I started.

Red
  • 1
  • Red, thanks for this. I'm presuming you've marked the 'fi's for my benefit, and the asterisks should not make it to the final script? Thanks also for the link. Will test the script tomorrow and report back – Macburp Feb 25 '15 at 00:01
  • Also, why do almost all (but not all) lines end in a semi-colon? – Macburp Feb 25 '15 at 00:09
  • Tried this version, it didn't work. Got this error - /home/Paul/scripts/sqlite/syncsqlremoterip6.sh: line 39: warning: here-document at line 24 delimited by end-of-file (wanted `EOF') /home/Paul/scripts/sqlite/syncsqlremoterip6.sh: line 40: syntax error: unexpected end of file – Macburp Feb 25 '15 at 12:50
  • I just reformatted your code to what I thought it was supposed to be and added semicolons to the end of each line in order to make it more easily debugged (bash is technically a logical scripting language). The asterisks on each "fi" were just to bold it for you. There must be a missing end quote or an incomplete command -- try looking into your variables (hard-code a little at a time and work from there). Also, just mashing two scripts together can usually cause some headaches (try doing each piece from the cmd line and see what works). – Red Feb 26 '15 at 16:38