当前位置:网站首页>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 !!!
边栏推荐
- QT learning (II) -.Pro file explanation
- 历时一年,论文终于被国际顶会接收了
- Qt 学习(二) —— Qt Creator简单介绍
- 3D face reconstruction and dense alignment with position map progression network
- 吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
- Meeting seating function of conference OA project & Implementation of meeting submission for approval
- 安装了HAL库如何恢复原来的版本
- Overview of PCL modules (1.6)
- 【云原生 • DevOps】一文掌握容器管理工具 Rancher
- 吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解
猜你喜欢

Interview JD T5, was pressed on the ground friction, who knows what I experienced?

语音直播系统——开发推送通知需要遵守的原则

去 OPPO 面试,被问麻了

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

Oracle RAC 19C PDB instance is down

吃透Chisel语言.24.Chisel时序电路(四)——Chisel内存(Memory)详解

Gbase 8A MPP cluster capacity expansion practice

Redis 为什么这么快?Redis 的线程模型与 Redis 多线程
![[cloud native • Devops] master the container management tool rancher](/img/6f/50deaf053c86486e52d2c2c7310ed2.png)
[cloud native • Devops] master the container management tool rancher

并发之线程状态转换
随机推荐
Simple use of tflite
Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction
Practice and exploration of overseas site Seata of ant group
食品安全 | 无糖是真的没有糖吗?这些真相要知道
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Exercises --- quick arrangement, merging, floating point number dichotomy
NFT系统开发-教程
吃透Chisel语言.22.Chisel时序电路(二)——Chisel计数器(Counter)详解:计数器、定时器和脉宽调制
Live countdown 3 days sofachannel 29 P2P based file and image acceleration system Dragonfly
flash闪存使用和STM32CUBEMX安装教程【第三天】
pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
Interview Essentials: shrimp skin server 15 consecutive questions
面试必备:虾皮服务端15连问
Shell的read 读取控制台输入、read的使用
并发之线程状态转换
A ride into Qinchuan -- a brief talk on how beego Autorouter works
Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数
3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network