[英]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.