Print Works As Return Statement When Fetching Values In Bash From Python Scripts

Pavol Kutaj
1 min readJan 5, 2022

--

The aim of this pagešŸ“ is show getting a value out of a python script that runs as part of a bash script, to achieve

# BASH
var=$(python3 foo.py)

Story: I am running a transient AWS EMR cluster for a job. I need to configure the service, the config is based on the data volume which keeps changing. I have a python script accepting data volume as an input parameter and it does the configuration itself via AWS CLI. All of the sudden, however, I need to work with the type of the configuration that the python script sets in the bash script itself, for infrastructure analytics.

1. notes

  • the answer is that the print statement within Python is assignable to a variable in Bash
config_type=$(python3 ./configure_EMR.py --size "1024000000")
echo $config_type
EMR config set to type 6
  • in addition, I need to get only the integer from that string
  • Then, use parameter expansion to delete everything except the numeric value
config_type=$(python3 ./configure_EMR.py --size "1024000000")
config_type=${config_type#"EMR config set to type "}
echo $config_type
6
  • Of course, this is the most simplistic answer but I am happy its possible

2. links

--

--

No responses yet