Simple Python equivalent for the shell command

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


I  remembered that I might need to add an else condition if the emp_num
does not exist , so re sending the updated code


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]
        else:
                print("emp_unum does not exist")
                return False
    return emp_unum

PS :  [Edited the above code with else condition]

Regards,
Ganesh


On Mon, Nov 28, 2016 at 8:38 PM, Ganesh Pal <ganesh1pal at gmail.com> wrote:

>
>
> 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