python - String comparing between output system command with defined variable -
i tried compare output of command line variable that's defined logic throw false instead of true.
$ sudo hdparm -i /dev/sda | grep serial | awk '{print $3}' 6ra3x34p
in python:
hdserial="6ra3x34p" cmd1="sudo hdparm -i /dev/sda | grep serial | awk '{print $3}'" output = subprocess.check_output(cmd1, shell=true) def check_serial(string): if string != hdserial: print '\nquitting..' sys.exit() check_serial(output)
why comparison failing?
the output of command contains trailing newline. should remove using str.strip
or str.rstrip
:
output = subprocess.check_output(cmd1, shell=true).strip()
Comments
Post a Comment