Use the variables in a python script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use the variables in a python script.
# 1  
Old 06-27-2017
Use the variables in a python script.

Hi,

I have a defined some variables in a configuration file and I want to use those variables in a python script. For example, I have the following variables in a configuration file.

Code:
[test_ini]
Region = North
Country = USA

[ptfm_dgnst]
CONN_FL=`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`
CONN=`$HOME/opt/bin/decrypt $CONN_FL`

Now I want to execute those variables whenever the configuration file is being called from python script and print the value of CONN variable using the python script.

I am new to python and appreciate if you could help me fix the following script I wrote.

Code:
import os
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('pltfm.ini')
conn_str=os.system("echo $CONN")
print conn_str

# 2  
Old 06-28-2017
Using Python 2.7 on Ubuntu 16.04 (Xenial) - What is your system?

Does this help?
Code:
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from ConfigParser import SafeConfigParser
>>> parser = SafeConfigParser()
>>> parser.read('pltfm.ini')
['pltfm.ini']
>>> help(parser)

>>> parser.sections()
['test_ini', 'ptfm_dgnst']
>>> parser.items('test_ini')
[('region', 'North'), ('country', 'USA')]
>>> parser.items('ptfm_dgnst')
[('conn_fl', "`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`"), 
('conn', '`$HOME/opt/bin/decrypt $CONN_FL`')]
>>> ptfm_dgnst = dict(parser.items('ptfm_dgnst'))
>>> conn_str = ptfm_dgnst['conn']
>>> print conn_str
`$HOME/opt/bin/decrypt $CONN_FL`
>>>

As you can see I am using the Python CLI - it's so much easier for testing snippets of code. Note the use of the help function - it shows the equivalent of a man page of Python classes and their methods. I don't know what you were trying to achieve with
Code:
conn_str=os.system("echo $CONN")

but I hope the above helps you with achieving that.

A quick look at the statements I typed in:
parser.sections() lists the section of the ini file.
parser.items('test_ini') lists the section test_ini as a list of tuples; each tuple a name/value pair.
ptfm_dgnst = dict(parser.items('ptfm_dgnst')) creates a dictionary (simplistically Python's hash table) so you can now access the values through their names.
conn_str = ptfm_dgnst['conn'] does what I think you were trying to do with the conn_str = os.system() statement.

I hope this helps.

Andrew

Last edited by apmcd47; 06-28-2017 at 12:03 PM.. Reason: Typo! con.system instead of os.system
# 3  
Old 06-28-2017
Thanks for your time Andrew.

I am working on Python 2.6.9 (unknown, Aug 5 2016, 11:15:31)

Basically I want to execute the following statements in sequence. It can be done inside the python code but I prefer to have these values defined on the pltfm.ini file and then let python execute these statements and store the output in CONN variable.

Code:
CONN_FL=`perl -F= -lane 'print $F[1] if m!^com.cis.b33.team_db_conn!' $HOME/opt/config.ini`
CONN=`$HOME/opt/bin/decrypt $CONN_FL`

I know how to handle this in ksh or bash but I am new to Python so I am looking for assistance here. Thank you
# 4  
Old 06-28-2017
Look at using the following:
Code:
import shlex
import subprocess
conn_fl = ptfm_dgnst['conn_fl']
subprocess.call(shlex.split(conn_fl))

Not checked. You will need to read up on shlex and subprocess to get them to work as you would expect. Note that the "proper" way of making system calls in Python is now through the use of subprocess.

Have fun with that Smilie

Andrew

Last edited by apmcd47; 06-28-2017 at 12:47 PM.. Reason: Typos. Always typos :)
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 06-28-2017
So you want to run shell from python?
# 6  
Old 06-28-2017
Yes that's pretty much what I would like to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

3. Shell Programming and Scripting

Using Python Variables in one Instance

I have another basic Pythonic question that I cant seem to figure out. Here we go. So what I am trying to accomplish is simply using a second variable in my "scanjob" variable adding "api_tok". I am using a product that needs an api_token for every call so I just want to persistently add "api_tok"... (0 Replies)
Discussion started by: metallica1973
0 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Python script called by a shell script

experts, i wrote a python script to do a certain job, i tried it and it is working fine, i want this script to be executed automatically after a ksh script, the problem is when i execute the ksh script my python script runes perfectly after the ksh script as I have include it at the end of the ksh... (1 Reply)
Discussion started by: q8devilish
1 Replies

6. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

7. Web Development

passing variables to python script

Hello again, You guys might remember me from the "Apache problems" thread. And because this forum is the only one that actually helped me out after months of problems I was hoping you could help me with something related. Now if you remember the final code that we made for creating an account... (0 Replies)
Discussion started by: darkphaux
0 Replies

8. Shell Programming and Scripting

Hi Python and shell script,the script hangs

Hi I need to run a shell script from a TCL script,the shell script in trun will run a python script 1.Tcl script set filename "./GopiRun.sh" 2.GopiRun.sh python ./psi.py $MYSB/test_scripts/delivery/gpy1.py 3.I have my gpy1.py script. Here the problem i am facing is on running... (0 Replies)
Discussion started by: nathgopi214
0 Replies

9. Shell Programming and Scripting

How to run python script from a Tcl script

Hi I have a python script,i need to run this script from a tcl script.Can anyone let me know how to do this (1 Reply)
Discussion started by: nathgopi214
1 Replies
Login or Register to Ask a Question