2017-07-15 56 views
0

我有一个包含颜色表的文件,该表设置了用于zsh提示的环境变量。目前有一个python脚本,它可以在#color-begin和#color-end之间找到所有颜色。但是,我无法将python字符串变量传递到python shell调用中,我不确定问题出在哪里。打印包含python颜色的环境变量

pythonStr = "fg_blue" 
pythonStr = "$"+fg_blue 
os.system("echo + pythonStr") 

东西到目前为止,我还发现,传递变量到.SH文件的几个例子,但是如何做一个打印出来没有.SH文件中的环境变量的颜色?如果这是不可能的,为什么会出现这种情况? 这里是我现有的python代码以及我想要打印的颜色代码。

任何帮助将不胜感激。颜色

printcolor.py

import os 
import subprocess 

def prcl(): 

with open("appearance") as f: 
    content = f.readlines() 

    foo = False 

    for line in content: 
     line = line.strip(' \n\t') 

     # Toggle boolean                           
     if "color-" in line: 
      foo = not foo 

     if foo == True: 
      if line is not "" and "#" not in line: 
       # Use '=' as a delimiter 
       head, sep, tail = line.partition("=") 
       head='"$'+head+'"' 

       # prints out blank lines 
       os.system("echo "+head) 

       #prints literal string 
       #print(head)                          

环境变量

#color-begin 

fg_black=%{$'\e[0;30m'%} 
fg_red=%{$'\e[0;31m'%} 
fg_green=%{$'\e[0;32m'%} 
fg_brown=%{$'\e[0;33m'%} 
fg_blue=%{$'\e[0;34m'%} 
fg_purple=%{$'\e[0;35m'%} 
fg_cyan=%{$'\e[0;36m'%} 
fg_lgray=%{$'\e[0;37m'%} 
fg_dgray=%{$'\e[1;30m'%} 
fg_lred=%{$'\e[1;31m'%} 
fg_lgreen=%{$'\e[1;32m'%} 
fg_yellow=%{$'\e[1;33m'%} 
fg_lblue=%{$'\e[1;34m'%} 
fg_pink=%{$'\e[1;35m'%} 
fg_lcyan=%{$'\e[1;36m'%} 
fg_white=%{$'\e[1;37m'%} 
fg_blue=%{$'\e[0;34m'%} 
fg_purple=%{$'\e[0;35m'%} 
fg_cyan=%{$'\e[0;36m'%} 
fg_lgray=%{$'\e[0;37m'%} 
fg_dgray=%{$'\e[1;30m'%} 
fg_lred=%{$'\e[1;31m'%} 
fg_lgreen=%{$'\e[1;32m'%} 
fg_yellow=%{$'\e[1;33m'%} 
fg_lblue=%{$'\e[1;34m'%} 
fg_pink=%{$'\e[1;35m'%} 
fg_lcyan=%{$'\e[1;36m'%} 
fg_white=%{$'\e[1;37m'%} 

#Text Background Colors                                                               
bg_red=%{$'\e[0;41m'%} 
bg_green=%{$'\e[0;42m'%} 
bg_brown=%{$'\e[0;43m'%} 
bg_blue=%{$'\e[0;44m'%} 
bg_purple=%{$'\e[0;45m'%} 
bg_cyan=%{$'\e[0;46m'%} 
bg_gray=%{$'\e[0;47m'%} 

#Attributes                                                                  
at_normal=%{$'\e[0m'%} 
at_bold=%{$'\e[1m'%} 
at_italics=%{$'\e[3m'%} 
at_underl=%{$'\e[4m'%} 
at_blink=%{$'\e[5m'%} 
at_outline=%{$'\e[6m'%} 
at_reverse=%{$'\e[7m'%} 
at_nondisp=%{$'\e[8m'%} 
at_strike=%{$'\e[9m'%} 
at_boldoff=%{$'\e[22m'%} 
at_italicsoff=%{$'\e[23m'%} 
at_underloff=%{$'\e[24m'%} 
at_blinkoff=%{$'\e[25m'%} 
at_reverseoff=%{$'\e[27m'%} 
at_strikeoff=%{$'\e[29m'%} 

#color-end 
+0

只是使用打印(),不使用os.system(回声..) – hek2mgl

+0

我在python代码片段的最后一行注释了“print(head)”。 – Zypps987

+0

我明白了。使用'os.system('/ bin/bash -c“echo -e'+ tail +'”')'。 – hek2mgl

回答

1

没有与你的Python程序的几个小问题,但为什么你的变量不扩大的主要原因是因为他们当Python程序运行时,不存在于环境中。

appearance文件应该是这样:

export fg_black=$'\e[0;30m' 
export fg_red=$'\e[0;31m' 
export fg_green=$'\e[0;32m' 

%{..%}仅仅是不必要的,但export是至关重要的。如果没有export,那么在shell中的source appearance之后,如果以后从该shell中调用python printcolor.py,则这些变量不会传递到python进程。

zsh另一种选择是使用setopt allexport和所有环境变量导出到一个子(子shell),但可以有很多不希望的效果,让你与个别export其中更加安全。

您修改appearance文件后,你将不得不调整颜色的名字在Python解析,也许像这样(内if只):

if line and not line.startswith("#"): 
    color = line.split("=")[0].split()[1] 
    subprocess.call('echo "${%s}"text' % color, shell=True) 
+0

感谢您的解释!现在正在工作。 – Zypps987

+0

不客气,很高兴它的作品。 – randomir