【问题标题】:Bash script to String concat two variables and do File compareBash脚本到String concat两个变量并进行文件比较
【发布时间】:2020-07-12 22:03:41
【问题描述】:

我想要实现的是,删除 Src_Dir1 和 Src_Dir2 中存在的相同文件名(文件名+modfiedtimestamp)

首先我尝试将所有文​​件名分别部署到 tempa(Src_Dir1) 和 tempb(Src_Dir2)。

下面是源码目录的截图。

存档里面的文件是这样的,外面的文件也很少..

所以,最初我想处理 Archive(SRC_Dir1) 内部的文件,然后处理 Archive(SRC_Dir2) 外部的文件,我想要做的是使用 while 循环读取每个文件名和字符串 concat 并修改时间戳(mtime)并输入到 tempc(例如,它应该像 AirTimeActs_2018-12-03.csv+2019-01-24 14:41:53.000000000 -0500 = AirTimeActs_2018-12-03.csv_2019-01- 24 14:41:53.000000000 -0500 这就是它应该如何为 Archive(SRC_Dir1) 中的每个文件名生成 tempc 文件。这就是我被困在字符串 concat 变量下的地方关于如何继续的部分。请帮助我编写代码,希望我可以理解。

重要

(真的很感激,如果你帮我解决我在这里没有提到但尚未实现的代码的扩展 - > 必须实现相同的代码(我正在尝试为 tempa 执行此代码,我也想为 tempb 执行此代码并将其命名为 tempd),然后在 tempc 和 tempd 之间进行文件数据比较)如果有任何类型相同的数据文件名,则删除存在于Src_Dir2中的文件,如果没有相同的数据文件名,则什么都不做。)

 #!/bin/bash
    Src_Dir1=path/Airtime_Activation/Archive
    Src_Dir2=path/Airtime_Activation/

    find "$Src_Dir1" -maxdepth 1 -name "*.xlsx" -o -name "*.csv" | sed "s/.*\///" > -print>path/Airtime_Activation/temp_a
    find "$Src_Dir2" -maxdepth 1 -name "*.xlsx" -o -name "*.csv" | sed "s/.*\///" > -print>path/Airtime_Activation/temp_b

    echo 'phase1'
    cat path/Airtime_Activation/temp_a | while read file; 
    do
        echo 'phase1.5'
        echo "$file"
        echo 'phase2'
        mtime=$(stat -c '%y' $file)
        Full_name=${file}_${mtime}
        echo "$Full_name" >> path/Airtime_Activation/temp_c
        echo 'phase3'

    done

【问题讨论】:

  • 尝试shellcheck.net 验证您的脚本。
  • 我不完全了解您想要实现的目标。请edit您的问题并添加一个示例:两个目录中都有哪些文件?您要删除哪些,应该保留哪些?将此描述为文本,而不是(仅)作为屏幕截图。当您使用示例数据运行脚本时,请说明您希望在temp_a 等中获得什么。
  • @Bodo - 我已经解释的更详细了,请立即查看
  • @Jetchisel - 我已经在 shellcheck.net 中进行了更改,并且没有错误,但代码似乎仍然无法正常工作,位于 $Full_name >>
  • 查看此处mywiki.wooledge.org/BashFAQ/036,了解如何比较您的文件

标签: bash variables unix string-comparison string-concatenation


【解决方案1】:
#!/bin/bash
    Src_Dir1=path/Airtime_Activation/Archive
    Src_Dir2=path/Airtime_Activation/

    find "$Src_Dir1" -maxdepth 1 -name "*.xlsx" -o -name "*.csv" | sed "s/.*\///" > -print>path/Airtime_Activation/temp_a
    find "$Src_Dir2" -maxdepth 1 -name "*.xlsx" -o -name "*.csv" | sed "s/.*\///" > -print>path/Airtime_Activation/temp_b

    echo 'phase1'
    cat path/Airtime_Activation/temp_a | while read file; 
    do
        echo 'phase1.5'
        echo "$file"
        echo 'phase2'
        mtime=$(stat -c '%y' $file)
        Full_name=${file}_${mtime}
        echo "$Full_name" >> path/Airtime_Activation/temp_c
        echo 'phase3'

    done

cat /path/Airtime_Activation/temp_b | while read file
#while IFS="" read -r -d $'\0' file; 

do
    #echo "$file"
    echo 'phase2'
    mtime=$(stat -c '%y' $Src_Dir2/$file)
    Full_name=${file}_${mtime}
    echo "$Full_name" >> path/temp_d
    echo 'phase3'

done


#file compare and delete old files from outisde archive
grep -Ff temp_d temp_c > path/Airtime_Activation/temp_e
cat path/Airtime_Activation/temp_e | while read file
#while IFS="" read -r -d $'\0' file; 

do
    #echo "$file"
    echo 'phase2'
    echo "${file%_*}"
    rm $Src_Dir2/${file%_*}
    echo 'phase3'

done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多