How to define a bash variable from python EOF
Question:
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
Answers:
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.
This solution might be complex, but working my ubuntu
one line of python script can
- import yaml and sys lib
- lambda-function read yaml file by using first sys.argv1
- get expected value from return nested-dictionary from lambda function by using sys.argv2 sys.argv3
- print yaml_as_dict will load test_var as variable
- check test_var by
echo $test_var
`
$
$ 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?
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
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.
This solution might be complex, but working my ubuntu
one line of python script can
- import yaml and sys lib
- lambda-function read yaml file by using first sys.argv1
- get expected value from return nested-dictionary from lambda function by using sys.argv2 sys.argv3
- print yaml_as_dict will load test_var as variable
- check test_var by
echo $test_var
`
$
$ 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?