![](/img/trans.png)
[英]Bash Script to group multiple file by file name and archiving into one zip file and then finally moving to another dirrectory
[英]bash script to replace the name of a zip file
我是编程脚本方面的新手。 我的目录中有很多zip文件。 我想提取它们,用正确的扩展名将内部文件的名称替换为zip文件。 如果有多个文件,则报告错误,如果内部包含“ remora.txt”,则报告错误。 文件“ remora.txt”是zip的一个ini文件,我不再使用它,但是在我的很多zip文件中。
示例1. ZIp文件:maths.zip,
它的内部具有:-“ maths.doc教程”-“ remora.txt”
行动:因此,脚本应删除或弃用“ remora.txt”,并提取名为maths.doc的“ tutorial in maths.doc”。
示例2。ZIp文件:geo.zip,
里面有:-“ excersices for geometry.doc”-“ geometry.doc”-“ remora.txt”项目
行动:它应该放在“我在geo.zip中发现了多个文件”
我正在使用linux,Ubuntu 12
我已经完成了此脚本,但是无法正常工作。
#!/bin/bash
#
# Linux Shell Scripting Tutorial 1.05r3, Summer-2002
#
for archive in *.zip # First I read the zip file
do
((i++))
unzip -Z1 $archive | while read line; # I read all the files in the ZIP
do
line=( ${line//,/ } )
inside[$a]=("${line[@]}") # Here I assigne the name of the file to an array
((a++))
done
If ( $a > 2) then
echo " Too much files in file $archive "
fi
If ($a <= 2)
then
if (inside[0]!= "remora.txt")
then unzip -p $archive > $(printf "%s" $archive).doc
fi
if (inside[1]!= "remora.txt")
then unzip -p $archive > $(printf "%s" $archive).doc
fi
fi
done
尝试逐步编写脚本。 与其编写20条语句,然后一次尝试调试所有语句,不如一次编写一条语句并进行测试以确保它在编写下一条语句之前进行测试。
如果您运行例如
If ( $a > 2) then
echo " Too much files in file $archive "
fi
就其本身而言,您会发现它不起作用。 然后,您可以更具体地了解问题所在,然后可以在Google或Stackoverflow上查找“ bash,如果变量大于”。
查看bash标签wiki,以获得有关调试和询问代码的更多有用提示。
您会发现的内容包括:
if
必须是小写 then
[[ $a -gt 2 ]]
。 [[ ${inside[0]} != "remora.txt" ]]
while read ...; do ...; done < <(somecommand)
使用while read ...; do ...; done < <(somecommand)
while read ...; do ...; done < <(somecommand)
while read ...; do ...; done < <(somecommand)
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.