linux - 在 Bash 中检查程序的版本等等

标签 linux bash

我创建了一个 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 )

我的脚本出了什么问题?

最佳答案

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

由于错误是假的,! 让它成为真,你的代码每次都会运行。

关于linux - 在 Bash 中检查程序的版本等等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798742/

相关文章:

linux - PLINK 命令执行但未创建文件

bash - 使用 grep 查找文件并在编辑器中打开

PHP 删除 Windows ^M 字符

linux - SVN 就地导入和结帐

linux - 查找命令只扫描子文件夹而不是只复制最后一天的文件

sql - 存储过程和数据导出自动化

python - 管道命令在 python Fabric 中不起作用

git - 如何在 Git 中查找最新的非 merge 提交消息?

c - gdb watchpoint 不触发内存地址

linux - linux 文件描述符限制如何工作?