如何检查用户输入的密码是否正确(在使用OpenSSL加密的命令中)



如何查明用户输入的密码是否正确(在使用OpenSSL加密的命令中(?如果密码成功,我想运行另一个命令。Voila我的代码:

#!/bin/bash
#decryption operation
openssl aes-128-cbc -d -in config.py.aes128 -out config.py
#run another command if password is correct
./doSomething.py 

尊重@EchoMike44的建议,您尝试过吗?

#!/bin/bash
openssl aes-128-cbc -d -in config.py.aes128 -out config.py && 
./doSomething.py || 
exit 1

如果输出中不需要详细的消息,那么这将使用相同的逻辑,但语法较短。

我的意思是,如果openssl ...命令的ExitCode为0(成功(,则将运行./doSomething.py,否则它将以1的ExitCode退出bash脚本。

是的,如果openssl无法解密文件,则会捕获退出代码。

选项1:您可以使用$?

#!/bin/bash
# create a temporary file for output errors
ERR_OUTPUT=$(mktemp)
openssl aes-128-cbc -d  -in config.py.aes128 -out  config.py    >"${ERR_OUTPUT}" 2>&1
EXIT_CODE=$?
if [  ${EXIT_CODE} -ne 0 ]
then
echo ERROR decryption failed
cat "${ERR_OUTPUT}"
rm -f "${ERR_OUTPUT}"
exit ${EXIT_CODE}
fi
rm -f "${ERR_OUTPUT}"
./doSomething.py

OPTION2:您可以在if子句中嵌入命令的执行

#!/bin/bash
# create a temporary file for output errors
ERR_OUTPUT=$(mktemp)
if ( ! openssl aes-128-cbc -d  -in config.py.aes128 -out  config.py  >"${ERR_OUTPUT}" 2>&1 )
then
echo ERROR decryption failed
cat "${ERR_OUTPUT}"
rm -f "${ERR_OUTPUT}"
exit 1
fi
rm -f "${ERR_OUTPUT}"
./doSomething.py

选项3:您可以在openssl和只有在openssl生成错误时才会执行的块之间使用||

#!/bin/bash
# create a temporary file for output errors
ERR_OUTPUT=$(mktemp)
openssl aes-128-cbc -d  -in config.py.aes128 -out  config.py  >"${ERR_OUTPUT}" 2>&1 || (
echo ERROR decryption failed
cat "${ERR_OUTPUT}"
rm -f "${ERR_OUTPUT}"
exit ${EXIT_CODE}
)
rm -f "${ERR_OUTPUT}"
./doSomething.py

最新更新