Linux常用Shell脚本珍藏【转载】

时间:2023-02-09 06:32:57


我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个linux运维必须学会的一门功课,这里收藏linux运维常用的脚本。如何学好脚本,最关键的是就是大量的练习 和实践。根据以下脚本我们可以拓展,这样我们提高的很快!举一反三!

1.用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下。


参考程序:

  1. #!/bin/sh
  2. FILENAME=
  3. echo “Input file name:”
  4. read FILENAME
  5. if [ -c "$FILENAME" ]
  6. then
  7. cp $FILENAME /dev
  8. fi

2.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。


参考答案:

  1. #!/bin/sh
  2. i=1
  3. groupadd class1
  4. while [ $i -le 30 ]
  5. do
  6. if [ $i -le 9 ] ;then
  7. USERNAME=stu0${i}
  8. else
  9. USERNAME=stu${i}
  10. fi
  11. useradd $USERNAME
  12. mkdir /home/$USERNAME
  13. chown -R $USERNAME /home/$USERNAME
  14. chgrp -R class1 /home/$USERNAME
  15. i=$(($i+1))
  16. done


3.编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。

参考程序:

  1. #!/bin/sh
  2. i=1
  3. while [ $i -le 50 ]
  4. do
  5. userdel -r stud${i}
  6. i=$(($i+1 ))
  7. done


4.某系统管理员需每天做一定的重复工作,请按照下列要求,编制一个解决方案:
(1)在下午4 :50删除/abc目录下的全部子目录和全部文件;
(2)从早8:00~下午6:00每小时读取/xyz目录下x1文件中每行第一个域的全部数据加入到/backup目录下的bak01.txt文件内;
(3)每逢星期一下午5:50将/data目录下的所有目录和文件归档并压缩为文件:backup.tar.gz;
(4)在下午5:55将IDE接口的CD-ROM卸载(假设:CD-ROM的设备名为hdc);
(5)在早晨8:00前开机后启动。

