0

I'm relatively new to bash and unable to explain my problem in the title, I've done a lot of googling and I guess that's the title I came up with

I want to be able to use python EOF and define a bash variable in the EOF (if possible) and call it after.

test.txt:

everything - literally the string everything

And I'm opening this file and getting contents with test.sh:

#!/bin/bash
CMD=$(cat <<EOF

with open('text.txt', 'r') as f:
    for line in f.readlines():
          pass

print(f"WOW text has {line}")

EOF
)

python3 -c "$CMD"

Output:

WOW text has everything

I want to be able to share a variable by defining it in my CMD (idk what it's called) and echo it in bash after it's done;

#!/bin/bash
CMD=$(cat <<EOF

with open('text.txt', 'r') as f:
    for line in f.readlines():
          pass

print(f"WOW text has {line}")
$var = line - somehow define a bash variable in python EOF
EOF
)

python3 -c "$CMD"
echo $var - output this 

So then the new output (of what I want) is:

WOW text has everything
everything
martineau
  • 119,623
  • 25
  • 170
  • 301
EpicGoodBoi
  • 120
  • 2
  • 11
  • You are assigning a python program to `CMD`, but attempting to invoke python with `$PYCMD`. – William Pursell Jan 31 '22 at 20:39
  • opps lol, but that's not the problem tho – EpicGoodBoi Jan 31 '22 at 20:57
  • The `$CMD` data is invoked inside `python3`, you can't set a variable in python and expect to see the value in the shell. You can capture the output from the python command into a variable e.g. `var=$(python3 -c "$CMD")`. – Anya Shenanigans Jan 31 '22 at 21:01
  • 2
    I think what you're calling "EOF" is a [here-document](https://www.gnu.org/software/bash/manual/bash.html#Here-Documents) -- "EOF" is just the delimiter you happen to be using to indicate the end of the here-document. As for defining a shell variable from a python program (wherever it's stored), you can't -- at least, not directly. Python runs as a subprocess of the shell, and has no ability to modify the shell's environment or internal state. – Gordon Davisson Jan 31 '22 at 21:07
  • @Petesh How would I return this value? A print statement? A return? – EpicGoodBoi Jan 31 '22 at 21:46
  • 1
    You capture the *printed* output from the python script. – Anya Shenanigans Jan 31 '22 at 21:51

2 Answers2

2

I want to be able to share a variable by defining it in my CMD

That is not possible.

I want to

Write a Bash builtin or path Bash with a modification that creates a shared memory region and introduces a new special variable that uses that shared memory region to store and fetch the value of the variable.

Use the shared memory region from python to store the value of the variable.

You could also go three steps further, and just straight introduce a Bash loadable module with interface to Python with communication via a local socket or similar, similar to vim Python library.


You can:

  • Output the value and use a command substitution to capture the output of python process and assign that output to a variable.

    var=$(python -c 'print(line)')
    echo "$var"
    
  • Store the value to a file and then read the content of that file in Bash. Or similar.

    python -c 'open("/tmp/temporaryfile.txt").write(line)'
    var=$(cat /tmp/temporaryfile.txt)
    echo "$var"
    

You may want to research "processes" - what they are, what do they share and what they don't share, and what are the methods of communication between processes.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

This solution might be complex, but working my

one line of script can

`

$
$ test_var=$(python3 -c $"import yaml,sys; yaml_as_dict=(lambda :yaml.safe_load(open(f'{sys.argv[1]}','r').read()))()[sys.argv[2]][sys.argv[3]]; print(yaml_as_dict)" <argv1> <argv2> <argv3>)
$
$ echo $test_var
$ 

How to execute multiline python code from a bash script?

How can I write multi-line code in the Terminal use python?

How can I put multiple statements in one line?

How to define a bash variable from python EOF

P_M
  • 21
  • 2