EOF % . run.-6ren">
gpt4 book ai didi

python - 在 python 中轻松创建 bash 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:27 31 4
gpt4 key购买 nike

我正在寻找一些将创建和执行 bash 脚本的测试。 Bash 本身有一个很好的方法来做到这一点:

 % cat > run.sh << EOF
> echo "I ran this"
> EOF
% . run.sh
I ran this

在 Python 中我可以这样做:

 with open ('run.sh', 'w') as rsh:
rsh.write('echo "I ran this"\n')
-- etc ---

这对于 Python 中的短脚本来说很好,但我想知道是否有一些我不知道的技术让我做一些我可以在 bash 中做的事情。

最佳答案

你可以这样做

#! /usr/bin/env python

with open ('run.sh', 'w') as rsh:
rsh.write('''\
#! /bin/bash
echo "I ran this"
echo "more lines"
''')

关于python - 在 python 中轻松创建 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516592/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com