无法在bash脚本中执行shell命令 [英] Cannot execute shell commands in bash script

查看:113
本文介绍了无法在bash脚本中执行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #!/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:第7行:find:未找到命令

find位于/bin下,该文件包含在我的PATH中.我还将将要执行此脚本的目录放在PATH中,但没有任何区别.

find is located under /bin, which is included in my PATH. I also put the directory this script was being executed in into the PATH but it didn't make a difference.

eval $ RSYSLOGCLIENTS 替换 echo .. 行也给了我同样的错误.

Replacing the echo.. line with eval $RSYSLOGCLIENTS also gave me the same error.

有人可以解释发生了什么事吗?

Can someone please explain what is happening?

注意:我认为这是非常糟糕的做法,但是此脚本位于root的主目录中.可能与此有关吗?

Note: I assume this is extremely bad practice, but this script is located in the home directory of root. Could this have something to do with it?

推荐答案

查找位于/bin下,该文件包含在我的PATH中

find is located under /bin, which is included in my PATH

不,不是.在脚本的第3行中,您的PATH被重新定义为:

No, it isn't. Your PATH is redefined in line 3 of the script to be:

PATH=/var/log

观察到 find 命令在之前有效,但在之后重新分配PATH无效:

Observe that the find command works before but not after PATH is reassigned:

$ 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.您不想覆盖它们.对内部脚本变量使用小写或至少混合大小写.

The general lesson here is, when defining shell variables for your script, never use all capitals. The shell uses all caps for its important variables, like PATH. You don't want to overwrite them. Use lower or, at least, mixed case for your internal script variables.

例如,为 path 变量分配了一个值,它不会影响外壳查找 find 的能力:

For example, the path variable is assigned a value and it does not affect the ability of the shell to find 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]+")
$

在外壳中,变量名称区分大小写,因此, PATH path 是独立的变量.

In shell, variable names are case-sensitive and, therefore, PATH and path are separate and independent variables.

这篇关于无法在bash脚本中执行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