How to find out if the password entered by user is correct (in the command encrypted with OpenSSL)? I want to run another command if the password is successful. Voila my code :
#!/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
CodePudding user response:
Yes you catch the exit code if openssl fail to decrypt the file .
OPTION 1 : you can use $?
#!/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
OPTION 2 : you can embed the execution of the command in the if
clause
#!/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
OPTION 3 : you can use ||
between openssl and a block that will executed only if openssl generate a error
#!/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
CodePudding user response:
With all respect for the @EchoMike44 suggestion, have you tried this?
#!/bin/bash
openssl aes-128-cbc -d -in config.py.aes128 -out config.py && \
./doSomething.py || \
exit 1
If you don't need detailed messages in the output, so this will work with the same logic but in short syntax.
I mean if the ExitCode of the openssl ...
command was 0 (successful) then will run ./doSomething.py
otherwise it will exit from the bash script with the ExitCode of 1
.