程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python3 脚本中的复杂 Bash 循环结构大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Python3 脚本中的复杂 Bash 循环结构?

开发过程中遇到Python3 脚本中的复杂 Bash 循环结构的问题如何解决?下面主要结合日常开发的经验,给出你关于Python3 脚本中的复杂 Bash 循环结构的解决方法建议,希望对你解决Python3 脚本中的复杂 Bash 循环结构有所启发或帮助;

我想从 python3 脚本运行(复杂的)Bash while 循环。 我知道 os.subprocess 和 os.subprocess.check_output 在这种情况下有效,但我无法理解如何将 while 包含在 Python 子进程中。

while read -r line
do
    if [ "$(echo "$line" | cut -d : -f 7)" = "/bin/bash" ] && [ $(printf "$(echo "$line" | cut -d : -f 1)" | wc -C) -gt $mIDa ]
    then
        echo $line | cut -d : -f 1
    fi
done < /etc/passwd

我尝试了以下方法:

out=subprocess.check_output(""" while read -r line; do; if [ "$(echo "$line" | cut -d : -f 7)" = "/bin/bash" ] && [ $(printf "$(echo "$line" | cut -d : -f 1)" | wc -C) -gt $mIDa ]; then; echo $line | cut -d : -f 1; fi; done < /etc/passwd """,sHell=TruE)

解决方法

@H_489_16@

只需正常包含它。就像这样。无论如何,您都在使用 """ 引号。

out = subprocess.check_output("""
while read -r line
do
    if [ "$(echo "$line" | cut -d : -f 7)" = "/bin/bash" ] && [ $(printf "$(echo "$line" | cut -d : -f 1)" | wc -C) -gt $mida ]
    then
        echo $line | cut -d : -f 1
    fi
done < /etc/passwd
""",sHell=TruE)

注意事项:

  • 您应该在使用前导出 @H_169_5@mida 环境变量。如果未设置 $mida 变量将导致一些 [: something expected but not there 消息。
  • printf "$(stuff)" | wc -c?只需stuff | wc -c
  • 使用 http://sHellcheck.net
  • 检查您的脚本
  • 阅读https://mywiki.wooledge.org/BashFAQ/001
  • 阅读时只需使用 : 而不是使用 IFS 分割 cut 上的行
  • 也就是说,不要使用 sHell - 使用 python 并在 python 中编写逻辑。

大佬总结

以上是大佬教程为你收集整理的Python3 脚本中的复杂 Bash 循环结构全部内容,希望文章能够帮你解决Python3 脚本中的复杂 Bash 循环结构所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:循环结构