0

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.

Maxxx
  • 3,688
  • 6
  • 28
  • 55
  • I wouldn't expect there to be no output - can you show the exact script you're using to launch the python file as well as the console output? – Peter Gibson Jan 20 '21 at 03:44
  • @PeterGibson sorry but i missed out as i forgot to add the trap statement in the bash script. Apparently i found that the "set -e" is causing $? to not able to be echoed. – Maxxx Jan 20 '21 at 04:27

0 Answers0