二十五、发送消息脚本

最后一章了,真没想到能把《linux命令行与shell脚本编程大全》看完,目前也差不多只是通读了一遍,例子敲了一遍,还有sed跟gawk还落下没看,不过还是很棒了。

下一回合就是把整本书再练习一遍,融会贯通。

这一节写的是发送消息脚本,也还不错挺好玩的。

确定登录用户

第一列用户名,第二列用户所在终端,第三列用户登入系统时间

[root@tzPC ~]# who
root     tty1         2020-09-08 22:31
root     pts/0        2020-09-09 17:09 
root     pts/1        2020-09-10 15:08 
tz       pts/2        2020-09-10 15:09 

启用消息功能

[root@tzPC ~]# who -T
root     + tty1         2020-09-08 22:31
root     + pts/0        2020-09-09 17:09 
root     - pts/1        2020-09-10 15:08 
tz       + pts/2        2020-09-10 15:09 

+号表示已启用-号表示未启用

启用跟禁用接收消息功能

[root@tzPC ~]# mesg y #启用当前用户接收消息功能
[root@tzPC ~]# mesg n #禁用当前用户接收消息功能

注意双方都需要启用消息功能。

向其他用户发送消息

write命令向对方发送消息,需要对方用户名跟登录终端名

注意这是单方向发送,tz用户只能接收,root只能发送

Ctrl+D结束

[root@tzPC ~]# who
root     tty1         2020-09-08 22:31
root     pts/0        2020-09-09 17:09 
root     pts/1        2020-09-10 15:08 
tz       pts/2        2020-09-10 15:09 
[root@tzPC ~]# write tz pts/2 
Hello TZ!
How are you?

检查用户是否登录

-i 忽略大小写

-m 最多匹配多少个,防止有多个用户登录,1参数表示只匹配1个

[tz@tzPC ~]$ who -T | grep -i -m 2 root
root     + tty1         2020-09-08 22:31
root     + pts/0        2020-09-09 17:09 (192.168.0.123)
[tz@tzPC ~]$ who -T | grep -i -m 1 root
root     + tty1         2020-09-08 22:31

检查用户是否登录脚本如下

#Determine if user is logged on:
logged_on=$(who | grep -i -m 1 $1 | gawk '{print $1}')

检查用户是否接收消息

gawk '{print $2}'表示取第二列

[tz@tzPC ~]$ who -T | grep -i -m 1 root
root     + tty1         2020-09-08 22:31
[tz@tzPC ~]$ who -T | grep -i -m 1 root | gawk '{print $2}'
+

检查用户是否接收消息脚本如下

#Determine if user allows messaging:
allowed=$(who -T | grep -i -m 1 $1 | gawk '{print $2}')
if [ $allowd != "+" ]
then
    echo "$1 does not allowing messaging."
    echo "Exiting script..."
    exit
fi

检查是否包含要发送的消息

#Determine if a message was included:
if [ -z $2 ]
then    
    echo "No message parameter included."
    echo "Exiting script..."
    exit
fi

发送简单消息

如下就能将一个单词,或者引号里所有的字符串发给对方了。

[root@tzPC ~]# echo "boss is comming" | write tz pts/2

部分脚本如下

#Send message to user:
uterminal=$(who | grep -i -m 1 $1 | gawk '{print $2}') #获取用户登录终端名
echo $2 | write $logged_on $uterminal 

如果想不使用引号发送长消息即包括空格的消息则需要使用shift。

#Save the username parameter
#首先要保存第一个用户名参数,因为shift一旦使用,命令行第一个参数就丢失了
muser=$1
shift
while [ -n "$1" ] #此时$1是原命令行上的第二个参数
do 
    whole_message=$whole_message' '$1
    shift
done

整个脚本如下

[root@tzPC 26Unit]# cat mu.sh 
#!/bin/bash
#mu.sh - Send a Message to a particular特定 user
#Save the username parameter
muser=$1 #保存命令行第一个参数,即要发送给谁的用户名
#Determine if user is logged on:
logged_on=$(who | grep -i -m 1 $muser | gawk '{print $1}')
if [ -z $logged_on ]
then
    echo "$muser is not logged on."
    echo "Exiting script..."
    exit
fi
#Determine if user allows messaging:
allowed=$(who -T | grep -i -m 1 $muser | gawk '{print $2}')
if [ $allowed != "+" ]
then
    echo "$muser does not allowing messaging."
    echo "Exiting script..."
fi
#Determine if a message was included:
if [ -z $2 ]
then
    echo "No message parameter included."
    echo "Exiting script..."
    exit
fi
#Determine if there is more to the message:
shift
while [ -n "$1" ] #注意上一行的shift将$1移动没了,现在的$1其实是原来的$2参数
do
    whole_message=$whole_message' '$1
    shift
done
#Send message to user:
uterminal=$(who | grep -i -m 1 $muser | gawk '{print $2}')
echo $whole_message | write $logged_on $uterminal
exit

效果如下

#root
[root@tzPC 26Unit]# bash mu.sh tz Boss is comming!

#tz
Message from root@tzPC on pts/1 at 16:47 ...
Boss is comming!
EOF

 学习来自:《Linux命令行与Shell脚本大全 第3版》第26章

今天的学习是为了以后的工作更加的轻松!
原文地址:https://www.cnblogs.com/tz90/p/13646696.html