我创建了一个 Bash 脚本,该脚本通过 crontab 运行,用于检查 Linux 主机上安装的 nmap 版本。问题是由于某种原因,检查工作不正常,它总是一次又一次地尝试安装 nmap...
#!/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 )
我的脚本出了什么问题?
最佳答案
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
由于错误是假的,!
让它成为真,你的代码每次都会运行。
关于linux - 在 Bash 中检查程序的版本等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798742/