LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2020, 09:22 AM   #1
gIn_gOut
Member
 
Registered: Jul 2015
Posts: 39

Rep: Reputation: Disabled
Passing Arguments to a Bash Script from a Python Script


G'Day folks

I have a bit of a "sticky wicket" today, which probably needs only a small tweak to resolve.

I'm passing the path to a file, which is discovered in a python script, for further work over in a bash script. Here's the python-side snippet:
Code:
filePath = os.path.realpath(sys.argv[4])
print "This", filePath, "file and path has been identified."
subprocess.Popen(['bash ./receivingScript', filePath], shell=True)
The trouble:
  1. While the string content of "filePath" will expand and print correctly to the console in line 2, and
  2. our bash script, "receivingScript", will be launched as intended in line 3,
  3. the string content of "filePath" is not passed along properly as a commandline argument for "receivingScript".
In sum, whatever gets through to the bash script seems to be completely empty.

Where's the beef?!

Thanks a bunch
 
Old 06-26-2020, 09:47 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,606

Rep: Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279Reputation: 4279
Assuming this is a current version of Python, and receivingScript is passed properly?

Code:
subprocess.Popen([f'bash ./receivingScript {filePath}'], shell=True)
Note that I'm using an f string.
 
Old 06-26-2020, 10:15 AM   #3
gIn_gOut
Member
 
Registered: Jul 2015
Posts: 39

Original Poster
Rep: Reputation: Disabled
@dugan:
Thanks for the tip. And yes, "./receivingScript" is passed properly -- all deploys as anticipated.

Furthermore, this type of example works for both sides without issue:
Code:
subprocess.Popen(['bash ./receivingScript someText'], shell=True)
Drawing upon your quick example, I tried both
Code:
subprocess.Popen([f'bash ./receivingScript {filePath}'], shell=True)
and
Code:
subprocess.Popen([f"bash ./receivingScript {filePath}"], shell=True)
Output: "SyntaxError: invalid syntax"
Version: Python 2.7.9

If this is a version/feature issue, is there a workaround solution for this problem? Upgrading on this particular machine would be a hairball...

Thanks again!
 
Old 06-26-2020, 10:40 AM   #4
gIn_gOut
Member
 
Registered: Jul 2015
Posts: 39

Original Poster
Rep: Reputation: Disabled
Stop the presses!

This appears to work:
Code:
subprocess.Popen(['bash ./receivingScript %s' % (filePath)], shell=True)


Thanks and Cheers!
 
Old 06-26-2020, 03:36 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,056

Rep: Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469Reputation: 1469
I'd try this change:
Code:
- subprocess.Popen(['bash ./receivingScript', filePath], shell=True)
+ subprocess.Popen(['bash', './receivingScript', filePath], shell=False)
The difference will be visible if filePath contains whitespace characters. Or shell meta-characters like $ \ `
 
1 members found this post helpful.
Old 07-03-2020, 04:18 PM   #6
gIn_gOut
Member
 
Registered: Jul 2015
Posts: 39

Original Poster
Rep: Reputation: Disabled
@NevemTeve:

Thanks for stopping by! Added your snippets to my collection

Have a great day...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing arguments with SPACES to bash script theEditor876 Linux - Newbie 2 06-28-2014 07:41 AM
[SOLVED] Passing arguments with blanks to du in a bash shell script impert Programming 13 06-12-2014 04:57 AM
Bash Script passing arguments dimitriauxio Linux - Newbie 8 09-16-2013 09:19 AM
Passing arguments to shell script like perl script get-opts somupl86 Linux - Newbie 2 12-02-2010 11:14 PM
[SOLVED] passing commands as arguments to functions in bash script mastahyeti Linux - General 3 05-31-2010 06:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration