Forum Discussion

Arjuna's avatar
2 years ago

To pass a string to python script using Linux/Unix Script parameters from datasource

Hello,

We have a python program which retrieve the customer send/receive details using a customer name, and we are using upload script option (Linux/Unix Script field) to execute the python program and its parameters are passing through a simple shellscript as below,  Parameters we are trying to pass are Customer name and passcode

###Name of the shellscript: cus_inprocess.sh
#/bin/bash
python /usr/local/logicmonitor/agent/local/bin//sps/cust_inprocess_rpi.py $@

 

We are able to pass the customer name if it is a single word i.e SPU but if we try to pass the customer name with space inbetween i.e "SPU UPS" its not working. we have tried with Underscore inbetween i.e "SPU_UPS" its working, but we think underscore also passing as a value and not indicating space between the words while passing to the python script.

Could someone please provide a solution for passing the customer name with space as a parameter to the script. PFA

  • Anonymous's avatar
    Anonymous

    It works on my Python datasources simply by putting the argument in quotes. I'm using argparse instead of sys.argv. Maybe that makes the difference?

    You could move whatever might have spaces to the end, then parse your arguments from left to right, knowing that when you get to the Nth argument, everything after it should be joined back together:

    # ~$ python3 test.py 16353583 abracadabra This is one argument
    
    import sys
    arguments = sys.argv
    print(f"All arguments: {arguments}")
    print(f"Argument 1: {arguments[1]}")
    print(f"Argument 2: {arguments[2]}")
    print(f"Argument 3: {' '.join(arguments[3:])}")