[英]Cannot execute shell commands in bash script
#!/bin/bash
#general security monitoring
PATH=/var/log
echo "The IP addresses of users with more than 2 failed login attempts are:"
IPFAILEDLOGINS=$(grep "Failed password" /var/log/secure | cut -d: -f4 | awk '{print $6}' | uniq -c | awk '{if ($1>=2) print $2}')
echo "$IPFAILEDLOGINS"
RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
echo "The current rsyslog clients are: $RSYSLOGCLIENTS"
错误: ./securityanalysis.sh: line 7: find: command not found
查找位于/ bin下,该文件包含在我的PATH中。 我还将将要执行此脚本的目录放在PATH中,但没有任何区别。
用eval $RSYSLOGCLIENTS
替换echo..
行也给了我同样的错误。
有人可以解释发生了什么吗?
注意:我认为这是非常糟糕的做法,但是此脚本位于root的主目录中。 这可能与它有关吗?
查找位于/ bin下,该文件包含在我的PATH中
不,不是。 您的PATH在脚本的第3行中重新定义为:
PATH=/var/log
观察到find
命令在重新分配PATH 之前有效,但在重新分配PATH 之后无效:
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$ PATH=/var/log
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
bash: find: command not found
这里的一般教训是,在为脚本定义shell变量时, 切勿使用全部大写字母。 外壳程序将所有大写形式用作其重要变量,例如PATH。 您不想覆盖它们。 内部脚本变量使用小写或至少混合大小写。
例如,为path
变量分配了一个值,它不会影响shell查找find
的能力:
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$ path=/var/log
$ RSYSLOGCLIENTS=$(find /var/log -type d -regextype posix-egrep -regex ".*/([0-9]+\.){3}[0-9]+")
$
在shell中,变量名称区分大小写,因此PATH
和path
是独立的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.