我想在bash脚本中嵌入短python脚本的文本,比如说,我的.bash_profile.做这样的事情最好的方法是什么?
我到目前为止的解决方案是使用-c选项调用python解释器,并告诉解释器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)] % "
Run Code Online (Sandbox Code Playgroud)
这种方法虽然闻起来有点滑稽,但我想知道这样做的替代方法可能是什么.
我的bash脚本编写技巧非常简陋,所以我特别感兴趣的是,从bash解释器的角度来看,我是否正在做一些愚蠢的事情.
Ric*_*nes 43
你为什么要用-c?这对我有用:
python << END
... code ...
END
Run Code Online (Sandbox Code Playgroud)
无需任何额外的东西.
Ned*_*ily 33
python解释器-在命令行上接受作为同义词,stdin因此您可以使用以下命令替换对pyexec的调用:
python - <<END
Run Code Online (Sandbox Code Playgroud)
请参阅此处的命令行参考.
har*_*dsv 16
使用bash的一个问题here document是脚本然后传递给Python stdin,所以如果你想使用Python脚本作为过滤器,它就变得难以处理.一种选择是使用bash's process substitution,这样的东西:
... | python <( echo '
code here
' ) | ...
Run Code Online (Sandbox Code Playgroud)
如果脚本太长,你也可以here document在paren中使用,如下所示:
... | python <(
cat << "END"
code here
END
) | ...
Run Code Online (Sandbox Code Playgroud)
在脚本内部,您可以像往常一样从/向标准i/o进行读/写(例如,sys.stdin.readlines吞噬所有输入).
另外,python -c可以像其他答案中提到的那样使用,但是我喜欢这样做以便格式化,同时仍然尊重Python的缩进规则(信用):
read -r -d '' script <<-"EOF"
code goes here prefixed by hard tab
EOF
python -c "$script"
Run Code Online (Sandbox Code Playgroud)
只需确保此处文档中每行的第一个字符是一个硬标签.如果你必须将它放在一个函数中,那么我使用我在某处看到的下面的技巧使它看起来是对齐的:
function somefunc() {
read -r -d '' script <<-"----EOF"
code goes here prefixed by hard tab
----EOF
python -c "$script"
}
Run Code Online (Sandbox Code Playgroud)
有时在这里使用文档并不是一个好主意.另一种方法是使用python -c:
py_script="
import xml.etree.cElementTree as ET,sys
...
"
python -c "$py_script" arg1 arg2 ...
Run Code Online (Sandbox Code Playgroud)
如果你需要在bash脚本中使用python的输出,你可以这样做:
#!/bin/bash
ASDF="it didn't work"
ASDF=`python <<END
ASDF = 'it worked'
print ASDF
END`
echo $ASDF
Run Code Online (Sandbox Code Playgroud)
准备好使用输入复制粘贴示例:
input="hello"
output=`python <<END
print "$input world";
END`
echo $output
Run Code Online (Sandbox Code Playgroud)