Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 循环脚本,直到文件大小相等一分钟_Bash_Loops_If Statement_Exists - Fatal编程技术网

Bash 循环脚本,直到文件大小相等一分钟

Bash 循环脚本,直到文件大小相等一分钟,bash,loops,if-statement,exists,Bash,Loops,If Statement,Exists,我必须每天在特定时间运行脚本。该脚本用于转换特定文件夹中的大文件(约2GB)。问题是,我的同事并非每天都会在时间到来之前将文件放入文件夹,以cronjob的形式编写 请帮助我在脚本中添加命令或为以下内容编写第二个脚本: 检查文件夹中是否存在该文件 如果上一个操作为true,请每分钟检查一次文件大小。(我希望避免转换仍然输入的大文件) 如果文件大小在2分钟内保持不变,请启动脚本进行转换 到目前为止,我向大家介绍了剧本的重要内容: cd /path-to-folder for $i in *.mpg

我必须每天在特定时间运行脚本。该脚本用于转换特定文件夹中的大文件(约2GB)。问题是,我的同事并非每天都会在时间到来之前将文件放入文件夹,以cronjob的形式编写

请帮助我在脚本中添加命令或为以下内容编写第二个脚本:

  • 检查文件夹中是否存在该文件
  • 如果上一个操作为true,请每分钟检查一次文件大小。(我希望避免转换仍然输入的大文件)
  • 如果文件大小在2分钟内保持不变,请启动脚本进行转换
  • 到目前为止,我向大家介绍了剧本的重要内容:

    cd /path-to-folder
    for $i in *.mpg; do avconv -i "$i" "out-$i.mp4" ; done
    
    10倍的帮助

    注释后的新代码:

    文件夹中有一个文件

    #! /bin/bash
    
    
    cdate=$(date +%Y%m%d)
    dump="/path/folder1"
    base=$(ls "$dump")
    
    if [ -n "$file"]
    then
        file="$dump/$base"
        size=$(stat -c '%s' "$file")
        count=0
        while sleep 10
        do
            size0=$(stat -c '%s' "$file")
            if [ $size=$size0 ]
            then $((count++))
                 count=0
            fi
            if [ $count = 2 ]
            then break
            fi
        done
        # file has been stable for two minutes. Start conversion.
    
    CONVERSION CODE
    
    fi
    
    终端中的消息:可能是错误

    script.sh: 17: script.sh: arithmetic expression: expecting primary: "count++"
    

    根据稍加修改的要求(在注释中描述),并假设文件名不包含空格、换行符或其他类似的笨拙字符,则可以使用:

    dump="/work/daily/dump"                 # folder 1
    base=$(ls "$dump")
    
    if [ -n "$file" ]
    then
        file="$dump/$base"
        ...code as before...
        # File has been stable for 2 minutes - start conversion
        dir2="/work/daily/conversion"       # folder 2
        file2="$dir2/$(basename $base .mpg).xyz"
        convert -i "$file" -o "$file2"
        mv "$file" "/work/daily/originals"  # folder 3
        ncftpput other.coast.example.com /work/daily/input "$file2"
        mv "$file2" "/work/daily/converted" # folder 4
    fi
    
    如果文件夹中没有任何内容,则进程退出。如果希望它等到有文件要转换,则需要围绕文件测试进行循环:

    while file=$(ls "$dump")
          [ -z "$file" ]
    do sleep 60
    done
    
    这使用了一个鲜为人知的shell循环特性;可以在控件中堆叠命令,但控制循环的是最后一个命令的退出状态


    根据稍加修改的要求(在注释中描述),并假设文件名不包含空格、换行符或其他类似的笨拙字符,则可以使用:

    dump="/work/daily/dump"                 # folder 1
    base=$(ls "$dump")
    
    if [ -n "$file" ]
    then
        file="$dump/$base"
        ...code as before...
        # File has been stable for 2 minutes - start conversion
        dir2="/work/daily/conversion"       # folder 2
        file2="$dir2/$(basename $base .mpg).xyz"
        convert -i "$file" -o "$file2"
        mv "$file" "/work/daily/originals"  # folder 3
        ncftpput other.coast.example.com /work/daily/input "$file2"
        mv "$file2" "/work/daily/converted" # folder 4
    fi
    
    如果文件夹中没有任何内容,则进程退出。如果希望它等到有文件要转换,则需要围绕文件测试进行循环:

    while file=$(ls "$dump")
          [ -z "$file" ]
    do sleep 60
    done
    

    这使用了一个鲜为人知的shell循环特性;您可以在控件中堆叠命令,但控制循环的是最后一个命令的退出状态。

    好吧,我终于编写了一些工作代码,如下所示:

    #!/bin/bash
    
    cdate=$(date +%Y%m%d)
    folder1="/path-to-folder1"
    
    cd $folder1
    
    while file=$(ls "$folder1")
          [ -z "$file" ]
    do sleep 5 && echo "There in no file in the folder at $cdate."
    done
    
    echo "There is a file in folder at $cdate"
    size1=$(stat -c '%s' "$file")
    echo "The size1 is $size1 at $cdate"
    size2=$(stat -c '%s' "$file")
    echo "The size2 is $size2 at $cdate"
    if [ $size1 = $size2 ]
    then
    echo "file is stable at $cdate. Do conversion."
    
    下一行是循环相同脚本的正确行吗??? 下面注释后面的正确代码是
    好的,我最终制作了一些工作代码,如下所示:

    #!/bin/bash
    
    cdate=$(date +%Y%m%d)
    folder1="/path-to-folder1"
    
    cd $folder1
    
    while file=$(ls "$folder1")
          [ -z "$file" ]
    do sleep 5 && echo "There in no file in the folder at $cdate."
    done
    
    echo "There is a file in folder at $cdate"
    size1=$(stat -c '%s' "$file")
    echo "The size1 is $size1 at $cdate"
    size2=$(stat -c '%s' "$file")
    echo "The size2 is $size2 at $cdate"
    if [ $size1 = $size2 ]
    then
    echo "file is stable at $cdate. Do conversion."
    
    下一行是循环相同脚本的正确行吗??? 下面注释后面的正确代码是
    请注意,此解决方案将在Linux中工作,但在其他操作系统中,
    stat
    可能使用不同的选项。例如,在FreeBSD和OSX中,您需要使用
    stat-f“%z'$file”
    来设置
    size
    变量。另外,对于
    if
    ,通常最好使用bash内置的
    [[
    操作符,而不是旧的Bourne
    /bin/[
    。更好的性能,更好的习惯。操作系统是Ubuntu12.04,但我必须在行
    #文件稳定2分钟后立即将命令放入转换脚本中吗?开始转换
    ,或者我必须编写一个命令来运行另一个脚本?@ispasov-你可以做任何你想做的事情,要么向该代码中添加命令,要么这称为外部代码。由于你的问题中没有发布任何代码,Jonathan无法在他发布的脚本摘录中真正满足你的具体要求。他的回答肯定符合你在问题中发布的内容。你在我发布我给出的答案45分钟后添加了代码片段。从片段中看不清楚您已经给出了您关心更改的
    .mpg
    文件中的哪些文件。您需要从答案脚本中吸取想法,并将其应用到您的情况中。我们提供帮助;我们尽量不照顾他人。@ghoti,
    [
    是bash内置--
    类型-a[
    请注意,此解决方案将在Linux中运行,但在其他操作系统中,
    stat
    可能会使用不同的选项。例如,在FreeBSD和OSX中,您需要使用
    stat-f“%z'”$file”
    来设置
    大小
    变量。此外,通常最好使用bash的内置
    [[
    运算符,用于
    if
    而不是旧的Bourne
    /bin/[
    。更好的性能,更好的习惯。操作系统是Ubuntu12.04,但我必须在行
    #文件稳定2分钟后立即将命令放入转换脚本中吗?开始转换
    ,或者我必须编写一个命令来运行另一个脚本?@ispasov-你可以做任何你想做的事情,要么向该代码中添加命令,要么这称为外部代码。由于你的问题中没有发布任何代码,Jonathan无法在他发布的脚本摘录中真正满足你的具体要求。他的回答肯定符合你在问题中发布的内容。你在我发布我给出的答案45分钟后添加了代码片段。从片段中看不清楚您已经给出了您关心更改的
    .mpg
    文件中的哪些文件。您需要从答案脚本中吸取想法,并将其应用到您的情况中。我们提供帮助;我们尽量不照顾他人。@ghoti,
    [
    是bash内置--
    类型-a[
    FYI:对于我来说,使用
    bash
    3.2,命令序列
    count=0;:$(count++);echo$count;((count++);echo$count$($count+3))$(count+3))
    回显1、2、5、5。也就是说,这两种符号都有效。一种可能是您毕竟没有使用
    bash
    ——通常,shebang不使用空格,但我不确定这是否重要。如果[$size=$size0],您在
    中确实存在问题。
    ;您必须在测试中分离参数:
    如果[$size=$size0]
    。这并不能解释报告的错误。您似乎缺少一个
    else
    子句。您可能需要使用
    :$(count++)
    then
    子句中。@JonathanLeffler在您的帮助下,我用工作代码添加了新的答案。我还有最后一个问题,关于在文件存在的情况下循环脚本的正确方法。有时间的话,请参阅答案2。10xFYI:对于我来说,使用
    bash
    3.2,命令序列
    count=0;:$((count++);echo$count;((count++);echo