Password

gpg批量解密文件時如何只輸入一次密碼

  • April 12, 2022

我想解密一些 gpg 文件,輸出到一個文件中。但是每次gpg都會詢問密碼。

for i in *.gpg; do echo $i>>~/t; gpg -d --batch $i >>~/t; done

我測試了–multifile 和–batch,那些不是我想要的。

幾種方式:

# gather the password into $P
stty -echo; read -r P; stty echo; 
for i in *.gpg; do printf '%s\n' "$i" >> ~/t; printf '%s' | gpg -d --batch --passphrase-fd 0 "$i" >> ~/t; done

# gather the password into $P
stty -echo; read -r P; stty echo; 
for i in *.gpg; do printf '%s\n' "$i" >> ~/t; gpg -d --batch --passphrase "$P" "$i" >> ~/t; done

d=$(mktemp -d)
# gather the password into a file named `p`
stty -echo; cat > "$d/p"; stty echo
for i in *.gpg; do printf '%s\n' "$i" >> ~/t; gpg -d --batch --passphrase-file "$d/p" 0 "$i" >> ~/t; done
rm -rf "$d"

引用自:https://unix.stackexchange.com/questions/355704