1

I'm running a bash script inside of a Python script, and I need to use the variables that were defined inside of the bash script, in the Python script.

For some context, here is the bash script:

#!/bin/bash
updates=$(/usr/lib/update-notifier/apt-check 2>&1)
all=${updates%";"*}
security=${updates#*";"}

Here is how I am calling it in the Python script:

import subprocess
subprocess.call(["/usr/bin/checkupdates"])

I want to use the variables that were defined in that bash script ('all' and 'security') in this SQL update statement (which is part of the Python script):

cursor.execute("update dbo.updates_preprod set updates_available = ?, securityupdates_available = ? where hostname = ?",(all, security , socket.gethostname()))
cnxn.commit()

Is it possible to do this? If not, could I run 2 separate scripts (each script would echo one of the variables) and grab the stdout of each script and define it as a variable in Python?

Thanks in advance!

4
  • 3
    bash has to display values (using echo) and then Python can get this text with values and use it in SQL query. But I would display updates to get and change it in Python. I would even run /usr/lib/update-notifier/apt-check 2>&1 directly with subprocess. Commented Apr 28, 2019 at 1:33
  • 3
    instead of call() you should use output = subprocess.check_output(...) or output = subprocess.run(...).stdout. More in documentation: subprocess Commented Apr 28, 2019 at 1:41
  • 1
    Mixing languages is never a good design choice, probably better to rewrite the bash script in python. i.e. why not call /usr/lib/update-notifier/apt-check from python? Commented Apr 28, 2019 at 9:32
  • @cdarke Of course... (bad habits) Commented Apr 29, 2019 at 18:11

1 Answer 1

1

In the end it was much easier to call that command directly from the Python script.

output = subprocess.check_output("usr/lib/update-notifier/apt-check", shell=True) all,sec = output.decode().split(';')

Thanks to @cdarke and @furas for the suggestion.

Sign up to request clarification or add additional context in comments.

1 Comment

@furas also suggested that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.