日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux常用Shell脚本珍藏

發(fā)布時間:2025/3/21 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux常用Shell脚本珍藏 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們在運(yùn)維中,尤其是linux運(yùn)維,都知道腳本的重要性,腳本會讓我們的 運(yùn)維事半功倍,所以學(xué)會寫腳本是我們每個linux運(yùn)維必須學(xué)會的一門功課,這里收藏linux運(yùn)維常用的腳本。如何學(xué)好腳本,最關(guān)鍵的是就是大量的練習(xí) 和實(shí)踐。根據(jù)以下腳本我們可以拓展,這樣我們提高的很快!舉一反三!

?

1.用Shell編程,判斷一文件是不是字符設(shè)備文件,如果是將其拷貝到 /dev 目錄下。


參考程序:

C代碼 ?
  • #!/bin/sh??
  • FILENAME=??
  • echo?“Input?file?name:”??
  • read?FILENAME??
  • if?[?-c?"$FILENAME"?]??
  • then??
  • cp?$FILENAME?/dev??
  • fi???
  • ?

    2.設(shè)計一個shell程序,添加一個新組為class1,然后添加屬于這個組的30個用戶,用戶名的形式為stdxx,其中xx從01到30。


    參考答案:

    C代碼 ?
  • #!/bin/sh??
  • i=1??
  • groupadd?class1??
  • while?[?$i?-le?30?]??
  • do??
  • if?[?$i?-le?9?]?;then??
  • USERNAME=stu0${i}??
  • else??
  • USERNAME=stu${i}??
  • fi??
  • useradd?$USERNAME??
  • mkdir?/home/$USERNAME??
  • chown?-R?$USERNAME?/home/$USERNAME??
  • chgrp?-R?class1?/home/$USERNAME??
  • i=$(($i+1))??
  • done??

  • 3.編寫shell程序,實(shí)現(xiàn)自動刪除50個賬號的功能。賬號名為stud1至stud50。

    ?

    參考程序:

    C代碼 ?
  • #!/bin/sh??
  • i=1??
  • while?[?$i?-le?50?]??
  • do??
  • userdel?-r?stud${i}??
  • i=$(($i+1?))??
  • done??

  • 4.某系統(tǒng)管理員需每天做一定的重復(fù)工作,請按照下列要求,編制一個解決方案:
    (1)在下午4 :50刪除/abc目錄下的全部子目錄和全部文件;
    (2)從早8:00~下午6:00每小時讀取/xyz目錄下x1文件中每行第一個域的全部數(shù)據(jù)加入到/backup目錄下的bak01.txt文件內(nèi);
    (3)每逢星期一下午5:50將/data目錄下的所有目錄和文件歸檔并壓縮為文件:backup.tar.gz;
    (4)在下午5:55將IDE接口的CD-ROM卸載(假設(shè):CD-ROM的設(shè)備名為hdc);
    (5)在早晨8:00前開機(jī)后啟動。


    參考答案:
    解決方案:
    (1)用vi創(chuàng)建編輯一個名為prgx的crontab文件;
    (2)prgx文件的內(nèi)容:

    C代碼 ?
  • 50?16?*?*?*?rm?-r?/abc/*??
  • 0?8-18/1?*?*?*?cut?-f1?/xyz/x1?>;>;?/backup/bak01.txt??
  • 50?17?*?*?*?tar?zcvf?backup.tar.gz?/data??
  • 55?17?*?*?*?umount?/dev/hdc??
  • ?
    (3)由超級用戶登錄,用crontab執(zhí)行 prgx文件中的內(nèi)容:
    root@xxx:#crontab prgx;在每日早晨8:00之前開機(jī)后即可自動啟動crontab。


    5.設(shè)計一個shell程序,在每月第一天備份并壓縮/etc目錄的所有內(nèi)容,存放在/root/bak目錄里,且文件名為如下形式y(tǒng)ymmdd_etc,yy為年,mm為月,dd為日。Shell程序fileback存放在/usr/bin目錄下。

    ?

    參考答案:
    (1)編寫shell程序fileback:

    C代碼 ?
  • #!/bin/sh??
  • DIRNAME=`ls?/root?|?grep?bak`??
  • if?[?-z?"$DIRNAME"?]?;?then??
  • mkdir?/root/bak??
  • cd?/root/bak??
  • fi??
  • YY=`date?+%y`??
  • MM=`date?+%m`??
  • DD=`date?+%d`??
  • BACKETC=$YY$MM$DD_etc.tar.gz??
  • tar?zcvf?$BACKETC?/etc??
  • echo?“fileback?finished!”??
  • ?
    (2)編寫任務(wù)定時器:

    C代碼 ?
  • echo?“0?0?1?*?*?/bin/sh?/usr/bin/fileback”?>;?/root/etcbakcron??
  • crontab?/root/etcbakcron??
  • 或使用crontab?-e?命令添加定時任務(wù):??
  • 0?1?*?*?*?/bin/sh?/usr/bin/fileback??
  • ?
    6.有一普通用戶想在每周日凌晨零點(diǎn)零分定期備份/user/backup到/tmp目錄下,該用戶應(yīng)如何做?

    ?

    參考答案:

    ?

    (1)第一種方法:

    C代碼 ?
  • 用戶應(yīng)使用crontab?–e?命令創(chuàng)建crontab文件。格式如下:??
  • 0?0?*?*?sun?cp?–r?/user/backup?/tmp??
  • ?
    (2)第二種方法:
    用戶先在自己目錄下新建文件file,文件內(nèi)容如下:

    C代碼 ?
  • 0?*?*?sun?cp?–r?/user/backup?/tmp??
  • 然后執(zhí)行 crontab file 使生效。

    ?
    7.設(shè)計一個Shell程序,在/userdata目錄下建立50個目錄,即user1~user50,并設(shè)置每個目錄的權(quán)限,其中其他用戶的權(quán)限為:讀;文件所有者的權(quán)限為:讀、寫、執(zhí)行;文件所有者所在組的權(quán)限為:讀、執(zhí)行。

    ?

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

    C代碼 ?
  • #!/bin/sh??
  • i=1??
  • while?[?i?-le?50?]??
  • do??
  • if?[?-d?/userdata?];then??
  • mkdir?-p?/userdata/user$i??
  • chmod?754?/userdata/user$i??
  • echo?“user$i”??
  • let?“i?=?i?+?1″?(或i=$(($i+1))??
  • else??
  • mkdir?/userdata??
  • mkdir?-p?/userdata/user$i??
  • chmod?754?/userdata/user$i??
  • echo?“user$i”??
  • let?“i?=?i?+?1″?(或i=$(($i+1))??
  • fi??
  • done??
  • ?

    8、mysql備份實(shí)例,自動備份mysql,并刪除30天前的備份文件

    C代碼 ?
  • #!/bin/sh??
  • ??
  • #auto?backup?mysql??
  • #wugk??2012-07-14??
  • #PATH?DEFINE??
  • ??
  • BAKDIR=/data/backup/mysql/`date?+%Y-%m-%d`??
  • MYSQLDB=www??
  • MYSQLPW=backup??
  • MYSQLUSR=backup??
  • ??
  • if[?$UID?-ne?0?];then??
  • echo?This?script?must?use?administrator?or?root?user?,please?exit!??
  • sleep?2??
  • exit?0??
  • fi??
  • ??
  • if[?!?-d?$BAKDIR?];then??
  • mkdir?-p?$BAKDIR??
  • else??
  • echo?This?is?$BAKDIR?exists?,please?exit?….??
  • sleep?2??
  • exit??
  • fi??
  • ??
  • ###mysqldump?backup?mysql??
  • ??
  • /usr/bin/mysqldump?-u$MYSQLUSR?-p$MYSQLPW?-d?$MYSQLDB?>/data/backup/mysql/`date?+%Y-%m-%d`/www_db.sql??
  • ??
  • cd?$BAKDIR?;?tar?-czf??www_mysql_db.tar.gz?*.sql??
  • ??
  • cd?$BAKDIR?;find??.?-name?“*.sql”?|xargs?rm?-rf[?$??-eq?0?]&&echo?“This?`date?+%Y-%m-%d`?RESIN?BACKUP?is?SUCCESS”??
  • ??
  • cd?/data/backup/mysql/?;find?.?-mtime?+30?|xargs?rm?-rf??
  • ?

    9、自動安裝Nginx腳本,采用case方式,選擇方式,也可以根據(jù)實(shí)際需求改成自己想要的腳本

    C代碼 ?
  • #!/bin/sh??
  • ??
  • ###nginx?install?shell??
  • ###wugk?2012-07-14??
  • ###PATH?DEFINE??
  • ??
  • SOFT_PATH=/data/soft/??
  • NGINX_FILE=nginx-1.2.0.tar.gz??
  • DOWN_PATH=http://nginx.org/download/??
  • ??
  • if[?$UID?-ne?0?];then??
  • echo?This?script?must?use?administrator?or?root?user?,please?exit!??
  • sleep?2??
  • exit?0??
  • fi??
  • ??
  • if[?!?-d?$SOFT_PATH?];then??
  • mkdir?-p?$SOFT_PATH??
  • fi??
  • ??
  • download?()??
  • {??
  • cd?$SOFT_PATH?;wget?$DOWN_PATH/$NGINX_FILE??
  • }??
  • ??
  • install?()??
  • {??
  • yum?install?pcre-devel?-y??
  • 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??
  • [?$??-eq?0?]&&make?&&make?install??
  • }??
  • ??
  • start?()??
  • {??
  • lsof?-i?:80[?$??-ne?0?]&&/usr/local/nginx/sbin/nginx??
  • }??
  • ??
  • stop?()??
  • {??
  • ps?-ef?|grep?nginx?|grep?-v?grep?|awk?‘{print?$2}’|xargs?kill?-9??
  • }??
  • ??
  • exit?()??
  • {??
  • echo?$??;exit??
  • }??
  • ??
  • ###case?menu?#####??
  • ??
  • case?$1?in??
  • download?)??
  • download??
  • ;;??
  • ??
  • install?)??
  • install??
  • ;;??
  • ??
  • start?)??
  • start??
  • ;;??
  • stop?)??
  • stop??
  • ;;??
  • ??
  • *?)??
  • ??
  • echo?“USAGE:$0?{download?or?install?or?start?or?stop}”??
  • exit??
  • esac??
  • ?

    10、批量解壓tar腳本,批量解壓zip并且建立當(dāng)前目錄。

    C代碼 ?
  • #!/bin/sh??
  • PATH1=/tmp/images??
  • PATH2=/usr/www/images??
  • for?i?in?`ls?${PATH1}/*`??
  • do??
  • tar?xvf??$i??-C?$PATH2??
  • done??
  • ?

    這個腳本是針對所有tar文件在一個目錄,但是實(shí)際情況中,有可能在下級或者更深的目錄,我們可以使用find查找

    C代碼 ?
  • #!/bin/sh??
  • PATH1=/tmp/images??
  • PATH2=/usr/www/images??
  • for?i?in?`find??$PATH1??-name??”*.tar”?`??
  • do??
  • tar?xvf??$i??-C?$PATH2??
  • done??
  • ?

    如何是zip文件,例如123189.zip 132342.zip 等等批量文件,默認(rèn)unzip直接解壓不帶自身目錄,意思是解壓123189.zip完當(dāng)前目錄就是圖片,不能創(chuàng)建123189目錄下并解壓,可以用shell腳本實(shí)現(xiàn)

    C代碼 ?
  • #!/bin/sh??
  • PATH1=/tmp/images??
  • PATH2=/usr/www/images??
  • cd?$PATH1??
  • ??
  • for?i?in?`find??.?-name??”*.zip”|awk??-F.??{print?$2}?`??
  • do??
  • ??
  • mkdir?-p???PATH2$i??
  • ??
  • unzip?-o??.$i.zip??-d???PATH2$i??
  • done???
  • ?

    原創(chuàng)文章轉(zhuǎn)載請注明: Linux常用Shell腳本珍藏 | 專注Unix/Linux領(lǐng)域

    ?

    ssh 批量上傳文件

    上傳文件大多數(shù)用的是ftp,但是用ftp有一點(diǎn)不好,就是本地和遠(yuǎn)程的目錄要對應(yīng),這樣就要在多個目錄下去切換,這樣挺麻煩的,如果不注意的話,很有可能傳錯。所以想了個辦法利用scp來批量上傳文件或者目錄。

    ?

    一,scp上傳不要輸入密碼

    如果要用scp來上傳文件,第一步就要去掉scp上傳時要輸入密碼。要不然就沒辦法批量上傳了。具體請參考:ssh 不用輸入密碼

    ?

    二,ssh批量上傳腳本

    ?

    1,要上傳的文件列表放到一個test文件中

    C代碼 ?
  • root@ubuntu:/home/zhangy#?cat?test????
  • /home/zhangy/test/aaa????
  • /home/zhangy/test/nginx.conf????
  • ????
  • /home/zhangy/test/test.sql????
  • /home/zhangy/test/pa.txt????
  • /home/zhangy/test/password????
  • ?

    上面就要上傳的文件。

    ?

    2,批量上傳的腳本

    ?

    vim file_upload.sh

    C代碼 ?
  • #!/bin/sh??
  • ??
  • DATE=`date?+%Y_%m_%d_%H`??
  • ??
  • if?[?$1?]??
  • then??
  • ??for?file?in?$(sed?'/^$/d'?$1)?????//去掉空行??
  • ??do??
  • ????if?[?-f?$file?]?????????????????//普通文件??
  • ????then??
  • ??????res=`scp?$file?$2:$file`??????//上傳文件??
  • ??????if?[?-z?$res?]????????????????//上傳成功??
  • ??????then??
  • ????????echo?$file?>>?${DATE}_upload.log???//上傳成功的日志??
  • ??????fi??
  • ????elif?[?-d?$file?]??????????????//目錄??
  • ????then??
  • ??????res=`scp?-r?$file?$2:$file`??
  • ??????if?[?-z?$res?]??
  • ??????then??
  • ????????echo?$file?>>?${DATE}_upload.log??
  • ??????fi??
  • ????fi??
  • ??done??
  • else??
  • ??echo?"no?file"?>>?${DATE}_error.log??
  • fi??
  • ?

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

    ?

    3,上傳的格式

    C代碼 ?
  • ./file_upload.sh?test?192.168.1.13????
  • test是上傳列表文件,192.168.1.13文件要傳到的地方。

    0

    轉(zhuǎn)載請注明
    作者:海底蒼鷹
    地址:http://blog.51yip.com/linux/1356.html

    ?

    1. 轉(zhuǎn)換文件大小寫:

    A script to convert the specified filenames to lower case.

    ?

    C代碼 ?
  • #!/bin/sh??
  • ?#?lowerit??
  • #?convert?all?file?names?in?the?current?directory?to?lower?case??
  • #?only?operates?on?plain?files--does?not?change?the?name?of?directories??
  • #?will?ask?for?verification?before?overwriting?an?existing?file??
  • for?x?in?`ls`??
  • do??
  • ????if?[?!?-f?$x?];?then??
  • ????????continue??
  • ????fi??
  • ????lc=`echo?$x??|?tr?'[A-Z]'?'[a-z]'`??
  • ????if?[?$lc?!=?$x?];?then??
  • ????????mv?-i?$x?$lc??
  • ????fi??
  • done??
  • ?

    ?

    or

    ?

    C代碼 ?
  • if?test?$#?=?0??
  • then??
  • ????echo?"Usage?$0:?<files>"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • for?filename?in?"$@"??
  • do??
  • ????new_filename=`echo?"$filename"?|?tr?A-Z?a-z`??
  • ????test?"$filename"?=?"$new_filename"?&&?continue??
  • ????if?test?-r?"$new_filename"??
  • ????then??
  • ????????echo?"$0:?$new_filename?exists"?1>&2??
  • ????elif?test?-e?"$filename"??
  • ????then??
  • ????????mv?"$filename"?"$new_filename"??
  • ????else??
  • ????????echo?"$0:?$filename?not?found"?1>&2??
  • ????fi??
  • done??
  • ?

    ?

    2. 看網(wǎng)站 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代碼 ?
  • %?watch_website.sh?http://ticketek.com.au/?'Ke[sS$]+ha'?andrewt@cse.unsw.edu.au??
  • ??
  • ??
  • repeat_seconds=300??#check?every?5?minutes??
  • ??
  • if?test?$#?=?3??
  • then??
  • ????url=$1??
  • ????regexp=$2??
  • ????email_address=$3??
  • else??
  • ????echo?"Usage:?$0?<url>?<regex>"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • while?true??
  • do??
  • ????if?wget?-O-?-q?"$url"|egrep?"$regexp"?>/dev/null??
  • ????then??
  • ????????echo?"Generated?by?$0"?|?mail?-s?"$url?now?matches?$regexp"?$email_address??
  • ????????exit?0??
  • ????fi??
  • ????sleep?$repeat_seconds??
  • done??
  • ?

    ?

    3. 轉(zhuǎn)GIF到PNG convert GIF files to PNG

    ?

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

    ?

    C代碼 ?
  • if?[?$#?-eq?0?]??
  • then??
  • ????echo?"Usage:?$0?files..."?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • if?!?type?giftopnm?2>/dev/null??
  • then??
  • ????echo?"$0:?conversion?tool?giftopnm?not?found?"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • #?missing?"in?..."?defaults?to?in?"$@"??
  • for?f??
  • do??
  • ????case?"$f"?in??
  • ????*.gif)??
  • ????????#?OK,?do?nothing??
  • ????????;;??
  • ????*)??
  • ????????echo?"gif2png:?skipping?$f,?not?GIF"??
  • ????????continue??
  • ????????;;??
  • ????esac??
  • ??
  • ????dir=`dirname?"$f"`??
  • ????base=`basename?"$f"?.gif`??
  • ????result="$dir/$base.png"??
  • ??
  • ????giftopnm?"$f"?|?pnmtopng?>?$result?&&?echo?"wrote?$result"??
  • done??
  • ?

    ?

    4.? 計數(shù) 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代碼 ?
  • if?test?$#?=?1??
  • then??
  • ????start=1??
  • ????finish=$1??
  • elif?test?$#?=?2??
  • then??
  • ????start=$1??
  • ????finish=$2??
  • else??
  • ????echo?"Usage:?$0?<start>?<finish>"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • for?argument?in?"$@"??
  • do??
  • ????if?echo?"$argument"|egrep?-v?'^-?[0-9]+$'?>/dev/null??
  • ????then??
  • ????????echo?"$0:?argument?'$argument'?is?not?an?integer"?1>&2??
  • ????????exit?1??
  • ????fi??
  • done??
  • ??
  • number=$start??
  • while?test?$number?-le?$finish??
  • do??
  • ????echo?$number??
  • ????number=`expr?$number?+?1`????#?or?number=$(($number?+?1))??
  • done??
  • ?

    ?

    5. 字頻率 Word Frequency

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

    ?

    C代碼 ?
  • sed?'s/?/\n/g'?"$@"|??????#?convert?to?one?word?per?line??
  • tr?A-Z?a-z|???????????????#?map?uppercase?to?lower?case??
  • sed?"s/[^a-z']//g"|???????#?remove?all?characters?except?a-z?and?'????
  • egrep?-v?'^$'|?????????????#?remove?empty?lines??
  • sort|?????????????????????#?place?words?in?alphabetical?order??
  • uniq?-c|??????????????????#?use?uniq?to?count?how?many?times?each?word?occurs??
  • sort?-n???????????????????#?order?words?in?frequency?of?occurrance??
  • ?For example

    ?

    ?

    C代碼 ?
  • %?cd?/home/cs2041/public_html/lec/shell/examples??
  • %?./word_frequency.sh?dracula.txt|tail??
  • ???2124?it??
  • ???2440?that??
  • ???2486?in??
  • ???2549?he??
  • ???2911?a??
  • ???3600?of??
  • ???4448?to??
  • ???4740?i??
  • ???5833?and??
  • ???7843?the??
  • ?

    ?

    6. Finding

    ?

    Search $PATH for the specified programs

    C代碼 ?
  • if?test?$#?=?0??
  • then??
  • ????echo?"Usage?$0:?<program>"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • for?program?in?"$@"??
  • do??
  • ????program_found=''??
  • ????for?directory?in?`echo?"$PATH"?|?tr?':'?'?'`??
  • ????do??
  • ????????f="$directory/$program"??
  • ????????if?test?-x?"$f"??
  • ????????then??
  • ????????????ls?-ld?"$f"??
  • ????????????program_found=1??
  • ????????fi??
  • ????done??
  • ????if?test?-z?$program_found??
  • ????then??
  • ????????echo?"$program?not?found"??
  • ????fi??
  • done??
  • ?

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

    C代碼 ?
  • if?test?$#?=?0??
  • then??
  • ????echo?"Usage?$0:?<program>"?1>&2??
  • ????exit?1??
  • fi??
  • ??
  • for?program?in?"$@"??
  • do??
  • ????echo?"$PATH"|??
  • ????tr?':'?'\n'|??
  • ????while?read?directory??
  • ????do??
  • ????????f="$directory/$program"??
  • ????????if?test?-x?"$f"??
  • ????????then??
  • ????????????ls?-ld?"$f"??
  • ????????fi??
  • ????done|??
  • ????egrep?'.'?||?echo?"$program?not?found"??
  • done??
  • ?

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

    C代碼 ?
  • if?test?$#?=?0??
  • then??
  • ????echo?"Usage?$0:?<program>"?1>&2??
  • ????exit?1??
  • fi??
  • for?program?in?"$@"??
  • do??
  • ????n_path_components=`echo?$PATH|tr?-d?-c?:|wc?-c`??
  • ????index=1??
  • ????while?test?$index?-le?$n_path_components??
  • ????do??
  • ????????directory=`echo?"$PATH"|cut?-d:?-f$index`??
  • ????????f="$directory/$program"??
  • ????????if?test?-x?"$f"??
  • ????????then??
  • ????????????ls?-ld?"$f"??
  • ????????????program_found=1??
  • ????????fi??
  • ????????index=`expr?$index?+?1`??
  • ????done??
  • ????test?-n?$program_found?||?echo?"$program?not?found"??
  • done??
  • ?

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

    總結(jié)

    以上是生活随笔為你收集整理的Linux常用Shell脚本珍藏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。