Trevor Ludgate
Trevor Ludgate

Reputation: 21

How to run a bash script in python and use the variables that where defined in that script

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!

Upvotes: 1

Views: 89

Answers (1)

Trevor Ludgate
Trevor Ludgate

Reputation: 21

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.

Upvotes: 1

Related Questions