V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
DongDongXie
V2EX  ›  程序员

多条指令后台执行怎么同时写一个数组

  •  
  •   DongDongXie · Sep 1, 2019 · 2198 views
    This topic created in 2443 days ago, the information mentioned may be changed or developed.

    背景

    有一堆文件,一些是 txt 文件,一些是 tag 文件,tag 文件里面放的是对应 txt 文件的 MD5 值,现在我需要校验 txt 文件的 MD5 值和 tag 文件里面记载的是否一致,文件比较多,所以我让校验那块儿逻辑在后台执行,如果一个文件校验没通过,我就把该文件名追加到一个名为 errfile 的文件中. 如果我不把文件名保存在文件中,要保存在一个数组里面,后台多个同时执行的任务就会同时写一个数组,这种有没有安全一点的处理办法呢

        for file in `hadoop fs -ls $tmptxtDir|awk '{print $8}'|awk -F'/' '{print $NF}'`
        do
            {
            tmpmd5=`hadoop fs -md5sum $tmptxtDir/$file`
            md5=`hadoop fs -cat $tmpmd5Dir/$file.tag`
            if [ "$tmpmd5" == "$md5" ];then
                hadoop fs -cp $tmptxtDir/$file $finaltxtdataDir/$timestamp
                hadoop fs -cp $tmpmd5Dir/$file.tag $finaltxtdataDir/$timestamp
            else
                echo $file >>errFile.txt
                echo "Failed now. $file::$tmpmd5::$md5"
            fi
            } &
            ((i++))
            echo $i
            val=$(( $i % 10 ))
            if [ $val -eq 0 ]; then wait; fi
        done
    
    3 replies    2019-09-01 21:12:47 +08:00
    5thcat
        1
    5thcat  
       Sep 1, 2019   ❤️ 1
    [Terminal #1] $ lockfile -r 0 /tmp/the.lock
    [Terminal #1] $

    [Terminal #2] $ lockfile -r 0 /tmp/the.lock
    [Terminal #2] lockfile: Sorry, giving up on "/tmp/the.lock"

    [Terminal #1] $ rm -f /tmp/the.lock
    [Terminal #1] $

    [Terminal #2] $ lockfile -r 0 /tmp/the.lock
    [Terminal #2] $
    AX5N
        2
    AX5N  
       Sep 1, 2019
    用队列,处理一个就踢出一个
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   960 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 19:08 · PVG 03:08 · LAX 12:08 · JFK 15:08
    ♥ Do have faith in what you're doing.