How to use export with Python on Linux

后端 未结 12 1529
梦如初夏
梦如初夏 2020-11-29 23:09

I need to make an export like this in Python :

# export MY_DATA=\"my_export\"

I\'ve tried to do :

# -*- python-mode -*-
# -         


        
12条回答
  •  一整个雨季
    2020-11-29 23:32

    I have an excellent answer.

    #! /bin/bash
    
    output=$(git diff origin/master..origin/develop | \
    python -c '
      # DO YOUR HACKING
      variable1_to_be_exported="Yo Yo"
      variable2_to_be_exported="Honey Singh"
      … so on
      magic=""
      magic+="export onShell-var1=\""+str(variable1_to_be_exported)+"\"\n"
      magic+="export onShell-var2=\""+str(variable2_to_be_exported)+"\""  
      print magic
    '
    )
    
    eval "$output"
    echo "$onShell-var1" // Output will be Yo Yo
    echo "$onShell-var2" // Output will be Honey Singh
    
    

    Mr Alex Tingle is correct about those processes and sub-process stuffs

    How it can be achieved is like the above I have mentioned. Key Concept is :

    1. Whatever printed from python will be stored in the variable in the catching variable in bash [output]
    2. We can execute any command in the form of string using eval
    3. So, prepare your print output from python in a meaningful bash commands
    4. use eval to execute it in bash

    And you can see your results

    NOTE Always execute the eval using double quotes or else bash will mess up your \ns and outputs will be strange

    PS: I don't like bash but your have to use it

提交回复
热议问题