Free mind

          Be fresh and eager every morning, and tired and satisfied every night.
          posts - 39, comments - 2, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          常用腳本命令集

          Posted on 2007-02-13 22:47 morphis 閱讀(247) 評論(0)  編輯  收藏 所屬分類: 2. System

          常用腳本命令集

          如何用腳本實(shí)現(xiàn)分割文件

          #!/bin/bash

          if [ $# -ne 2 ]; then
          ? ?? ???echo 'Usage: split file size(in bytes)'
          ? ?? ???exit
          fi

          file=$1
          size=$2

          if [ ! -f $file ]; then
          ? ?? ???echo "$file doesn't exist"
          ? ?? ???exit
          fi

          #TODO: test if $size is a valid integer

          filesize=`/bin/ls -l $file | awk '{print $5}'`
          echo filesize: $filesize

          let pieces=$filesize/$size
          let remain=$filesize-$pieces*$size
          if [ $remain -gt 0 ]; then
          ? ?? ???let pieces=$pieces+1
          fi
          echo pieces: $pieces

          i=0
          while [ $i -lt $pieces ];
          do
          ? ?? ???echo split: $file.$i:
          ? ?? ???dd if=$file of=$file.$i bs=$size count=1 skip=$i
          ? ?? ???let i=$i+1
          done

          echo "#!/bin/bash" >; merge

          echo "i=0" >;>; merge
          echo "while [ $i -lt $pieces ];" >;>; merge
          echo "do" >;>; merge
          echo " echo merge: $file.$i" >;>; merge
          echo " if [ ! -f $file.$i ]; then" >;>; merge
          echo " echo merge: $file.$i missed" >;>; merge
          echo " rm -f $file.merged" >;>; merge
          echo " exit" >;>; merge
          echo " fi" >;>; merge
          echo " dd if=$file.$i of=$file.merged bs=$size count=1 seek=$i" >;>; merge
          echo " let i=$i+1" >;>; merge
          echo "done" >;>; merge
          chmod u+x merge'

          如何查找日期為某一天的文件

          #!/bin/sh
          # The right of usage, distribution and modification is here by granted by the author.
          # The author deny any responsibilities and liabilities related to the code.
          #
          OK=0
          A=`find $1 -print`
          if expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fi
          if expr $3 == 2 >;/dev/null ; then M=Feb ; OK=1 ; fi
          if expr $3 == 3 >;/dev/null ; then M=Mar ; OK=1 ; fi
          if expr $3 == 4 >;/dev/null ; then M=Apr ; OK=1 ; fi
          if expr $3 == 5 >;/dev/null ; then M=May ; OK=1 ; fi
          if expr $3 == 6 >;/dev/null ; then M=Jun ; OK=1 ; fi
          if expr $3 == 7 >;/dev/null ; then M=Jul ; OK=1 ; fi
          if expr $3 == 8 >;/dev/null ; then M=Aug ; OK=1 ; fi
          if expr $3 == 9 >;/dev/null ; then M=Sep ; OK=1 ; fi
          if expr $3 == 10 >;/dev/null ; then M=Oct ; OK=1 ; fi
          if expr $3 == 11 >;/dev/null ; then M=Nov ; OK=1 ; fi
          if expr $3 == 12 >;/dev/null ; then M=Dec ; OK=1 ; fi
          if expr $3 == 1 >;/dev/null ; then M=Jan ; OK=1 ; fi
          if expr $OK == 1 >; /dev/null ; then
          ls -l --full-time $A 2>;/dev/null | grep "$M $4" | grep $2 ;
          else
          ??echo Usage: $0 path Year Month Day;
          ??echo Example: $0 ~ 1998 6 30;
          fi

          如何計算當(dāng)前目錄下的文件數(shù)和目錄數(shù)

          # ls -l * |grep "^-"|wc -l ---- to count files
          # ls -l * |grep "^d"|wc -l ----- to count dir

          如何只列子目錄?

          ls -F | grep /$ 或者 alias sub = "ls -F | grep /$"(linux)

          ls -l | grep "^d" 或者 ls -lL | grep "^d" (Solaris)

          如何實(shí)現(xiàn)取出文件中特定的行內(nèi)容

          如果你只想看文件的前5行,可以使用head命令,
          如: head -5 /etc/passwd

          如果你想查看文件的后10行,可以使用tail命令,
          如: tail -10 /etc/passwd

          你知道怎么查看文件中間一段嗎?你可以使用sed命令
          如: sed -n '5,10p' /etc/passwd 這樣你就可以只查看文件的第5行到第10行。

          如何查找含特定字符串的文件

          例如查找當(dāng)前目錄下含有"the string you want find..."字符串的文件:
            $find . -type f -exec grep “the string you want find...” {} ; -print

          如何列出目錄樹

          下面的短小的shell程序可以列出目錄樹, 充分利用了sed強(qiáng)大的模式匹配能力.
            目錄樹形式如下:
            .
            `----shellp
            `----updates
            `----wu-ftpd-2.4
            | `----doc
            | | `----examples
            | `----src
            | | `----config
            | | `----makefiles
            | `----support
            | | `----makefiles
            | | `----man
            | `----util
            腳本如下:
            #!/bin/sh
            # dtree: Usage: dtree [any directory]
            dir=${1:-.}
            (cd $dir; pwd)
            find $dir -type d -print | sort -f | sed -e "s,^$1,," -e "/^$/d" -e "s,[^/]*/([^/]*)$,`----1," -e "s,[^/]*/,| ,g"

          如何實(shí)現(xiàn)取出文件中特定的列內(nèi)容

          我們經(jīng)常會遇到需要取出分字段的文件的某些特定字段,例如/etc/password就是通過“:”分隔各個字段的??梢酝ㄟ^cut命令來實(shí)現(xiàn)。例如,我們希望將系統(tǒng)賬號名保存到特定的文件,就可以:
            cut -d: -f 1 /etc/passwd >; /tmp/users
            -d用來定義分隔符,默認(rèn)為tab鍵,-f表示需要取得哪個字段。
            當(dāng)然也可以通過cut取得文件中每行中特定的幾個字符,例如:
            cut -c3-5 /etc/passwd
            就是輸出/etc/passwd文件中每行的第三到第五個字符。
            -c 和 -f 參數(shù)可以跟以下子參數(shù):
            N 第N個字符或字段
            N- 從第一個字符或字段到文件結(jié)束
            N-M 從第N個到第M個字符或字段
            -M 從第一個到第N個字符或字段

          在vim中實(shí)現(xiàn)批量加密

          密碼中還是不能帶空格,不管了,能加密就好,先這么用著。

          ============================================================
          #!/bin/bash
          # Encrypt file with vim

          if (test $# -lt 2) then
          ? ? echo Usage: decrypt password filename
          else
          ? ? vim -e -s -c ":set key=$1" -c ':wq' $2
          ? ? echo "$2 encrypted."
          fi
          ============================================================
          [weeder@SMTH weeder]$ for file in *.txt ; do encrypt test $file ; done
          test2.txt encrypted.
          test4.txt encrypted.
          test9.txt encrypted.
          kick.txt encrypted.
          ? ? echo "$2 encrypted."
          fi
          [weeder@SMTH weeder]$ for file in *.txt ; do encrypt test $file ; done
          test2.txt encrypted.
          test4.txt encrypted.
          test9.txt encrypted.
          kick.txt encrypted.
          too_old.txt encrypted.
          too_old_again.txt encrypted.
          bg5.txt encrypted.
          [weeder@SMTH weeder]$

          $@等特定shell變量的含義

          在shell腳本的實(shí)際編寫中,有一些特殊的變量十分有用:
          $# 傳遞到腳本的參數(shù)個數(shù)

          $* 以一個單字符串顯示所有向腳本傳遞的參數(shù)。與位置變量不同,此選項參數(shù)可超過9個

          $$ 腳本運(yùn)行的當(dāng)前進(jìn)程ID號

          $! 后臺運(yùn)行的最后一個進(jìn)程的進(jìn)程ID號

          $@ 與$#相同,但是使用時加引號,并在引號中返回每個參數(shù)

          $- 顯示shell使用的當(dāng)前選項,與set命令功能相同

          $? 顯示最后命令的退出狀態(tài)。0表示沒有錯誤,其他任何值表明有錯誤。

          如何使程序的執(zhí)行結(jié)果同時定向到屏幕和文件

          program_name |tee logfile
          這樣程序執(zhí)行期間的顯示都記錄到logfile同時顯示到標(biāo)準(zhǔn)輸出(屏幕)。

          如何用sendmail給系統(tǒng)所有用戶送信

          首先在aliases文件里面建立一個alias:
          alluser: :include:/etc/mail/allusers
          并執(zhí)行newaliases使之生效,然后在/etc/mail/allusers里面列出所有用戶,可以使用下面的命令:
          awk -F: '$3 >; 100 { print $1 }' /etc/passwd >; /etc/mail/allusers

          如何查找某條命令的相關(guān)庫文件

          在制作自己的發(fā)行版時經(jīng)常需要判斷某條命令需要哪些庫文件的支持,以確保指定的命令在獨(dú)立的系統(tǒng)內(nèi)可以可靠的運(yùn)行。
          在Linux環(huán)境下通過ldd命令即可實(shí)現(xiàn),在控制臺執(zhí)行:
          ldd /bin/ls
          即可得到/bin/ls命令的相關(guān)庫文件列表。

          如何使用host命令獲得更多信息

          Host能夠用來查詢域名,然而它可以得到更多的信息。host -t mx linux.com可以查詢出Linux.com的MX記錄,以及處理Mail的Host的名字。Host -l linux.com會返回所有注冊在linux.com下的域名。host -a linux.com則會顯示這個主機(jī)的所有域名信息。

          如何停止終端多個進(jìn)程

          以下是腳本:
            echo "系統(tǒng)當(dāng)前用戶"
            echo "---------------"
            who | awk '{print $2}'
            echo "---------------"
            echo "輸入要?dú)⑺澜K端的終端號:"
            read $TTY
            kill -9 ${K}=`ps -t $TTY | grep [0-9] | awk '{print $1}'`


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蕲春县| 富宁县| 克山县| 吉安县| 双鸭山市| 沙河市| 名山县| 同心县| 崇明县| 南部县| 兴安县| 定兴县| 长兴县| 南安市| 汨罗市| 威远县| 百色市| 庆云县| 盐边县| 犍为县| 石狮市| 黎城县| 城固县| 津市市| 大田县| 台中县| 龙陵县| 平湖市| 涞源县| 平原县| 顺义区| 郸城县| 哈尔滨市| 县级市| 成安县| 南丰县| 陕西省| 沂南县| 犍为县| 泽普县| 两当县|