300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > linux经典脚本实例 Linux常用Shell脚本珍藏

linux经典脚本实例 Linux常用Shell脚本珍藏

时间:2019-09-18 08:53:29

相关推荐

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.#wugk-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.##Wugk-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:/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.%watchwebsiteshau/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

【实例截图】

【核心代码】

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。