我有一个脚本,它使用以下命令对某些文件夹中的特定 zip 和\或 tar.gz 存档执行大量提取:
unzip -o "$zip_path" -d "$destination_folder"
不幸的是,当存档受密码保护时,脚本会停止并等待密码输入。
有没有什么办法可以省略密码输入阶段而不中断脚本运行?
附言无需提取受密码保护的文件。仅省略此文件。 像这样的东西:
if "$zip_path" [ determine that archive is password-protected ]; then
echo "Password-protected"
elif "continue script execution"
fi
最佳答案
对于 zip 文件,您可以使用 -P
标志指定虚拟(错误)密码。
对于未加密的文件,它将被忽略,
对于加密文件,您将收到警告并且文件将被跳过。例如:
unzip -P x -o "$zip_path" -d "$destination_folder"
对于 tar 文件,加密不是标准功能,所以我不确定您的意思。您可以尝试将标准输入从 /dev/null
重定向到脚本,使其无法读取并跳到下一个文件:
tar -xvzf "$tgz_path" --directory "$destination_folder" < /dev/null
如果这不起作用,那么您可以尝试 expect
。
关于Bash 脚本跳过密码保护文件的提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593758/