我正在制作一个python脚本,它将自动检查计算机上是否安装了nmap,然后继续运行nmap。我遇到的一个问题是,当它运行时,它会返回/bin/sh: 1: [: missing ]
并且我还想知道如何将终端的输出管道返回到我的程序中。假设我运行hostname -I
如何复制输出并在脚本中为其分配一个变量名。谢谢,代码在下面
import os
import subprocess
def isInstalled(name):
cmd = """ if ! [ -x "$#(command -v """ + name + """)" ]; then
echo '0'
exit 0
fi"""
ret = subprocess.check_output(cmd, shell=True).strip()
if ret == b'0':
return False
return True
if isInstalled('nmap'):
print("Nmap is installed")
else:
print("nmap is uninstalled since quite mode is active auto install will")
考虑到这一点:
将代码更改为:
关于你的第二个问题,看看这个答案:https://stackoverflow.com/a/6657718/3589567
看起来您的默认shell是
sh
,但没有{a1}可用,所以请尝试在要编写的脚本中指定bash shebang#!/bin/bash
:或者可以在bash中使用double bracketsfor if else语句:
^{pr2}$相关问题 更多 >
编程相关推荐