Bash IF内部的变量是否可能投射到IF外部的变量上?
我编写了此脚本,用于检查某个文件是否已更改:Bash IF内部的变量是否可能投射到IF外部的变量上?,bash,shell,Bash,Shell,我编写了此脚本,用于检查某个文件是否已更改: #!/bin/bash path=$1 if [ -z "$path" ]; then echo "usage: $0 [path (required)]" 1>&2 exit 4 fi lastmodsecs=`stat --format='%Y' $path` lastmodsecshum=`date -d @$lastmodsecs` basedate=$newdate if [ $lastmodsecs !=
#!/bin/bash
path=$1
if [ -z "$path" ]; then
echo "usage: $0 [path (required)]" 1>&2
exit 4
fi
lastmodsecs=`stat --format='%Y' $path`
lastmodsecshum=`date -d @$lastmodsecs`
basedate=$newdate
if [ $lastmodsecs != $basedate ]; then
echo "CRITICAL: $path was last modified on $lastmodsecshum !"
newdate=`stat --format='%Y' $path`
exit 1
else
echo "OK: $path hasn't been modified since $lastmodsecshum \(last supervized change\)"
exit 0
fi
如果IF语句为true,我想用上次更改的unix时间设置$newdate变量,然后将其投影到刚好高于IF的$basedate变量,这可能吗
哔叽:
脚本现在看起来是这样的,结果是,如果文件已更改,则检查的状态保持在关键状态:/etc/passwd上次修改日期,并且由于某些原因,$persist文件未正确更新:
#!/bin/bash
path=$1
if [ -z "$path" ]; then
echo "usage: $0 [path (required)]" 1>&2
exit 4
fi
lastmodsecs=`stat --format='%Y' $path`
lastmodsecshum=`date -d @$lastmodsecs`
persist="/usr/local/share/applications/file"
if [ -z $persist ]
then newdate=`stat --format='%Y' $path`
else read newdate < $persist
fi
basedate=$newdate
if [ $lastmodsecs != $basedate ]; then
echo "CRITICAL: $path was last modified on $lastmodsecshum !"
echo $lastmodsecs > $persist
exit 1
else
echo "OK: $path hasn't been modified since $lastmodsecshum \(supervized change\)"
exit 0
fi
看起来您的代码应该在循环中运行,newdate最初是上次运行的值。通常,如果循环在脚本中,这可以正常工作:
...
# newdate first initialisation
newdate=`stat --format='%Y' $path`
while true
do lastmodsecs=`stat --format='%Y' $path`
lastmodsecshum=`date -d @$lastmodsecs`
basedate=$newdate
if [ $lastmodsecs != $basedate ]; then
echo "CRITICAL: $path was last modified on $lastmodsecshum !"
newdate=$lastmodsecs
exit 1
else
echo "OK: $path hasn't been modified since $lastmodsecshum \(last supervized change\)"
fi
done
但正如我看到的退出0和退出1一样,这个脚本也用于向调用方返回状态。您不能使用环境,因为不允许程序修改其父环境。所以,唯一的可能是让调用方管理newdate,或者将其持久化到一个文件中。最后一个很简单,无需在调用者中进行修改:
...
persist=/path/to/private/persist/file
# eventual first time initialization or get newdat from $persist
if [ -z $persist ]
then newdate=`stat --format='%Y' $path`
else read newdate < $persist
fi
...
basedate=$newdate
if [ $lastmodsecs != $basedate ]; then
echo "CRITICAL: $path was last modified on $lastmodsecshum !"
echo $lastmodsecs > $persist
exit 1
else
echo "OK: $path hasn't been modified since $lastmodsecshum \(last supervized change\)"
exit 0
fi
当然,这些测试是您的,因为您正在谈论Nagios…要检查文件日期是否比上次检查的时间更晚,请尝试以下方法:
#!/bin/bash
lastchecked="/tmp/lastchecked.state"
file="/my/file"
# compare file date against date of last check
[[ "$file" -nt "$lastchecked" ]] && echo "$file has been modified since last check"
# remember time when this check was done
touch "$lastchecked"
你说的项目是什么意思,分配?是的,可能是我不知道正确的术语。。。可以分配。那么在设置newdate之后,您想对basedate执行什么操作?你不再在脚本中使用basedate了。我想我有点迷糊了,我的目标是检查一个文件是否已更改,以及是否必须在何时显示。但这是一个Nagios check_命令,我正在尝试为下一次运行准备该命令,因此$basedate变量必须包含新的上次修改时间。。。我很确定我做得不好…检查文件是否已更改。。。从什么时候开始变了?自从上次你执行那个脚本以来?从指定的时间开始?当前,您似乎比较了当前文件日期和当前文件日期。很可能这两个日期是相同的。我已经用脚本的结果编辑了我的问题。