简体   繁体   中英

Pass variable from python file to bash script

I am trying to pass variables from python to bash script. I have python file init.py in that file i have to variables that variables i want to call using bash script. but i am unable to call. i tried like in python file (init.py)

server = os.environ('in_server')
database = os.environ('in_database')
tokenstruct=MSI_token()
cnxn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};Server='+server+';DATABASE='+database+';',attrs_before={1256:bytearray(tokenstruct)})
    cursor= cnxn.cursor()

in bash file:

export in_server=${OUT_SERVER}
export in_database=${OUT_DATABASE}
OUT_SERVER = url
OUT_DATABASE = database name

but its not working for me. How can i call python variables in bash script?

错误

If you have something like this in your bash script

echo "var=$var"

then from python, you can call

#! /usr/bin/env python3

import subprocess

env = {'var': 'val'}
subprocess.run(['myscript'], env=env)

and you'll receive

var=val

in your python script you can set a environment variable,

import os

#Set environment variables
os.environ['VARIABLE'] = 'VALUE'

in your bash script in the same terminal session, you can call the environment variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM