2016-06-13 35 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 

由於錯誤是假的,!使其真正和你的代碼運行每次。

相關問題