当前位置:网站首页>Shell integrated application cases, archiving files, sending messages
Shell integrated application cases, archiving files, sending messages
2022-07-27 10:04:00 【Redamancy06】
List of articles
1. The archive
In practical production application , It is often necessary to archive and back up important data .
demand : Implement a script that archives and backs up the specified directory every day , Enter a directory name ( It doesn't end with /), Archive and save all files in the directory on a daily basis , And attach the filing date to the file name , Put it in /root/archive Next .
The archive command is used here : tar
You can add -c Option means archive , add -z Option means to compress at the same time , The resulting file suffix is .tar.gz
1.1 Script implementation

#!/bin/bash
# First, judge whether the number of input parameters is 1
if [ $# -ne 1 ]
then
echo " Wrong number of parameters ! You should enter a parameter , As the archive directory name "
exit
fi
# Get the directory name from the parameter
if [ -d $1 ]
then
echo
else
echo
echo " directory does not exist !"
echo
exit
fi
DIR_NAME=$(basename $1)
DIR_PATH=$(cd $(dirname $1); pwd)
# Get current date
FILE=archive_${
DIR_NAME}_$DATE.tar.gz
DEST=/root/archive/$FILE
# Start archiving catalog files
echo " Start filing ..."
echo
tar -czf $DEST $DIR_PATH/$DIR_NAME
if [ $? -eq 0 ]
then
echo
echo " Archive success !"
echo " Archive file is :$DEST"
echo
else
echo " There is a problem with archiving !"
echo
fi
exit
1.2 test



Then create a file 

Archive success
1.3 Regular filing
Use crontab
crontab The code in
0 2 * * * /root/scripts/daily_archive.sh /root/scripts

2. Send a message

This allows you to view users' online status , + Number represents that you can send messages ,mesg It's open 
mesg n Can be mesg close
mesg y Can be mesg open
2.1write Send a message
- write user name Console
[[email protected] scripts]# write cat pts/1


We can use Linux Self contained mesg and write Tools , Send messages to other users .
demand : Implement a script to quickly send messages to a user , Enter the user name as the first parameter , Followed directly by the message to be sent . The script needs to detect whether the user logs in to the system 、 Whether to turn on the message function , And whether the currently sent message is empty .
The script is implemented as follows :
#!/bin/bash
# Check whether the user is logged in
login_user=$(who | grep -i -m 1 $1 | awk '{print $1}')
if [ -z $login_user ]
then
echo "$1 Not online !"
echo " Script exit ..."
exit
fi
# Check whether the user turns on the message function
is_allwoed=$(who -T | grep -i -m 1 $1 | awk '{print $2}')
if [ "$is_allowed"x != "+"x ]
then
echo "$1 No message function is enabled "
echo " Script exit ..."
exit
fi
# Confirm whether there is a message sent
if [ -z $2 ]
then
echo "$1 No message sent "
echo " Script exit ..."
exit
fi
# Get the message to be sent from the parameter
whole_msg=$(echo $* | cut -d " " -f 2-)
# Get the terminal where the user logs in
user_terminal=$(who | grep -i -m 1 $1 | awk '{print $2}')
# Write the message to send
echo $whole_msg | write $login_user $user_terminal
if [ $? != 0 ]
then
echo " fail in send !"
else
echo " Send successfully !"
fi
exit


The end !!!
边栏推荐
- 3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
- Voice live broadcast system - Principles to be followed in developing push notifications
- Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
- Concurrent thread state transition
- 食品安全 | 无糖是真的没有糖吗?这些真相要知道
- 7/26 thinking +dp+ suffix array learning
- Exercises --- quick arrangement, merging, floating point number dichotomy
- 2016 outlook
- 吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
- Example of ICP registration for PCL
猜你喜欢

卸载CUDA11.1

二叉树习题总结

Anaconda安装(非常详细)

Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter

35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction

食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好

pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0

3D修复论文:Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks

Dcgan paper improvements + simplified code

3D restoration paper: shape painting using 3D generative advantageous networks and recurrent revolutionary networks
随机推荐
面试京东 T5,被按在地上摩擦,鬼知道我经历了什么?
吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解
About getter/setter methods
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Summary of binary tree exercises
WGAN、WGAN-GP、BigGAN
Easy to understand! Graphic go synergy principle and Practice
Explain knative cloud function framework in simple terms!
Meeting seating function of conference OA project & Implementation of meeting submission for approval
吃透Chisel语言.24.Chisel时序电路(四)——Chisel内存(Memory)详解
[SCM]源码管理 - perforce 分支的锁定
Shell综合应用案例,归档文件、发送消息
中高级试题」:MVCC 实现原理是什么?
语音直播系统——开发推送通知需要遵守的原则
Example of ICP registration for PCL
电机控制器中的MOS驱动
3D face reconstruction and dense alignment with position map progression network
A ride into Qinchuan -- a brief talk on how beego Autorouter works
npm常用命令
c'mon! Please don't ask me about ribbon's architecture principle during the interview