bashshell脚本无限循环登录脚本
当前正在尝试编写脚本以显示用户在网络上的登录和注销。目前的代码如下:bashshell脚本无限循环登录脚本,sh,infinite-loop,login-script,Sh,Infinite Loop,Login Script,当前正在尝试编写脚本以显示用户在网络上的登录和注销。目前的代码如下: echo "The current users are:" who | awk '{print $1}' | sort > tempfile1 cp tempfile1 tempfile2 more tempfile1 while true do who | awk '{print $1}' | sort > temp2 cmp -s tempfile1 tempfile2 case
echo "The current users are:"
who | awk '{print $1}' | sort > tempfile1
cp tempfile1 tempfile2
more tempfile1
while true
do
who | awk '{print $1}' | sort > temp2
cmp -s tempfile1 tempfile2
case "$?" in
0)
echo "No user has logged in/out in the last 3 seconds."
;;
1)
user=`comm -23 tempfile1 tempfile2`
file=`grep $user tempfile1 tempfile2 | cut -c 1-5`
[ $file == "tempfile1" ]
echo "User "$user" has logged out."
[ $file == "tempfile2" ];
echo "User "$user" has logged in."
;;
esac
rm tempfile1
mv tempfile2 tempfile1
sleep 3
done
运行脚本时,我会得到以下结果:
The current users are:
No user has logged in/out in the last 3 seconds.
mv: cannot stat ‘tempfile2’: No such file or directory
rm: cannot remove ‘tempfile1’: No such file or directory
mv: cannot stat ‘tempfile2’: No such file or directory
我相当肯定这段代码中的某个地方存在语法问题,但我是瞎子。与其他类似的例子相比,这类脚本毫无用处。如果有人能帮我指出我是多么的白痴,那将非常有帮助。干杯 在第一次循环结束时,您可以
rm tempfile1
然后mv tempfile2
。当您回到循环的顶部并执行cmp
时,您不会同时拥有这两个文件
who | awk'{print$1}'排序>temp2
应该是who |awk'{print$1}'排序>tempfile2
?(temp2
从未在其他任何地方引用过…我知道,我犯了一个愚蠢的错误哈哈。谢谢你指出这一点。