我是编程脚本方面的新手-。 我在一个目录中有很多 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,
里面有: - “几何练习.doc” - “几何.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 if variable greater than”的内容。
查看 bash tag wiki有关调试和询问代码的更多有用提示。
您会发现的内容包括:
-
if
必须小写 then
前需要换行或分号- 要查看变量是否大于,请使用
[[ $a -gt 2 ]]
. - 要查看数组元素是否不相等,请使用
[[ ${inside[0]} != "remora.txt" ]]
- 管道产生子外壳。使用
while read ...; do ...; done < <(somecommand)
相反。
关于linux - 用于替换 zip 文件名称的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430545/