Simple Python equivalent for the shell command

Ganesh Pal ganesh1pal at gmail.com
Mon Nov 28 10:08:41 EST 2016


I  was trying  to write a function that will return me the unique number
associated with each employee id.The command has the output in the below
pattern

Linux-Box-1# employee_details ls
List of names:
100910bd9 s7018
100d60003 s7019
110610bd3 s7020
100d60002 s7021


Linux-Box-1# employee_details ls | grep "s7020" | awk '{print $1}'
100d60003

It's a one liner in Shell  :)


I tried converting  the same  in the python style , Any better suggestion
and loop holes in the below program

def get_unique_number(str(emp_id)):
    """ Return the unique number associated with each employee id """
    out, err, rc = run("employee_details ls", timeout=600)
    emp_unum=""
    if rc != 0:
        return False

    for line in out.split('\n'):
        if emp_id in line:
           emp_unum = line.split()[0]
    return emp_unum

I am on Python 2.7 and Linux OS

Regards,
Ganesh



More information about the Python-list mailing list