Shell Script If File Exists

Discussion in 'Programming/Scripts' started by bschultz, Apr 11, 2010.

  1. bschultz

    bschultz Member

    I'm trying to write a script to:
    1 - download several mp3's
    2 - if they downloaded, rename the files
    3 - if they didn't download...exit script
    4 - if they did download, scp them to another server (I haven't written this part yet...wanted to get the first part working first.)


    Here's what I have so far:

    Code:
    #!/bin/sh
    set -e
    
    cd /var/www/testing/
    
    wget -nd -r -l1 --no-parent --no-passive-ftp -A.mp3 'ftp://username:password@IP'
    
    if [ -e /var/www/testing/MIXWX.mp3.1 ];
    then
     echo `rm Mix\ 2p.mp3`
     echo `cp MIXWX.mp3.1 Mix\ 2p.mp3`
     echo `rm MIXWX.mp3`
     echo `rm MIXWX.mp3.1`
    
    else
       echo ``
    
    if [ -e /var/www/testing/REALWX.mp3.1 ];
    then
       echo `rm Real\ 2p.mp3`
       echo `cp REALWX.mp3.1 Real\ 2p.mp3`
       echo `rm REALWX.mp3`
       echo `rm REALWX.mp3.1`
    
    else
       echo ``
    
    if [ -e /var/www/testing/TALKWX.mp3.1 ];
    then
      echo `rm Talk\ 2p.mp3`
      echo `cp TALKWX.mp3.1 Talk\ 2p.mp3`
      echo `rm TALKWX.mp3`
      echo `rm TALKWX.mp3.1`
    
    else
       echo ``
    
    
    if [ -e /var/www/testing/WMIS.mp3.1 ];
    then
      echo `rm Talk\ 2p.mp3`
      echo `cp WMIS.mp3.1 WMIS\ 2p.mp3`
      echo `rm WMIS.mp3`
      echo `rm WMIS.mp3.1`
    
    else
       echo ``
    
    if [ -e /var/www/testing/Current.mp3 ];
    then
      echo `rm Current.mp3`
    else
       echo ``
    
    exit
    
    
    I'm getting this error:

    Any ideas what I'm doing wrong?

    Thanks!
     
  2. Mark_NL

    Mark_NL New Member

    why are you echo ``-ing in every else?
     
  3. bschultz

    bschultz Member

    don't delete, don't rename, don't do anything if the file didn't download
     
  4. Mark_NL

    Mark_NL New Member

    so that would be the same as not defining it at all ;-)

    Code:
    #!/bin/bash
    
    cd /var/www/testing/
    
    wget -nd -r -l1 --no-parent --no-passive-ftp -A.mp3 'ftp://username:password@IP'
    
    if [ -e MIXWX.mp3.1 ];
    then
     echo `rm Mix_2p.mp3`
     echo `cp MIXWX.mp3.1 Mix_2p.mp3`
     echo `rm MIXWX.mp3`
     echo `rm MIXWX.mp3.1`
    fi
    
    if [ -e REALWX.mp3.1 ];
    then
       echo `rm Real_2p.mp3`
       echo `cp REALWX.mp3.1 Real_2p.mp3`
       echo `rm REALWX.mp3`
       echo `rm REALWX.mp3.1`
    fi
    
    if [ -e TALKWX.mp3.1 ];
    then
      echo `rm Talk_2p.mp3`
      echo `cp TALKWX.mp3.1 Talk_2p.mp3`
      echo `rm TALKWX.mp3`
      echo `rm TALKWX.mp3.1`
    fi
    
    if [ -e WMIS.mp3.1 ];
    then
      echo `rm Talk_2p.mp3`
      echo `cp WMIS.mp3.1 WMIS_2p.mp3`
      echo `rm WMIS.mp3`
      echo `rm WMIS.mp3.1`
    fi
    
    if [ -e Current.mp3 ];
    then
      echo `rm Current.mp3`
    fi
    
    exit
     
  5. bschultz

    bschultz Member

    That worked great...Thanks!

    Now, on to work on the scp part.
     
  6. bschultz

    bschultz Member

    Is there a way to write this so that it retries to download the file after a minute if the remote server isn't reachable?
     

Share This Page