1

I'm making a python script that will automatically check if nmap is installed on a computer and then proceed to run nmap. One problem I have is when it is run it comes back with /bin/sh: 1: [: missing ] and I was also wondering how one would pipe the output of a terminal back into my program. Let's say I run hostname -I how can I copy the output and assign it a variable name in my script. thanks the code is below

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")
Alejandro Blasco
  • 1,295
  • 2
  • 20
  • 24
coding679
  • 13
  • 1
  • 4
  • This is not a linux problem, but a shell problem: please adjust tags. – greybeard Mar 02 '19 at 23:52
  • Your code really should not give this error. However, with a tiny modification it will: if you write `[ -x "$#(command -v """ + name + """)"]; then` (without a space between `)"` and `]`) then it will give this error. Can you please copy-paste your code from this question into a new file and double check? – that other guy Mar 03 '19 at 04:56

2 Answers2

2

Looks like your default shell is sh that doesn't have test utility available, so try to specify bash shebang #!/bin/bash in script to be composed:

def isInstalled(name):
    cmd = """#!/bin/bash
    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

Or you can use double brackets for if-else statement in bash:

if [[ some expression ]]
then
    some code
fi
Artsiom Praneuski
  • 2,259
  • 16
  • 24
  • under return True I would put "if isInstalled('nmap'): " right? – coding679 Mar 02 '19 at 23:17
  • It's great that this was accepted, but adding a shebang in this case does not matter and will not make the script run with Bash. I suspect OP simply reformatted the code to make it prettier for posting, and unintentionally inserted a missing space before the `]` thereby fixing the original error – that other guy Mar 03 '19 at 04:57
0

There is a problem in your indentation and the blank line between print and else.

Take into account this:

Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.

Change your code to this:

if isInstalled('nmap'):
    print("Nmap is installed")
else:
    print("nmap is uninstalled since quite mode is active auto install will")

Regarding to your second question, take a look at this answer: https://stackoverflow.com/a/6657718/3589567

Alejandro Blasco
  • 1,295
  • 2
  • 20
  • 24