2016-06-13 37 views
0

我已经创建了一个bash脚本,它通过一个crontab运行,该脚本检查linux主机上安装的nmap版本。问题是,由于某种原因,检查工作不正常,它总是试图一次又一次地安装NMAP ...检查程序的版本,所以在bash中的东西

#!/bin/sh 
if ! $(nmap --version | grep -q "7.12"); then 
    wget https://nmap.org/dist/nmap-7.12.tar.bz2 -P /tmp/ 
    cd /tmp && bzip2 -cd nmap-7.12.tar.bz2 | tar xvf - 
    cd nmap-7.12 
    ./configure --without-zenmap 
    make 
    make install 
    cd .. 
    rm nmap-7.12.tar.bz2 
    rm -rf nmap-7.12 
    reboot 
fi 

如果我检查,看看是否作业运行(这是它应该一次,但从来没有一次因为版本应与第二次)这是...

$> ps aux | grep nmap 
root  27696 15.4 0.3 2940 1464 ?  R 16:31 0:00 /bin/bash ./configure --disable-option-checking --prefix=/usr/local --without-zenmap --cache-file=/dev/null --srcdir=. --no-create --no-recursion 

运行命令行收益率检查(无-q):

$> nmap --version | grep "7.12" 
Nmap version 7.12 (https://nmap.org) 

什么是搞砸了我的脚本PT?

回答

3

ShellCheck说:

Line 2: 
if ! $(nmap --version | grep -q "7.12"); then 
    ^-- SC2091: Remove surrounding $() to avoid executing output. 

做到这一点,正确的做法就是:

if ! nmap --version | grep -q "7.12"; then 

你试图找到字符串Nmap version 7.12 (https://nmap.org),而且由于$(..)的它,然后尝试运行,作为一个命令。这导致你大概应该在问题记录下来,包括一个错误:

Nmap: command not found 

由于错误是假的,!使其真正和你的代码运行每次。

相关问题