求linux下shell判断语句,要求当root登陆时,执行命令‘’当root退出时执行命令‘’。

如题所述

#!/bin/sh
#########0,表示登录,1表示未登录############
check_root()
{
who|awk
'{print
$1}'|grep
-w
"root"
>/dev/null
echo
$?
}
while
:
do
if
[
$(check_root)
-eq
"0"
];then
#######发现root后
执行命令
##########
echo
"root
login"
while
:
do
if
[
$(check_root)
-eq
1
];then
##########执行root退出命令
并跳出while循环##################
echo
"root
logout"
break
fi
sleep
1
done
fi
sleep
1
done
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-01-15
你好!
#!/bin/bash
if
[
`id
-u`
-eq
"0"
];
then
....
else
echo
"the
current
user
is
NOT
ROOT";
fi
前段时间看老外脚本的。支持sudu运行,呵呵
希望对你有所帮助,望采纳。