本篇主要写一些shell
脚本until
语句的使用。
计算1-50的和
#!/bin/bash
i=0
s=0
until [ $i -eq 51 ];do
let s+=i;let i++
done
echo $s
[[email protected] ~]# vim sum.sh
[[email protected] ~]# chmod +x sum.sh
[[email protected] ~]# ./sum.sh
1275
为指定用户发送在线消息
#!/bin/bash
username=$1
# 判断格式是否正确
if [ $# -lt 1 ] ;then
echo "Usage:`basename $0` <username> [message]"
exit 1
fi
# 判断用户是否存在
if grep "^$username:" /etc/passwd > /dev/null ;then :
else
echo "用户不存在"
exit 1
fi
# 判断用户是否在线,不在则每5s联系一次
until who|grep "$username" > /dev/null ;do
echo "用户不在线"
sleep 5
done
# 发送信息
mes=$*
echo $mes | write $username
[[email protected] ~]# vim message.sh
[[email protected] ~]# chmod +x message.sh
[[email protected] ~]# ./message.sh
Usage:message.sh <username> [message]
[[email protected] ~]# ./message.sh zhangsan hello
用户不存在
[[email protected] ~]# useradd zhangsan && echo "000000" | passwd --stdin zhangsan
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.
[[email protected] ~]# ./message.sh zhangsan hello
用户不在线
用户不在线
^C
[[email protected] ~]$
[[email protected] ~]# ./message.sh zhangsan hello
[[email protected] ~]$
Message from [email protected] on pts/0 at 02:25 ...
zhangsan hello
EOF
原文地址:https://www.cnblogs.com/llife/p/11633413.html
时间: 2024-11-29 08:05:38