在运维中,尤其是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。

参考答案:



linux下用shell脚本还是python脚本_数据库



    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。

     

    参考程序:



    linux下用shell脚本还是python脚本_数据库


    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文件的内容:



    linux下用shell脚本还是python脚本_数据库



      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:



      linux下用shell脚本还是python脚本_数据库


      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)编写任务定时器:



      linux下用shell脚本还是python脚本_数据库



        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)第一种方法:



        linux下用shell脚本还是python脚本_数据库


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

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



        linux下用shell脚本还是python脚本_数据库

        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天前的备份文件



        linux下用shell脚本还是python脚本_数据库


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


         

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



        linux下用shell脚本还是python脚本_数据库

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



         

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



        linux下用shell脚本还是python脚本_数据库


        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查找



        linux下用shell脚本还是python脚本_数据库

        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脚本实现



        linux下用shell脚本还是python脚本_数据库

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


        ssh 批量上传文件



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

         

        一,scp上传不要输入密码

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

         

        二,ssh批量上传脚本

         

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



        linux下用shell脚本还是python脚本_数据库

        1. root@ubuntu:/home/zhangy# cat test    
        2. /home/zhangy/test/aaa    
        3. /home/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



        linux下用shell脚本还是python脚本_数据库


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


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

         

        3,上传的格式



        linux下用shell脚本还是python脚本_数据库



          1. ./file_upload.sh test 192.168.1.13



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



          0




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


           

          1. 转换文件大小写:

          A script to convert the specified filenames to lower case.

           



          linux下用shell脚本还是python脚本_数据库

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

           



          linux下用shell脚本还是python脚本_数据库


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


           

           

          3. 转GIF到PNG convert GIF files to PNG

           

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

           



          linux下用shell脚本还是python脚本_数据库


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

           



          linux下用shell脚本还是python脚本_数据库

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


           

           

          5. 字频率 Word Frequency

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

           



          linux下用shell脚本还是python脚本_数据库



            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

             

             



            linux下用shell脚本还是python脚本_数据库

            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



            linux下用shell脚本还是python脚本_数据库


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



             

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



            linux下用shell脚本还是python脚本_数据库

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

             

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



            linux下用shell脚本还是python脚本_数据库

            1. if test $# = 0  
            2. then  
            3. "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. "$PATH"|cut -d: -f$index`  
            13. "$directory/$program"  
            14. if test -x "$f"  
            15.         then  
            16. "$f"  
            17.             program_found=1  
            18.         fi  
            19.         index=`expr $index + 1`  
            20.     done  
            21. "$program not found"  
            22. done