username=$1
freq=$2
checkuser()
{
if who grep "$1"
then
sleep 60
fi
}
if [ -n "$1" ]
then
echo "Enter username"
read username
checkuser
echo -e "$1 is logged on \a"
echo -e "$1 logged in at `date`">>LOG
checkuser
else
echo "User is not logged on"
fi
我需要将第二个参数集成到我的代码中,它允许用户指定脚本应该在什么时间之后检查谁登录。我目前将它设置为 60 秒,这需要是默认频率。我尝试使用其他功能但无济于事。我想到了这样的事情......
if [ "$2" -ne 0 ]
then
freq=$2
else
freq=60
感谢威廉,这非常有帮助!我稍微更改了代码并想出了这个。I now need to add a 3rd argument "X" which when selected just sends a message to the LOGFILE and not to the screen. 我做了尝试,但没有按预期进行。
username=$1
freq=${2:-10}
X=$3
checkuser()
{
whoami|grep "$1";
}
while checkuser "$username"
do
echo -e "$1 is logged on \a"
echo "$1 logged in at `date`">>LOGFILE
sleep $freq
exit 0
done
echo "User is not logged in"
if [ "$3" -ne 1 ]
then
echo "$1 logged in at `date`"LOGFILE
fi