how to get a variable of a python file from bash script - python-3.x

I have a python file, conf.py which is used to store configuration variables. conf.py is given below:
import os
step_number=100
I have a bash script runner.sh which tries to reach the variables from conf.py:
#! /bin/bash
#get step_number from conf file
step_number_=$(python ./conf.py step_number)
However, if I try to print the step_number_ with echo $step_number_, it returns empty value. Can you please help me to fix it?

$(command) is replaced with the standard output of the command. So the Python script needs to print the variable so you can substitute it this way.
import os
step_number = 100
print(step_number)

Related

Can I pass a variable from python to bash file?

I have a bash file with a bunch of sed commands like this :
sed -i 's/hello my name is Thibault/hello my name is Louis/g' "$1"
so for now i'm doing all of this "by hand", however, I have a python script with a tkinter GUI and several input fields for the user. I would like to find a trick so that if the user inputs "hello my name is Olivia" in the text field then the regex would look like this:
sed -i 's/hello my name is Thibault/hello my name is Olivia/g' "$1"
So I was thinking that i could store the python text input result in the variable to have the regex look like this:
sed -i 's/hello my name is Thibault/$my_variable/g' "$1"
but i don't know how or if this is even possible. Lastly I want to mention that i know i could just ask for the user input in the bash script but this is for my first internship and I have to go through the python GUI.
Edit: i'm on windows 10 if this is any important
Try it like this :
import os
original_text = 'hello my name is Thibault'
new_text = 'hello my name is Louis'
filename = 'test.txt'
os.system (f'sed -i "s/{original_text}/{new_text}/g" {filename}')
For passing data (in your case: some string) from your Python program to a subprocess running a bash script, you have first of all the same options like when calling one bash script from another one: Either design the called script to expect positional parameters (use it as $1 for example) and pass the string as parameter. For instance, if the string is stored in the Python variable parameter, it would look like:
import subprocess
subprocess.call ['bash', './script_to_be_called', parameter]
The other possibility is to design the bash script so that it expects the string to be stored in a variable of a certain name (use it as $PARSTRING for instance) and pass the data via the environment:
import os
os.environ['PARSTRING']=parameter
subprocess.call['bash', './script_to_be_called']
If the "script" executes only a single command, you could alternatively synthesize the command line in your Python program. Assume that you have a string bashcommand, which already holds the complete command which is supposed to be executed by bash, you could do a
import subprocess
subprocess.call ['bash', '-c', bashcommand]
While this should answer your question, I can't help but pointing out, that for executing a single external command, I would not create a shell process, but invoke this program directly as a child process. Also don't forget that spawning a child process takes time, and if you have many such invocations, it might make sense to redesign your approach, for instance by doing everything inside Python, or having only one child prcocess which gets as input the data for all the substitutions to be performed (typically via a file).

finding a file using general location in a python script

I making a script in python3. this script takes an input file. depends on who is running the script every time the location of this input file is different but always would be in the same directory as the script is. so I want to give the location of the input file to the script but basically the script should find it. my input file always would have the same name (infile.txt). to do so, I am using this way in python3:
path = os.path.join(os.getcwd())
input = path/infile.txt
but it does not return anything. do you know how to fix it?
os.getcwd() return the working directory which can be different to the directory where the script is. The working directory corresponds from where python is executed.
In order to know where is the scipt you should use
`
import os
input = os.path.join(os.path.dirname(os.path.realpath(__file__)), infile.txt)
# and you should use os.path.join
`
If i understand your question properly;
You have python script (sample.py) and a input file (sample_input_file.txt) in a directory say; D:\stackoverflow\sample.y and D:\stackoverflow\sample_input_file.txt respectively.
import os
stackoverflow_dir = os.getcwd()
sample_txt_file_path = os.path.join(stackoverflow_dir, 'sample_input_file.txt')
print(sample_txt_file_path)
os.path.join() takes *args as second argument which must have been your file path to join.

Can i access the variables of the python script after run that python script with console?

I run the python script using terminal command
python3 myScript.py
It's simply run my program but if i want to open python console after complete run of my script so that i can access my script's variables.
So, What should i do ? and How can i get my script's variables after run the code using terminal ?
Open a python terminal (type 'python' in cmd);
Paste this (replace 'myScript.py' with your script filename):
def run():
t = ""
with open('myScript.py') as f:
t = f.read()
return t
Type exec(run()). Now you will have access to the variables defined in myScript.py.
I needed to do this so I could explore the result of a request from the requests library, without having to paste the code to make the requests every time.
Make the program run the other program you want with the variables as arguments. For example:
#program1
var1=7
var2="hi"
import os
os.system("python %s %d %s" % (filename, var1, var2))
#program2
import sys
#do something such as:
print(sys.argv[1]) #for var1
print(sys.argv[2]) #for var2
Basically, you are running program2 with arguments that can be referenced later.
Hope this helps :)

can someone tell what this code snippet does

I am trying to understand this below mentioned code snippet, currently i am stuck at line number 3 and after digging alot i got to know that $MYPERL is where perl binaries are defined/located and for $PERLDB is what perl debugger i,e -d:ptkdb and basically this is a perl script and some how person who coded this wrapps it to use the latest perl version. can some one tell me how i can change MYPERL variable value /home/Desktop/goudar/perl/ and execute rest of the script ?
#!/bin/sh
# -*- cperl -*-
exec $MYPERL -x $PERLDB -wS $0 ${1+"$#"}
#!perl
#line 6
### perl
use Cwd;
use Data::Dumper;
use List::MoreUtils qw/ uniq /;
use JSON;
use Mojo::JSON;
#rest of the code go here#
can someone tell what this code snippet does
It executes the embedded Perl script using the Perl interpreter specified by env var MYPERL. Options specified in env var PERLDB (if any) are passed to the interpreter. Warnings are enabled globally.
how i can change MYPERL variable value /home/Desktop/goudar/perl/ and execute rest of the script
If the process that will launch the script is a bourne-based, then
export MYPERL=/home/Desktop/goudar/perl/
That said, I don't know why you want to assign that value to the MYPERL env variable since the script expects it to be the path to a Perl interpreter.

Set Enviroment Variable that contains two paths interelated paths

I am trying to create a custom environment variable that uses python to execute a py file.
Here is an example of what I have
export VAR=${VAR}:"/usr/bin/python2.7 /home/user/file"
When I use the variable I get the output:
bash: :/usr/bin/python2.7: No such file or directory
If I echo the variable I get the output:
/usr/bin/python2.7 /home/user/file
EDIT:
Trying "$VAR" gives me the output
bash: :/usr/bin/python2.7 /home/user/file: No such file or directory
If I run just this /usr/bin/python2.7 /home/user/file it works
I think an alias is more appropriate for all kinds like this (you may consider a more suitable name for the alias)
alias var="/usr/bin/python2.7 /home/user/file"
If you want to stick with your version you have to tell your shell to evaluate the content of VAR.
For this you just have to invoke
eval ${VAR}
By the way, why do you append the string "/usr/bin/python2.7 /home/user/file" to VAR instead of overwriting the content of VAR?

Resources