-2

I am trying to create a Bash script to download a new file (a Bash script) with various variables and then update OR read one of those variables with one already stored in an existing Python file.

I cannot update the Python file, only read from it.

My Python file (config.py) just contains a list of variables, the one i'm interested in:

iface  = 'wlan1'

My Bash script has a variable called IWFace which needs to be the same as iface from the Python file.

#!/bin/bash
IWFACE=wlan1

I have tried all sorts of ways to do this but it seems as I am reading from a Python file it turns out to be really quite difficult.

I have also tried a sed read command to read the variable from the Python file and use it to create a new variable in my Bash script but I'm struggling to make that work.

Is it actually possible? My only other idea was to ask the question via read -p and then store the answer as the variable but as it already exists I wanted to automate the process.

13
  • 2
    why not call/execute the bash script from python and pass the variable content as argument Commented Dec 3, 2023 at 17:09
  • 3
    $iface is not a valid Python variable name. Without seeing either script, we really cannot offer much by way of guidance. Anyway, this seems like an XY problem. One approach is to write a new Python script which imports the nasty one and extracts the value of the variable and prints it so that you can retrieve it from your Bash script. Commented Dec 3, 2023 at 17:24
  • 2
    We need an example of a Python script -- not necessarily your original, but something close enough that if we show you how to extract a variable from that script without changing it, you would accept that as an answer to your overall question -- included in the body of your question itself. Commented Dec 3, 2023 at 17:28
  • 2
    @Pigsfoot, edit the details needed for an answer into the question; content only present in comments doesn't count as included (and can be deleted at any time). And make sure you get the details right (iface = wlan0 isn't valid Python code unless there's a previously-defined wlan0 variable). Commented Dec 3, 2023 at 17:59
  • 1
    Right, what furas gave was correct when you said your script contained exactly iface = wlan1 without any quotes (that's part of why it's important when showing examples to get the details exactly right). You can find answers that work with the quotes being present in the linked duplicate. (The one I wrote there will work even if the Python script runs some code to dynamically assign iface, though you might get undesired execution if it has module-scoped code without a __name__ == '__main__' guard protecting it). Commented Dec 3, 2023 at 19:03

3 Answers 3

2

An alternative is to use awk

#!/usr/bin/env bash

IFACE=$(awk -F \' '/^iface =/ {print $2 }' config.py)
echo "$IFACE"

Or with grep and cut

#!/usr/bin/env bash

IFACE=$(grep '^iface =' config.py | cut -d \' -f 2 -)
echo "$IFACE"
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this won't work with all Python syntax. Strings don't need to use ' -- they can use ", they can use triple-quotes, they can span multiple lines...
(And think about posting this on the preexisting, more-canonical copy of the question at stackoverflow.com/questions/34751930/… rather than here).
2

Write a python script that imports config.py and just prints the value of the variable iface. In your shell script call IWFACE=$(python ...), e.g.:

$ cat tst.sh
#!/usr/bin/env bash

IWFACE=$(PYTHONPATH=. python -c 'from config import iface; print(iface)')
echo "$IWFACE"

$ ./tst.sh
wlan1

PYTHONPATH just needs to be set to the directory where config.py exists.

The above is just for the OPs case where My Python file (config.py) just contains a list of variables and they're writing the script to print the value of a specific variable whose name they also hard-code. See @CharlesDuffy's comments below for risks of other usage.

Tested with python 3.9.16.

13 Comments

If that's an adequate answer, the duplicate proposed in comments on the question covers it.
@CharlesDuffy when I tried that I got a syntax error $ File "<string>", line 6 print "%s\0" % str(d[k]).split("\0")[0] # ...and extract your strings NUL-delimited ^ SyntaxError: invalid syntax so I was planning to fill this in with a complete script if/when I had it working. Maybe that's a python2 vs python3 thing, idk, I'm using Python 3.9.16.
I'm particularly interested in the solution as I'm using python quite a bit these days and I didn't expect this to be so relatively complicated.
Hmm, lessee if I can reproduce that.
...you're right, it's a Python 2-vs-3 thing. Fix upcoming on the other post.
|
0

You could use grep to get text from file.

  • option -o shows only matching part (not full line).
  • option -P allows to use Perl regex and you can use (?<= ...) (for positive lookbehind) to find element which shouldn't be displayed in result.

If you have value in " " then

#!/usr/bin/env bash

IWFACE=$(grep -oP '(?<=iface = ")[^"]*' config.py)
echo $IWFACE

If you have value in ' ' then replace ' with " and ' with "

#!/usr/bin/env bash

IWFACE=$(grep -oP "(?<=iface = ')[^']*" config.py)
echo $IWFACE

EDIT:

I found shorter version with \K instead of (?<= ...)

IWFACE=$(grep -oP 'iface = "\K[^"]*' config.py)
IWFACE=$(grep -oP "iface = '\K[^']*" config.py)

Perl doc for (?<= ...) and \K

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.