【问题标题】:Bash script to obtain the newest file X in a folder and create a new variable called X+1Bash 脚本获取文件夹中的最新文件 X 并创建一个名为 X+1 的新变量
【发布时间】:2014-09-11 16:36:40
【问题描述】:

我正在尝试在 Bash 脚本中为一系列数据迁移创建一个循环:

在每一步开始时,脚本应该获取文件夹中最新文件的名称

调用“migrationfiles/”并将其存储在变量“migbefore”中并创建一个名为“migbefore+1”的新变量:

    Example:  if the "migrationfiles/" folder contains the following files: 

    migration.pickle1            migration.pickle2            migration.pickle3

变量“migbefore”和migafter应具有以下值:

    migbefore=migration.pickle3
    migafter=migration.pickle4

在每一步结束时,负责进行数据迁移的函数“metl”使用“migbefore”文件加载数据,并创建1个名为“migafter”的新文件并将其存储在"migrationfiles/" 文件夹,因此在这种情况下,将调用创建的新文件:

    "migration.pickle4"

我假装使用的代码如下:

#!/bin/bash

migbefore=0
migafter=0

for y in testappend/*
    for x in migrationfiles/*
    do
         migbefore=migration.pickle(oldest)
         migafter=migbefore+1

    done


do
    metl -m migrationfiles/"${migbefore}" 
         -t migrationfiles/"${migafter}" 
         -s "${y}" 
         config3.yml

done

有谁知道我如何制作第一个循环(在“migrationfiles/”文件夹中搜索最新文件的循环),然后将变量“migafter”的名称指定为“migbefore+1”?

【问题讨论】:

  • 这是在任意迁移文件已经存在的情况下运行的东西(所以一开始最新的文件可能是migration.pickle6)还是总是从0/1开始?
  • 您好 Etan,它只会以 0 和 1 开始一次,并且永远不会返回理想状态。
  • testappend 中的文件控制metl 将被调用多少次?
  • 是的,但是对于主“For”中的每次迭代,metl 只使用相应的文件“${y}”运行一次
  • 看来你的第一个 do 必须移动 6 行:紧跟在 for y in testappend/* 之后。

标签: bash shell for-loop filenames variable-names


【解决方案1】:

我认为这可能会满足您的需求。

#!/bin/bash

count=0
prefix=migration.pickle
migbefore=$prefix$((count++))
migafter=$prefix$((count++))

for y in testappend/*; do
    echo metl -m migrationfiles/"${migbefore}" \
         -t migrationfiles/"${migafter}" \
         -s "${y}" \
         config3.yml

    migbefore=$migafter
    migafter=$prefix$((count++))
done

【讨论】:

    【解决方案2】:

    使用编号备份复制

    真的很难说出你在这里真正想要做什么,以及为什么。但是,您可以通过使用 cp 命令中的--backup 标志来简化生活。例如:

    cp --backup=numbered testappend/migration.pickle migrationfiles/
    

    这将确保您拥有一系列迁移文件,例如:

    migration.pickle
    migration.pickle.~1~
    migration.pickle.~2~
    migration.pickle.~3~
    

    旧版本的序号较大,而最新版本没有序号扩展。这是一个非常简单的系统,但适用于各种用例。 YMMV。

    【讨论】:

      【解决方案3】:
      # configuration:
      path=migrationfiles
      prefix=migration.pickle
      
      # determine number of last file:
      last_number=$( find ${path} -name "${prefix}*" | sed -e "s/.*${prefix}//g" | sort -n | tail -1 )
      
      # put together the file names:
      migbefore=${prefix}${last_number}
      migafter=${prefix}$(( last_number + 1 ))
      
      # test it:
      echo $migbefore $migafter
      

      即使还没有迁移文件,这也应该可以工作。在这种情况下,migbefore 的值只是前缀,并不指向真正的文件。

      【讨论】:

      • 非常感谢 Michael,我与 Etan 提供的脚本一起实现了这个脚本,一切正常。
      • 虽然我刚刚意识到,如果有一个结尾数超过一位(即 10)的 migration.pickle 文件,那么 tail -1 语句将无法正常工作?
      • 你为什么这么认为?从每一行中删除前缀(除了数字之外的所有内容)后,应用数字排序 (-n),然后选择最后一行(数字最高的那一行)。
      • 是的,你是对的,我对 Bash 脚本不是很熟悉,我认为 tail -1 实际上是从名称字符串中获取最后一个元素,非常感谢!
      猜你喜欢
      • 2015-12-06
      • 2014-12-09
      • 2023-04-07
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多