代码之家  ›  专栏  ›  技术社区  ›  Matt Anderson

在bash脚本中嵌入短python脚本

  •  45
  • Matt Anderson  · 技术社区  · 16 年前

    我想在我的脚本中嵌入一个简短的脚本 .bash_profile . 做这样的事最好的方法是什么?

    到目前为止,我的解决方案是使用 -c exec 不管它读到什么 stdin

    function pyexec() {
        echo "$(/usr/bin/python -c 'import sys; exec sys.stdin.read()')"
    }
    
    function traildirs() {
        pyexec <<END
    trail=int('${1:-3}')
    import os
    home = os.path.abspath(os.environ['HOME'])
    cwd = os.environ['PWD']
    if cwd.startswith(home):
        cwd = cwd.replace(home, '~', 1)
    parts = cwd.split('/')
    joined = os.path.join(*parts[-trail:])
    if len(parts) <= trail and not joined.startswith('~'):
        joined = '/'+joined
    print joined
    END
    }
    
    export PS1="\h [\$(traildirs 2)] % "
    

    不过,这种方法闻起来有点滑稽,我想知道有什么办法可以替代这种方法。

    我的bash脚本编写技能非常初级,因此我特别感兴趣的是,从bash解释器的角度来看,我是否在做一些愚蠢的事情。

    9 回复  |  直到 16 年前
        1
  •  38
  •   Ned Deily    16 年前

    python解释器接受 - stdin 因此,您可以将对pyexec的调用替换为:

    python - <<END
    

    请参见命令行参考 here .

        2
  •  50
  •   Ricardo Cárdenes    16 年前

    为什么需要使用 -c ? 这对我很有用:

    python << END
    ... code ...
    END
    

        3
  •  27
  •   haridsv    7 年前

    使用bash的一个问题 here document stdin ,因此,如果您想将Python脚本用作过滤器,它将变得非常笨拙。另一种选择是使用 bash 的 process substitution

    ... | python <( echo '
    code here
    ' ) | ...
    

    这里是文件 在paren内部,如下所示:

    ... | python <(
    cat << "END"
    code here
    END
     ) | ...
    

    在脚本内部,您可以像平常一样从/到标准i/o(例如。, sys.stdin.readlines

    python -c credits ):

    read -r -d '' script <<-"EOF"
        code goes here prefixed by hard tab
    EOF
    python -c "$script"
    

    只需确保此处文档中每行的第一个字符是硬制表符。如果必须将其放入函数中,那么我将使用我在某处看到的以下技巧使其看起来对齐:

    function somefunc() {
        read -r -d '' script <<-"----EOF"
            code goes here prefixed by hard tab
    ----EOF
        python -c "$script"
    }
    
        4
  •  9
  •   Don Li    11 年前

    有时在这里使用文档不是一个好主意。另一种选择是使用python-c:

    py_script="
    import xml.etree.cElementTree as ET,sys
    ...
    "
    
    python -c "$py_script" arg1 arg2 ...
    
        5
  •  7
  •   desgua    12 年前

    #!/bin/bash
    
    ASDF="it didn't work"
    
    ASDF=`python <<END
    ASDF = 'it worked'
    print ASDF
    END`
    
    echo $ASDF
    
        6
  •  6
  •   AhHatem Adrian Lopez    8 年前

    准备使用输入复制粘贴示例:

    input="hello"
    output=`python <<END
    print "$input world";
    END`
    
    echo $output
    
        7
  •  2
  •   eric.frederich    16 年前

    有趣的。。。我现在也想要一个答案;-)

    他并不是问如何在bash脚本中执行python代码,而是问如何让python设置环境变量。

    把它放在bash脚本中,试着让它说“它成功了”。

    export ASDF="it didn't work"
    
    python <<END
    import os
    os.environ['ASDF'] = 'it worked'
    END
    
    echo $ASDF
    

    如果有解决这个问题的办法,我也很想看看。

        8
  •  1
  •   kuro    9 年前

    试试这个:

    #!/bin/bash
    a="test"
    python -c "print  '$a this is a test'.title()"
    
        9
  •  0
  •   fedeDev    12 年前

    如果您使用的是zsh,那么可以使用zsh的 join python - ):

    py_cmd=${(j:\n:)"${(f)$(
        # You can also add comments, as long as you balance quotes
        <<<'if var == "quoted text":'
        <<<'  print "great!"')}"}
    
    catch_output=$(echo $py_cmd | python -)
    

    您可以缩进Python代码段,因此它看起来比 EOF 或 <<END 在函数内部时的解决方案。