linux经典脚本实例,Linux常用Shell脚本珍藏
【实例简介】
Linux常用Shell脚本珍藏
(2)编写任务定时器:
C代码☆
1. echo 00 1 *k */bin/sh /usr/bin/fileback">:/root/etcbakcron
2. crontab /root/etcbakcron
3.或使用 crontab-e命令添加定时任务:
4.01** s */bin/sh /usr/bin/ fileback
6.有一普通用户想在每周日凌晨零点零分定期备份/user/ backup到/tmp目录下,该用户应如何做?
参考答案:
(1)第一种方法
代码☆
1.用户应使用 crontab-c命令创建 crontab文件。格式如下
2.00x
cp -r /user/backup /tmp
(2)第二种方
用户先在自己日录下新建文件fil,文件内容如下
C代码☆
1.0 * sun cp -r /user/backup /tmp
然后执行 crontab file使生效。
7.设计一个She11程序,在/ userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户
的权限为:读;文件所有者的权限为:读、写、执行:;文件所有者所在组的权限为:读、执行
参考答案:建立程序Pro16如下:
C代码☆
1. #! /bin/ sh
3. while[ i -le 50 1
5. if
d / userdata ]:then
6. mkdir -p /userdata/users
7. chmod 751/userdata/uscrSi
8. echo
user
e
(或i-S(($i+1))
10. else
11. mkdir /userdata
12. mkdir - p /userdata/user si
13. chmod 754 /userdata/user$i
14.echo“user$
5.1et“i=j+
(或i=S(($i+1))
16,fi
17. donc
8、 mysql各份实例,自动备份mysq1,并删除30人前的各份文件
C代码☆
1. #!/bin/sh
3. Auto backup mysql
4.#wugk2012-07-14
5. EPATH DEFINE
7. BAKDIR-/data/ backup, my sql/ datc +%Y-%m-%d
8. MYSQLDB=Www
9. MYSQLPW=backup
10. MYSQLUSR-backup
11
12. if[ SUID -ne 0 1; then
13. echo This script must use administrator or root user please exit
p
15. exit 0
16. fi
17
18. if[! -d SBAKDIR ] then
19. mkdir -p $ BAKDIR
20. else
21. echo This is SBAKDiR exists please exit..
22.sl
eep 2
3. exit
24.f
26. ###mysqldump backup mysql
28. /usr/bin/mysql dump -usMYSQLUSR -pSMYSQLPW-d $MYSQLDB > data/backup/mysql/ date +%Y
d
30
By cd SBAKDIR
tar -czf www mysql db. tar. gz * sql
32. cd $BAKdIR find
name“米.sq1” xargs rm-rf[$?-eq0]&echo“ This date+%Y
Dom-od resin baCKUP is SUCCESS
34. cd /data/backup/ my sql, find.-mtime +30 xargs rm
9、自动安装 nginx脚本,采用ease方式,选择方式,也可以根据实际需求改成自己想要的驸本
C代码☆
1. #!/bin/ sh
3. ###nginx install shell
1.##Wugk2012-07-11
5. ###PATH DEFINE
7. SOFT PATH-/data/sof
8. NGINX FILE=nginx-1. 2.0. tar gz
9. DowN Path=httP: //nginx. org/download/
11. if[ SUID-ne 0];then
12. echo This script must use administrator or root user please exit
Sleep
14. exit o
15,f
17. if[ -d SSOFT PATH ]; then
8. mkdir -p $SOFT PATH
19.fi
21. download O
23. cd SOFT PATH wget DOWN PATH/ SNGTNX FILE
24.}
26. install O
7.{
8. yum install pcre-devel-y
29. cd SOFT PATH tar xzf NGINX FILE ; cd nginx-1.2.0/&&./configure
3o prefix/usr/local/nginx
Eaewithhttpstubstatus_module-with-http-sslmodule
-eg 0 &&make &&make install
33. start ()
31.
35. Isof
80 S? -ne 0 &&/usr/local/nginx/ sbin/nginx
36.
37
38. stop O
40. ps -ef grep nginx grep-v grep awk [print $2), xargs kill-9
42.
43. exit O
44.{
15. cho s? cxit
46.}
47
48,### case menu###扦
49
50. case sl
1.download
52. download
55. install
56. install
556666666666
59. start
60. start
62. stop
4.;
0
68. echo "USAGE: o download or install or start or stop
9. exit
0. esac
10、批量解压tar脚本,批量解压zip并且建立当前目录。
C代码
1. #!/bin/sh
2. PATHl=/tmp/images
3. PATH2=/usr/www/images
4. for i in s sPATH1)/*
5. do
6. tar xvf S
C $PATH2
7. donc
这个脚本是针对所有tar文件在一个目录,但是实际情况中,有可能在下级或者更深的目求,我们可以使用find查找
代码☆
1. #! /bin/sh
2. PATH1-/ tmp/images
3. PATH2=/usr/www/images
4. for i in find PA
name
”*,tar
567
d
oao
kIf $i-C $PATH2
do
ne
如何是zip文件,例如123189.zip132342.zip等等批量文件,默认 unzip直接解压不带目身目录,意思是解压
123189.zip完当前目录就是图片,不能创建123189目录下并解压,可以用 shell肜本实现
C代码
1. #!/bin/sh
2. PATH1-/tmp/image
3. PATH2=/usr/www/images
4. cd $PATH1
5
6. for i in find
name
iprint
1. do
9. mkdir -p
PATH2Si
11. unzip -o. i zip
PATH2Si
12. done
原创文章转载请注明:Liux常用She胭本珍藏」专注Unix/inx领域
ssh批量上传文件
上传文件大多数用的是ftp,但是用ftp有一点不好,就是本地和远程的日录要对应,这样就要在多个日录下去切换,这样
挺麻烦的,如果不注意的话,很有可能传错。所以想了个办法利用scp来批量上传文件或者目求
,scp上传不要输入密码
如果要用scp来上传文件,第一步就要去掉scp上传时要输入密码。要不然就没办法批量上传∫。具体请参考:ssh不
用输入密
ssh批量上传脚本
1,要上传的文件列表放到一个tcst文件中
C代码☆
1. root @ubuntu: /home/zhangy# cat test
2./homc/zhangy/tost/aaa
3./homc/zhangy /test/nginx. conf
4
5. /home/zhangy/ test/test. sql
6. /home/zhangy/test/pa. txt
7./home/ zhangy/test/password
上面就要上传约文件。
2,批量上传的脚本
vim file upload. sh
代码☆
1. #!/bin/sh
3 date= date +oy %om od %H
456789
the
for filc in$(scd’/^$/d’$1)
//去掉空行
if[-f file
/普通文件
10
then
res= scp file $2: $file
//上传文件
z res
2345
//上传成功
then
echo file > $(DATE upload log
/上传成功的日志
elif d Sfile 1
//日录
17
then
rcs= scp -r Sfilc $2: $fil
19
z res
20
then
21
echo file > $(DATE) upload log
fi
one
25.el
echono file" > SDATE error log
27.fi
上传成功后,返回的是一个空行,上传不成功,什么都不返回
3,上传的格式
C代码
1.. /file upload. sh test 192. 168 1.13
tcst是上传列表文件,192.168.1.13文件要传到的地方。
转载请注明
作者:海底苍鹰
fuhl:http://b10g.51vip.com/linux/1356.htm1
1.转换文件大小写
A script to convert the spccificd filenames to lowcr casc
C代码
1.#!/b
t yoweri
3. convert all file names in the current directory to lower case
5, will ask for verification before overwriting an existing Ille rectories
only operates on plain files--does not change the name of d
6. for x in ls
7. do
f[! -f Sx ] then
continue
r,[A-ZI
Sx
fi
5. done
or
代码☆
1. if test #-0
2. then
345
cho"U
exit 1
1. for filename
8. do
new filename- echo"Sfilenam
tr z a-z
test Sfilename=Snew filename"&& contin
-r Snow filename
12345
then
echo $0: new filename exists" 1>&2
elif test -e Sfil
ende
the
nySfilename Snew file
else
echo$0: filename not found 1>&
9
20.d
2.看网站 Watch a Website
a script to repeated download a webpage until it matches a regex then notify an e-mail address.
For example to get e-mail when Kesha tickets (not for yourself of course) go on sale you might run
C代码☆
1.%watchwebsiteshhttp://ticketek.comau/kelss$]tha'andrewt@cse.unsw.edu.au
4. repeat seconds=300 #check every 5 minutes
6. if test $#
3
7. then
ur1=$1
email address-$3
11. else
cho"Usage: $0 url) regex> 1>&2
exit 1
f
45678
thile true
7. de
If wgct -0
ur
cgrp
regexp
cv/nu
then
cho gcncrated by $0
s Surl now matches Regexp" Semail address
f
23.
sleep repeat seconds
24. done
转GTF到 PnG convert gif files to png
This scripts converts GiF files to PNG files via the intermediate PPM format.
C代码☆
1.if[$*eq0]
2. then
echo"Usage: So files.. 1>&2
4.
exit 1
5.f
7. if type giftopnm 2>/ dev/null
h
then
echo"$0: conversion tool giftopnm not found
1>&2
0.
exit l
13. missing in.. defaults to in
14. for f
casc Sf in
18
ok, do nothing
19
21
echo gif2png: skipping f, not GIF
continue
23.
24.
esac
25
dir- dirname"S
base- basename sfm. gif
result= Sdir/Basc. png
29
30
giftopnm "Sf" pnmtopng result & echo wrote Result
31. done
4.计数 Counting
a utility script to print the sub-range of integers specified by its arguments
Useful to use on the command line or from other scripts
C代码☆
1. if test S#
then
start=1
4.
finish=$1
5. elif test $#+-2
6. then
start
finish=$2
9. else
10
cchoUsagc: so
1>82
cxit 1
12.fi
14. for argument in S@
15.do
echo"argument"egrep -v -?[0-9]+s'>/dev/null
then
echo$: argument Argument. is not an integer 1>82
exit I
22
23. number=Sstart
24. while test Snumber -le finish
25,do
echo number
27
number= expr number+
or number=S ((Snumber 1))
28. done
5.字频率 Word Frequency
Count the number of time each different word occurs in the files given as arguments
C代码☆
1.sed's//n/g"$@"
F convert to one word per line
2. tr a-z a-b
F map uppercase to lower case
3.sed"s/「a-z31//g
remove all characters cxcept a-z and
cgrcp-V
杆 remove empty lines
5. sort
place words in alphabetical order
6. unig
use unig to count how many times each word
occurs
F order words in frequency of occurrance
For cxamplc
C代码☆
1. cd /home/ cs2041/public html/lec/shell/examples
2. %./word frequency. sh dracula. txt tail
2124it
2
that
45678
n
2549he
2911a
3600of
4418to
9012
4740i
an
7843 the
6. Finding
【实例截图】
【核心代码】
linux经典脚本实例,Linux常用Shell脚本珍藏相关推荐
- linux 脚本中=$4,shell脚本实例,通向shell脚本大师的必经之路
概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...
- shell脚本实例,通向shell脚本大师的必经之路
概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...
- Linux常用Shell脚本珍藏
我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本.如何学好脚本,最关键的 ...
- Linux 常用Shell脚本,后端开发必备
Linux 常用Shell脚本,后端开发必备 文章目录 Linux 常用Shell脚本,后端开发必备 1 用户猜数字 2 查看有多少远程的 IP 在连接本机 3 helloworld 4 打印 tom ...
- linux脚本监控网卡流量,Shell脚本实现linux监控网卡实时流量[带宽]的方法
今天分享一个Linux网卡实时流量查看的shell脚本. 以下是脚本: #!/bin/bash if [ -z "$1" ]; then echo echo usage: $0 n ...
- 监控Oracle数据库的常用shell脚本
文章介绍了DBA每天在监控Oracle数据库方面的职责,讲述了如何通过shell脚本来完成这些重复的监控工作.本文首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron来定时执行 ...
- 监控Oracle数据库的常用shell脚本 ORACLE教程 教程作者:佚名 教程来源:不详 教程栏目:ORACLE教程
前言 这篇文章介绍了DBA每天在监控Oracle数据库方面的职责,讲述了如何通过shell脚本来完成这些重复的监控工作.本文首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron ...
- 常用shell脚本集合
常用shell脚本集合 !/bin/sh # 在脚本第一行脚本头 # sh为当前系统默认shell,可指定为bash等shell sh -x # 执行过程 sh -n # 检查语法 (a=bbk) # ...
- 大数据常用shell脚本之fl脚本
一.前置准备 CentOS7.jdk1.8.zookeeper-3.5.7.kafka-2.4.1.flume-1.9.0 想要完成本期视频中所有操作,需要以下准备: Flume安装及基本使用-视频教 ...
最新文章
- Seagull License Server 9.4 SR3 2781 完美激活(解决不能打印问题)
- 脉冲神经网络_【强基固本】脉冲神经网络(SNN)
- java web 静态_「Java Web」主页静态化的实现
- hydra mysql 爆破_Hydra(爆破神器)使用方法
- 冯仕堃:预训练模型哪家强?百度知识增强大模型探索实践!
- RMAN数据库恢复 之归档模式有(无)备份-丢失数据文件的恢复
- UVA11152 Safe Salutations【计算几何】
- 语音识别的原理_语音识别原理_语音识别原理框图 - 云+社区 - 腾讯云
- Part2-HttpClient官方教程-Chapter5-流利的API
- 使用postman解决浏览器POST测试时登录问题:未登录无法进行POST提交
- PPT制作手机手指滑动效果
- mysql 占比函数_MySQL通过分组计算百分比
- 密码疑云 (2)——RSA加密机制需要的数学知识
- PayPal贝宝集成
- mmclassification使用步骤与心得/ACCV实验记录
- csgo手机上看demo_CSGOdemo文件是什么 怎么去查看CSGOdemo文件
- 决策树(python)
- ASP.Net免费发送短信
- EBCDIC 与 GBK 的字符编码及其转换(转)
- 2022最全大数据学习路线(建议收藏)
热门文章
- python no module named pandas_【原创】大叔经验分享(11)python引入模块报错ImportError: No module named pandas numpy...
- 家装强电弱电布线图_家装电路改造注意事项是什么?深圳广田家透彻分析全搞定...
- cnn 回归 坐标 特征图_论文笔记 | CNN 是怎么学到图片绝对位置信息的
- 桩筏有限元中的弹性板计算_PKPM2010年11月结构技术问题汇总
- html判断ie6,jquery如何判断是否是ie?
- 天正坐标标注显示不全_高效率的天正CAD技巧,其实制图很简单!
- Vue-cli 自定义配置
- python FastDFS
- AudioBuffer
- 18 File Duplication and Pipes