用于重命名和压缩文本输入中的多个文件的Bash脚本

时间:2018-02-05 05:24:53

标签: bash file copy zip

目前我正在使用参数化的方式通过简单的bash脚本进行压缩操作,它采用了三个参数:

#!/bin/bash
#$1 Create zip output directory if not exists
#$2 Go to directory of files that you need to make zip and read input file list
#$3 Zip content name, to write the output as expected filename
mkdir -p $1
cd $2 && cat filelist.txt | zip -@ $1/$3

它正在按照预期完成工作,但我想通过使用第四个参数来改进它,如果有给脚本的第四个和第五个参数,例如$ 4值是“ru-BUCAT”,$ 5值是“ru-XB”,假设filelist.txt包含值:

VIB_ru-BUCAT_A01_D_CIB36P00.xml
VIB_ru-BUCAT_A01_D_CIR30P00.xml
VIB_ru-BUCAT_A01_D_CSG656RB1.xml
VIB_ru-BUCAT_A01_D_HBA23B450E.xml
VIB_ru-BUCAT_A01_D_HBA43T420.xml
VIB_ru-BUCAT_A01_D_HBC86K751S.xml
VIB_ru-BUCAT_A01_D_HBN880751.xml
VIB_ru-BUCAT_A01_D_HCA422120Q.xml
VIB_ru-BUCAT_A01_D_HCA643220Q.xml
VIB_ru-BUCAT_A01_D_HEZ338250.xml
VIB_ru-BUCAT_A01_D_HEZ394300.xml

我需要创建这些文件的副本,包含相同的内容,通过更改文件名$ 4“ru-BUCAT”和$ 5“ru-XB”的子字符串:

VIB_ru-XB_A01_D_CIB36P00.xml
VIB_ru-XB_A01_D_CIR30P00.xml
VIB_ru-XB_A01_D_CSG656RB1.xml
VIB_ru-XB_A01_D_HBA23B450E.xml
VIB_ru-XB_A01_D_HBA43T420.xml
VIB_ru-XB_A01_D_HBC86K751S.xml
VIB_ru-XB_A01_D_HBN880751.xml
VIB_ru-XB_A01_D_HCA422120Q.xml
VIB_ru-XB_A01_D_HCA643220Q.xml
VIB_ru-XB_A01_D_HEZ338250.xml
VIB_ru-XB_A01_D_HEZ394300.xml

然后使用这些文件创建zip文件,创建zip后我不再需要文件再次复制文件,所以只需要将文件放入压缩文件。

业务逻辑定义如下,所以请不要问为什么我要做这种操作。

任何帮助将不胜感激,我尝试了一些事情,但我是bash的新手,所以找不到好的,可读的解决方案。

非常感谢提前!

1 个答案:

答案 0 :(得分:1)

好吧,让我们保持简单。在压缩之前,将每个文件解析为zip并在专用目录中创建具有正确名称的副本。

#!/bin/bash
#$1 Create zip output directory if not exists
#$2 Go to directory of files that you need to make zip and read input file list
#$3 Zip content name, to write the output as expected filename
#$4 RegExp pattern looked for in filenames
#$5 Replace string to use instead of the pattern
mkdir -p $1
cd $2

# Create a temp dir to contains the zip file content
TMP_DIR=tmp_content
mkdir $TMP_DIR

# Parse all the files in filelist and copy them to the temp directory (with the new wname if a match is encountered)
for FILE in $( cat filelist.txt ); do
        NEW_FILE=$(sed -r "s/$4/$5/" <<< $FILE)
        cp -p $FILE $TMP_DIR/$NEW_FILE
done
cd $TMP_DIR
ls -1 | zip -@ $1/$3
rm -rf $TMP_DIR

然后你可以这样调用你的脚本:

$ ./my_script.sh /tmp/so_1/outpd so_1_cnt res.zip ru-BUCAT ru-XB

并有一个这样的列表:

VIB_ru-Bdfdf_A01_D_HEZ394300.xml
VIB_ru-BUCAT_A01_D_CIB36P00.xml
VIB_ru-BUCAT_A01_D_CIR30P00.xml
VIB_ru-BUCAT_A01_D_CSG656RB1.xml
VIB_ru-BUCAT_A01_D_HBA23B450E.xml
VIB_ru-BUCAT_A01_D_HBA43T420.xml
VIB_ru-BUCAT_A01_D_HBC86K751S.xml
VIB_ru-BUCAT_A01_D_HBN880751.xml
VIB_ru-BUCAT_A01_D_HCA422120Q.xml
VIB_ru-BUCAT_A01_D_HCA643220Q.xml
VIB_ru-BUCAT_A01_D_HEZ338250.xml

提供以下zip内容:

  adding: VIB_ru-Bdfdf_A01_D_HEZ394300.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CIB36P00.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CIR30P00.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CSG656RB1.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBA23B450E.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBA43T420.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBC86K751S.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBN880751.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HCA422120Q.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HCA643220Q.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HEZ338250.xml (stored 0%)