Posts: 3
Threads: 1
Joined: Apr 2024
I am a definite newbie to python. I've written some pretty basic programs that use shell scripts to pass user supplied arguments to my python programs. The problem I have now is that several of my parameters have spaces (for example an address line might read something like '1116 E. State St.'). The python program just parses the list of arguments each with one word. So my address line would be 4 arguments. I've tried single quotes, double quotes, three single ('''1116...'''), three double ("""1116..."""), and combinations of each of those. How do I pass my parameters as needed?
My shell program is attached.
This is the error I get:
(v398) -bash5.2 /home/opensource/pysrc/tst/RepEmploymentVerification $ rep_employment_verification.sh tst 1 kaustin Richard Green 123-45-6789 He Him Cutco ''''State Street'''' Olean NY 14760 Karen 585-968-1497 Jan12020 N Feb1202 N Feb22015 00004
---------------------------
** tst (tst) ENV LOADED **
Traceback (most recent call last):
File "/home/opensource/pysrc/tst/RepEmploymentVerification/./rep_employment_verification2.py", line 474, in <module>
scriptName, environment, letterType, userName, firstName, lastName, socSecNumber, pronoun, pronoun2, companyName, address, city, state, zip, attention, faxNumber, lastActiveDate, lastActiveDateFlag, lastOrderDate, lastOrderDateFlag, contractDate, company = argv
ValueError: too many values to unpack (expected 22)
Posts: 6,800
Threads: 20
Joined: Feb 2020
You need to post the python program, at least the part that processes the command line arguments.
Posts: 3
Threads: 1
Joined: Apr 2024
well, my python program only accepts the arguments and then uses them?
# _____________M A I N____________________________________________________________
# ===============================================================================
if __name__ == "__main__":
scriptName, environment, letterType, userName, firstName, lastName, socSecNumber, pronoun, pronoun2, companyName, address, city, state, zip, attention, faxNumber, lastActiveDate, lastActiveDateFlag, lastOrderDate, lastOrderDateFlag, contractDate, company = argv
Posts: 6,800
Threads: 20
Joined: Feb 2020
Apr-03-2024, 06:54 PM
(This post was last modified: Apr-03-2024, 06:54 PM by deanhystad.)
Arguments that contain spaces must be wrapped in double quotes, both at the command line and inside the shell script. Anywhere the argument is used as a command line argument.
python_program.py
import sys
print(sys.argv) test_script.sh
Output: #!/bin/bash
echo $1
echo $2
echo $3
python python_program.py "$1" "$2" "$3"
Running the script
Output: $ bash test_script.sh single "two words" "is three words"
single
two words
is three words
['python_program.py', 'single', 'two words', 'is three words']
I think there must be a better way to solve your problem. I would get rid of the shell script and call the python program directly. Instead of passing values to the python program using the command line, I would use a file that contains the information. What you have looks like a maintenance nightmare to me.
How do you call you script? You don't type all those command line arguments, do you?
Posts: 4,790
Threads: 76
Joined: Jan 2018
Apr-03-2024, 06:57 PM
(This post was last modified: Apr-03-2024, 06:58 PM by Gribouillis.)
(Apr-03-2024, 03:38 PM)kaustin Wrote: How do I pass my parameters as needed? I think it is a bash question instead of a python question. It seems to me that you only need to add quotes around your variables, for example in my bash terminal
Output: λ foo='hello world'
λ bar="an argument with many spaces"
λ echoargs "$foo" "$bar"
['/home/eric/bin/echoargs', 'hello world', 'an argument with many spaces']
λ echoargs $foo $bar
['/home/eric/bin/echoargs', 'hello', 'world', 'an', 'argument', 'with', 'many', 'spaces']
Here echoargs is a Python script that only prints the arguments that it receives. As you can see, in the first invocation with double quotes "$foo" the shell handles correctly hello world as a single argument, but when I don't use the double quotes, it treats it as two arguments.
Python has nothing to do with this problem because it only receives the arguments that were processed by the shell.
« We can solve any problem by introducing an extra level of indirection »
Posts: 3
Threads: 1
Joined: Apr 2024
(Apr-03-2024, 06:54 PM)deanhystad Wrote: Arguments that contain spaces must be wrapped in double quotes, both at the command line and inside the shell script. Anywhere the argument is used as a command line argument.
python_program.py
import sys
print(sys.argv) test_script.sh
Output: #!/bin/bash
echo $1
echo $2
echo $3
python python_program.py "$1" "$2" "$3"
Running the script
Output: $ bash test_script.sh single "two words" "is three words"
single
two words
is three words
['python_program.py', 'single', 'two words', 'is three words']
I think there must be a better way to solve your problem. I would get rid of the shell script and call the python program directly. Instead of passing values to the python program using the command line, I would use a file that contains the information. What you have looks like a maintenance nightmare to me.
How do you call you script? You don't type all those command line arguments, do you?
We run RPG here and as far as I knew (until a few minutes ago), the only way to call the python (.py) was to use the .sh file. I get my parameters from the user in an RPG program, then pass them to the shell which in turn calls the python. One of my co-workers just showed me another possiblity using QSHPYRUN which I haven't tried yet.
Posts: 6,800
Threads: 20
Joined: Feb 2020
You can call a python program directly. No need for a shell script. Make sure your python program file is executable (chmod +x). You can add a shebang at the top of the file to tell it what python interpreter to use. For example:
program.py
#!/usr/bin/python3
import sys
print(len(sys.argv), sys.argv) Output: $ ./program.py a "b c" "d e f g"
4 ['./program.py', 'a', 'b c', 'd e f g']
|