BASH中的BASH多个嵌套IF

时间:2011-10-31 10:45:56

标签: bash if-statement ftp nested

我尝试编写的脚本需要执行以下操作

转到特定目录,找到*.mp4扩展名超过30分钟的所有文件,并将其移动到变量中。

变量中的所有文件都需要上传到FTP,并且电子邮件通知会消失。下一个如果需要查看文件并查找扩展名为*1280x720_3500_h32.mp4的所有文件,这些文件将被上传到不同的FTP服务器。

最后,脚本需要将所有文件移动到网络上的单独位置以进行存档。

然后,该过程需要每30分钟运行一次。这就是为什么我添加了一个单独的脚本来运行30分钟的睡眠过程并调用初始脚本。所以实际上我有一个上传脚本和一个睡眠脚本。所以有两个问题浮现在脑海中:

  1. 我不确定脚本正在寻找所有*1280x720_3500_h32.mp4文件的第二个IF - 它是否会上传所有文件名超过30分钟的文件
  2. 脚本将尝试上传已处理过的文件或将跳过文件的竞争条件?
  3. 睡眠脚本:

    #!/bin/bash
    ScripLocal='/Users/jonathan/scripts/Event_Scripts'
    
    while true
    do
    echo "running Upload script: find_newerthan_mv_toFtp_v2.sh" 
    cd $ScripLocal
    . find_newerthan_mv_toFtp_v2.sh
    echo "finished running script: ffind_newerthan_mv_toFtp_v2.sh - next run is in 30 minutes"
    sleep 30m
    done 
    

    上传脚本:

    #!/bin/bash
    #File Origin
    GSPORIGIN='/Volumes/HCHUB/Episode/Watchfolders/Output/Profile_01/'
    #To location on the SAN where the files are being stored
    DESTDIRSAN='/Volumes/HCHUB/Digitalmedia/online_encodes'
    
    # 1st FTP Details
    HOST='ftp.upload01.com'
    USER='xxxx'
    PASSWD='xxxx'
    DESTDIR="/8619/_!/intlod/upload/"
    
    #2nd FTP details
    HOST01='ftp.upload02.com'
    USER01='xxxx'
    PASSWd01='xxxx'
    
    
    cd $GSPORIGIN
    for file in `find . -type f -name "*.mp4" -mmin -30`
    do 
    echo $file
    if [ -f $file ] ; then
    echo "Uploading to FTP 01"
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    cd $DESTDIR
    mput $file
    EOT
    echo "$file has been copied to FTP 01" | mail -s "$file has been copied to FTP 01 in      Directory $DESTDIR"  xxxx@xxxx.com xxxx@xxxx.com xxxx@xxxx.com; 
    if [[ $file == *1280x720_3500_h32.mp4 ]] ; then
    echo "Uploading FTP 02 version"
    ftp -n -v $HOST01 << EOT
    ascii
    user $USER01 $PASSWD01
    prompt
    mput $file
    EOT
    echo "$file has been copied to FTP 02" | mail -s "$file has been copied to FTP 02"   xxxx@xxxx.com; 
    fi
    echo "moving files to SAN $DESTDIRSAN"
    mv -v $file $DESTDIRSAN
    else exit 1
    fi
    done
    

3 个答案:

答案 0 :(得分:3)

如果您担心比赛条件,请不要使用FTP。 FTP是一个非常非常糟糕的数据交换协议 - 当出现问题时它不会给你正确的错误消息,退出代码总是0(即使某些东西不起作用),也没有防止重复或错过的保护上传等。

请改用rsync。 rsync将开始以临时名称写入目标文件(因此接收器在上传完成之前不会看到它),它可以恢复上传(对大文件很重要),它使用校验和来确保上传正确并且如果出现任何错误,它将失败,并显示非空错误代码。

最重要的是,它可以使用ssh来保护连接,因此您不必通过网络发送纯文本密码(即使在Intranet中,这也是一个非常糟糕的主意)。

rsync具有强大的过滤选项(上传内容),并且可以归档任何已处理的文件,因此您不会上传两次。它甚至可以检测用户何时第二次上传文件并失败(或者如果文件100%相同则默默跳过)。

要确保您不会同时运行两次上传,请使用锁定:

mkdir .lock || { echo "Can't lock - process is already running" ; exit 1 ; }
trap "rmdir .lock" EXIT

您可能还想看看FTAPI,它可以完成所有这些以及更多。

答案 1 :(得分:2)

使用crontab,而不是休眠线程。 sleep容易失败,无论如何nohup ping都很麻烦。

如果您可以,请使用rsync而不是自己查找更改。此外,您不需要像使用rsync一样编写ftp脚本。

答案 2 :(得分:0)

得到了脚本的python变体(感谢Daniel !!)。

#!/usr/bin/python
# Created in a rush, on the 31st October

# HALLOWEEN SPECIAL - QUICK AND DIRTY
import os
import shutil
from ftplib import FTP

# File Origin
ENCODE_OUTPUT='xxxxxxx'
# To location on the SAN where the files are being cached on the network
CACHE_DIR='xxxxxxxx'

#ENCODE_OUTPUT='encode/'
#CACHE_DIR='cache/'

#FTP01 FTP Details
FTP01_HOST='upload.FTP01.com'
FTP01_USER='xxxxx'
FTP01_PASSWD='xxxxxxx'
FTP01_DESTDIR="dir/dir/"

#Fingerprinting FTP details
FINGERPRINT_HOST='ftp.ftp02.com'
FINGERPRINT_USER='xxxxx'
FINGERPRINT_PASSWD='xxxxxx'
FINGERPRINT_DEST_DIR='/'

dirList = os.listdir(ENCODE_OUTPUT)
dirList.sort()

# Move the files out of the encoded located and into the cache dir
for video_file in dirList:
            print "Moving file %s to %s" % (video_file, CACHE_DIR)
        shutil.move(ENCODE_OUTPUT + video_file, CACHE_DIR)

        # Upload the file to FTP01
        print "Logging into %s FTP" % FTP01_HOST
        ftp = FTP(FTP01_HOST)
        ftp.login(FTP01_USER, FTP01_PASSWD)
        ftp.cwd(FTP01_DESTDIR)

        print "Uploading file %s to %s" % (video_file, FTP01_HOST+FTP01_DESTDIR)
        ftp.storbinary('STOR ' + FTP01_DESTDIR+video_file, open(CACHE_DIR+video_file, "rb"), 1024)

        # Close the connection
        ftp.close

        # If the file ands in "1280x720_3500_h32.mp4" copy it to the finger printing service
            if video_file[-21:]=="1280x720_3500_h32.mp4":
                # Upload the file to the fingerprinting service
                print "File is HD. Logging into %s FTP" % FINGERPRINT_HOST
                ftp = FTP(FINGERPRINT_HOST)
                ftp.login(FINGERPRINT_USER, FINGERPRINT_PASSWD)
                ftp.cwd(FINGERPRINT_DEST_DIR)

                print "Uploading file %s to %s" % (video_file, FINGERPRINT_HOST)
                ftp.storbinary('STOR ' + FINGERPRINT_DEST_DIR+video_file, open(CACHE_DIR+video_file, "rb"), 1024)

                ftp.close