参考答案:
解决方案:
(1)用vi创建编辑一个名为prgx的crontab文件;
(2)prgx文件的内容:

  1. 50 16 * * * rm -r /abc/*
  2. 0 8-18/1 * * * cut -f1 /xyz/x1 >;>; /backup/bak01.txt
  3. 50 17 * * * tar zcvf backup.tar.gz /data
  4. 55 17 * * * umount /dev/hdc

(3)由超级用户登录,用crontab执行 prgx文件中的内容:
root@xxx:#crontab prgx;在每日早晨8:00之前开机后即可自动启动crontab。


5.设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下。

参考答案:
(1)编写shell程序fileback:

  1. #!/bin/sh
  2. DIRNAME=`ls /root | grep bak`
  3. if [ -z "$DIRNAME" ] ; then
  4. mkdir /root/bak
  5. cd /root/bak
  6. fi
  7. YY=`date +%y`
  8. MM=`date +%m`
  9. DD=`date +%d`
  10. BACKETC=$YY$MM$DD_etc.tar.gz
  11. tar zcvf $BACKETC /etc
  12. echo “fileback finished!”

(2)编写任务定时器:

  1. echo “0 0 1 * * /bin/sh /usr/bin/fileback” >; /root/etcbakcron
  2. crontab /root/etcbakcron
  3. 或使用crontab -e 命令添加定时任务:
  4. 0 1 * * * /bin/sh /usr/bin/fileback


6.有一普通用户想在每周日凌晨零点零分定期备份/user/backup到/tmp目录下,该用户应如何做?

参考答案:

(1)第一种方法:

  1. 用户应使用crontab –e 命令创建crontab文件。格式如下:
  2. 0 0 * * sun cp –r /user/backup /tmp

(2)第二种方法:
用户先在自己目录下新建文件file,文件内容如下:

  1. 0 * * sun cp –r /user/backup /tmp

然后执行 crontab file 使生效。


7.设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。

参考答案: 建立程序 Pro16如下:

  1. #!/bin/sh
  2. i=1
  3. while [ i -le 50 ]
  4. do
  5. if [ -d /userdata ];then
  6. mkdir -p /userdata/user$i
  7. chmod 754 /userdata/user$i
  8. echo “user$i”
  9. let “i = i + 1″ (或i=$(($i+1))
  10. else
  11. mkdir /userdata
  12. mkdir -p /userdata/user$i
  13. chmod 754 /userdata/user$i
  14. echo “user$i”
  15. let “i = i + 1″ (或i=$(($i+1))
  16. fi
  17. done

8、mysql备份实例,自动备份mysql,并删除30天前的备份文件

  1. #!/bin/sh
  2. #auto backup mysql
  3. #wugk  2012-07-14
  4. #PATH DEFINE
  5. BAKDIR=/data/backup/mysql/`date +%Y-%m-%d`
  6. MYSQLDB=www
  7. MYSQLPW=backup
  8. MYSQLUSR=backup
  9. if[ $UID -ne 0 ];then
  10. echo This script must use administrator or root user ,please exit!
  11. sleep 2
  12. exit 0
  13. fi
  14. if[ ! -d $BAKDIR ];then
  15. mkdir -p $BAKDIR
  16. else
  17. echo This is $BAKDIR exists ,please exit ….
  18. sleep 2
  19. exit
  20. fi
  21. ###mysqldump backup mysql
  22. /usr/bin/mysqldump -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >/data/backup/mysql/`date +%Y-%m-%d`/www_db.sql
  23. cd $BAKDIR ; tar -czf  www_mysql_db.tar.gz *.sql
  24. cd $BAKDIR ;find  . -name “*.sql” |xargs rm -rf[ $? -eq 0 ]&&echo “This `date +%Y-%m-%d` RESIN BACKUP is SUCCESS”
  25. cd /data/backup/mysql/ ;find . -mtime +30 |xargs rm -rf

9、自动安装Nginx脚本,采用case方式,选择方式,也可以根据实际需求改成自己想要的脚本

  1. #!/bin/sh
  2. ###nginx install shell
  3. ###wugk 2012-07-14
  4. ###PATH DEFINE
  5. SOFT_PATH=/data/soft/
  6. NGINX_FILE=nginx-1.2.0.tar.gz
  7. DOWN_PATH=http://nginx.org/download/
  8. if[ $UID -ne 0 ];then
  9. echo This script must use administrator or root user ,please exit!
  10. sleep 2
  11. exit 0
  12. fi
  13. if[ ! -d $SOFT_PATH ];then
  14. mkdir -p $SOFT_PATH
  15. fi
  16. download ()
  17. {
  18. cd $SOFT_PATH ;wget $DOWN_PATH/$NGINX_FILE
  19. }
  20. install ()
  21. {
  22. yum install pcre-devel -y
  23. cd $SOFT_PATH ;tar xzf $NGINX_FILE ;cd nginx-1.2.0/ &&./configure –prefix=/usr/local/nginx/ –with-http_stub_status_module –with-http_ssl_module
  24. [ $? -eq 0 ]&&make &&make install
  25. }
  26. start ()
  27. {
  28. lsof -i :80[ $? -ne 0 ]&&/usr/local/nginx/sbin/nginx
  29. }
  30. stop ()
  31. {
  32. ps -ef |grep nginx |grep -v grep |awk ‘{print $2}’|xargs kill -9
  33. }
  34. exit ()
  35. {
  36. echo $? ;exit
  37. }
  38. ###case menu #####
  39. case $1 in
  40. download )
  41. download
  42. ;;
  43. install )
  44. install
  45. ;;
  46. start )
  47. start
  48. ;;
  49. stop )
  50. stop
  51. ;;
  52. * )
  53. echo “USAGE:$0 {download or install or start or stop}”
  54. exit
  55. esac

10、批量解压tar脚本,批量解压zip并且建立当前目录。

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. for i in `ls ${PATH1}/*`
  5. do
  6. tar xvf  $i  -C $PATH2
  7. done

这个脚本是针对所有tar文件在一个目录,但是实际情况中,有可能在下级或者更深的目录,我们可以使用find查找

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. for i in `find  $PATH1  -name  ”*.tar” `
  5. do
  6. tar xvf  $i  -C $PATH2
  7. done

如何是zip文件,例如123189.zip 132342.zip 等等批量文件,默认unzip直接解压不带自身目录,意思是解压123189.zip完当前目录就是图片,不能创建123189目录下并解压,可以用shell脚本实现

  1. #!/bin/sh
  2. PATH1=/tmp/images
  3. PATH2=/usr/www/images
  4. cd $PATH1
  5. for i in `find  . -name  ”*.zip”|awk  -F.  {print $2} `
  6. do
  7. mkdir -p   PATH2$i
  8. unzip -o  .$i.zip  -d   PATH2$i
  9. done

原创文章转载请注明: Linux常用Shell脚本珍藏 | 专注Unix/Linux领域

ssh 批量上传文件

上传文件大多数用的是ftp,但是用ftp有一点不好,就是本地和远程的目录要对应,这样就要在多个目录下去切换,这样挺麻烦的,如果不注意的话,很有可能传错。所以想了个办法利用scp来批量上传文件或者目录。

一,scp上传不要输入密码

如果要用scp来上传文件,第一步就要去掉scp上传时要输入密码。要不然就没办法批量上传了。具体请参考:ssh 不用输入密码

二,ssh批量上传脚本

1,要上传的文件列表放到一个test文件中

  1. root@ubuntu:/home/zhangy# cat test
  2. /home/zhangy/test/aaa
  3. /home/zhangy/test/nginx.conf
  4. /home/zhangy/test/test.sql
  5. /home/zhangy/test/pa.txt
  6. /home/zhangy/test/password

上面就要上传的文件。

2,批量上传的脚本

vim file_upload.sh

  1. #!/bin/sh
  2. DATE=`date +%Y_%m_%d_%H`
  3. if [ $1 ]
  4. then
  5. for file in $(sed '/^$/d' $1)     //去掉空行
  6. do
  7. if [ -f $file ]                 //普通文件
  8. then
  9. res=`scp $file $2:$file`      //上传文件
  10. if [ -z $res ]                //上传成功
  11. then
  12. echo $file >> ${DATE}_upload.log   //上传成功的日志
  13. fi
  14. elif [ -d $file ]              //目录
  15. then
  16. res=`scp -r $file $2:$file`
  17. if [ -z $res ]
  18. then
  19. echo $file >> ${DATE}_upload.log
  20. fi
  21. fi
  22. done
  23. else
  24. echo "no file" >> ${DATE}_error.log
  25. fi

上传成功后,返回的是一个空行,上传不成功,什么都不返回

3,上传的格式

  1. ./file_upload.sh test 192.168.1.13

test是上传列表文件,192.168.1.13文件要传到的地方。

转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/1356.html

1. 转换文件大小写:

A script to convert the specified filenames to lower case.

  1. #!/bin/sh
  2. # lowerit
  3. # convert all file names in the current directory to lower case
  4. # only operates on plain files--does not change the name of directories
  5. # will ask for verification before overwriting an existing file
  6. for x in `ls`
  7. do
  8. if [ ! -f $x ]; then
  9. continue
  10. fi
  11. lc=`echo $x  | tr '[A-Z]' '[a-z]'`
  12. if [ $lc != $x ]; then
  13. mv -i $x $lc
  14. fi
  15. done

or

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <files>" 1>&2
  4. exit 1
  5. fi
  6. for filename in "$@"
  7. do
  8. new_filename=`echo "$filename" | tr A-Z a-z`
  9. test "$filename" = "$new_filename" && continue
  10. if test -r "$new_filename"
  11. then
  12. echo "$0: $new_filename exists" 1>&2
  13. elif test -e "$filename"
  14. then
  15. mv "$filename" "$new_filename"
  16. else
  17. echo "$0: $filename not found" 1>&2
  18. fi
  19. done

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:

  1. % watch_website.sh http://ticketek.com.au/ 'Ke[sS$]+ha' andrewt@cse.unsw.edu.au
  2. repeat_seconds=300  #check every 5 minutes
  3. if test $# = 3
  4. then
  5. url=$1
  6. regexp=$2
  7. email_address=$3
  8. else
  9. echo "Usage: $0 <url> <regex>" 1>&2
  10. exit 1
  11. fi
  12. while true
  13. do
  14. if wget -O- -q "$url"|egrep "$regexp" >/dev/null
  15. then
  16. echo "Generated by $0" | mail -s "$url now matches $regexp" $email_address
  17. exit 0
  18. fi
  19. sleep $repeat_seconds
  20. done

3. 转GIF到PNG convert GIF files to PNG

This scripts converts GIF files to PNG files via the intermediate PPM format.

  1. if [ $# -eq 0 ]
  2. then
  3. echo "Usage: $0 files..." 1>&2
  4. exit 1
  5. fi
  6. if ! type giftopnm 2>/dev/null
  7. then
  8. echo "$0: conversion tool giftopnm not found " 1>&2
  9. exit 1
  10. fi
  11. # missing "in ..." defaults to in "$@"
  12. for f
  13. do
  14. case "$f" in
  15. *.gif)
  16. # OK, do nothing
  17. ;;
  18. *)
  19. echo "gif2png: skipping $f, not GIF"
  20. continue
  21. ;;
  22. esac
  23. dir=`dirname "$f"`
  24. base=`basename "$f" .gif`
  25. result="$dir/$base.png"
  26. giftopnm "$f" | pnmtopng > $result && echo "wrote $result"
  27. 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

  1. if test $# = 1
  2. then
  3. start=1
  4. finish=$1
  5. elif test $# = 2
  6. then
  7. start=$1
  8. finish=$2
  9. else
  10. echo "Usage: $0 <start> <finish>" 1>&2
  11. exit 1
  12. fi
  13. for argument in "$@"
  14. do
  15. if echo "$argument"|egrep -v '^-?[0-9]+$' >/dev/null
  16. then
  17. echo "$0: argument '$argument' is not an integer" 1>&2
  18. exit 1
  19. fi
  20. done
  21. number=$start
  22. while test $number -le $finish
  23. do
  24. echo $number
  25. number=`expr $number + 1`    # or number=$(($number + 1))
  26. done

5. 字频率 Word Frequency

Count the number of time each different word occurs in the files given as arguments.

  1. sed 's/ /\n/g' "$@"|      # convert to one word per line
  2. tr A-Z a-z|               # map uppercase to lower case
  3. sed "s/[^a-z']//g"|       # remove all characters except a-z and '
  4. egrep -v '^$'|             # remove empty lines
  5. sort|                     # place words in alphabetical order
  6. uniq -c|                  # use uniq to count how many times each word occurs
  7. sort -n                   # order words in frequency of occurrance

For example

  1. % cd /home/cs2041/public_html/lec/shell/examples
  2. % ./word_frequency.sh dracula.txt|tail
  3. 2124 it
  4. 2440 that
  5. 2486 in
  6. 2549 he
  7. 2911 a
  8. 3600 of
  9. 4448 to
  10. 4740 i
  11. 5833 and
  12. 7843 the

6. Finding

Search $PATH for the specified programs

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <program>" 1>&2
  4. exit 1
  5. fi
  6. for program in "$@"
  7. do
  8. program_found=''
  9. for directory in `echo "$PATH" | tr ':' ' '`
  10. do
  11. f="$directory/$program"
  12. if test -x "$f"
  13. then
  14. ls -ld "$f"
  15. program_found=1
  16. fi
  17. done
  18. if test -z $program_found
  19. then
  20. echo "$program not found"
  21. fi
  22. done

Alternative implementation using while, and a cute use of grep and ||

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <program>" 1>&2
  4. exit 1
  5. fi
  6. for program in "$@"
  7. do
  8. echo "$PATH"|
  9. tr ':' '\n'|
  10. while read directory
  11. do
  12. f="$directory/$program"
  13. if test -x "$f"
  14. then
  15. ls -ld "$f"
  16. fi
  17. done|
  18. egrep '.' || echo "$program not found"
  19. done

And another implementation using while, and a cute use of grep and ||

  1. if test $# = 0
  2. then
  3. echo "Usage $0: <program>" 1>&2
  4. exit 1
  5. fi
  6. for program in "$@"
  7. do
  8. n_path_components=`echo $PATH|tr -d -c :|wc -c`
  9. index=1
  10. while test $index -le $n_path_components
  11. do
  12. directory=`echo "$PATH"|cut -d: -f$index`
  13. f="$directory/$program"
  14. if test -x "$f"
  15. then
  16. ls -ld "$f"
  17. program_found=1
  18. fi
  19. index=`expr $index + 1`
  20. done
  21. test -n $program_found || echo "$program not found"
  22. done

来源: http://www.cse.unsw.edu.au/~cs2041/12s2/lec/shell/examples.notes.html