So I wrote a python script, login_email.py for reading emails with the "imaplib" and "email" module.
import sys
import imaplib
import email
def login(user,pwd):
mail = imaplib.IMAP4_SSL('imap.gmail.com')
try:
mail.login(user,pwd)
except imaplib.IMAP4.error:
sys.exit(1)
if __name__ == "__main__":
mail_usr = "tester@gmail.com"
mail_pwd = sys.argv[1] #password will be provided as an argument in the bash
login(mail_usr,mail_pwd)
And i want to simulate a failed login process so if the login fails, sys.exit(1) will trigger. In my bash script:
function catch() {
echo "Error"
}
trap 'catch $? $LINENO $BASH_COMMAND' ERR
set -e
password="12345"
python login_email.py $password
echo $?
However, when i run the script, i get no output as I was expecting $? to be 1 since I defined sys.exit(1) in the python script. What went wrong?
Edit I just found out that the "set -e" that i used to abort the script in case of any script error is causing "$?" to be not echoable.