Bash script to skip extraction of password protected archives

放肆的年华 提交于 2019-12-07 14:16:34

问题


I have a script, which performs mass extraction of specific zip and\or tar.gz archives in some folders using command:

unzip -o "$zip_path" -d "$destination_folder"

Unfortunately, when archive is password-protected, script stops and waiting for password input.

Is there any way to omit password entering stage to not interrupt script running?

P.S. There is no need to extract password-protected files. Only omit this archives. Something like:

if "$zip_path" [ determine that archive is password-protected ]; then
echo "Password-protected"
elif "continue script execution"
fi

回答1:


For zip files, you can specify a dummy (wrong) password with the -P flag. For non-encrypted files it will be ignored, for encrypted files you will get a warning and the file will be skipped. For example:

unzip -P x -o "$zip_path" -d "$destination_folder"

For tar files, encryption is not a standard feature, so I'm not sure you what you mean. You could try to redirect stdin to the script from /dev/null to make it fail to read and skip over to the next file:

tar -xvzf "$tgz_path" --directory "$destination_folder" < /dev/null

If this doesn't work, then you can try expect.



来源:https://stackoverflow.com/questions/40593758/bash-script-to-skip-extraction-of-password-protected-archives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!