Script Examples


Some Simple Shell Script Examples
Cut a block of lines
BatchFtp
Compute Average, Maximum and Minimum
Spelling Related Tools
Scripts for HTML
FAQ
FAQ2

Some Simple Shell Script Examples

☉ WARNING:

─ Exception handling are all omitted in the following examples.

─ Use with caution.

☉ Grep pattern with line number

─ Usage

     gn [options] pattern files 

─ Script

     grep -n $* 

☉ Check Mail: See the tail of the latest mail

tail  /usr/spool/mail/uname
tail $1  /usr/spool/mail/uname

☉ Double space a text file

  sed  G  filename

☉ Count the number of lines

   wc -l $1 | cut -c1-7

☉ Generate a sequence of numbers

   count=1
   while [ $count -le $1 ]
   do
       echo $count
       count=`expr $count + 1`
   done
 head -$1 longfile|cat -n|cut -c1-5
 head -$1 longfile|awk "{print NR}"
 head -$1 longfile|perl -p 'print $.'

☉ search a file recursively

   find $HOME -name $1 -print
   find  .    -name $1 -print

☉ convert a file into 2 coulumn

   pr -t -l66 -w80 -2 $1

☉ Eliminate double space effect

─ when upload a file from PC

   ex $1 <<%        ___________
   1,\$s/^M\$//     |1,$s/^M// |
   \$s/^^Z\$//      |$/^Z//    |
   w                |w         |
   %                -----------

☉ change file names for many files

  ls | sed 's/..*/mv & &/' > script
  vi  script
  sh script
國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  simple.htm, 
Cut a block of lines

☉ cut a block of lines

─ Usage

     gl beginline endline file

─ Script

   sed -n -e  "$1,$2p"  $3
        QuitPoint=`expr $2 + 1`
        sed -n -e "$1,$2p
        ${QuitPoint}q" $3

☉ delete a block of lines

─ (dl startline endline filenmae)

   cp $3 tmp.o
   echo "$1,$2d
   w" > script
   ex $3 < script

☉ replace a block of lines

─ (rl startline endline file1 file2)

   mv $3 tmp.o
   a=`expr $1 - 1`
   b=`expr $2 + 1`
   head -$a tmp.o > $3
   cat $4 >> $3
   sed -n -e "$b,\$p" tmp.o >> $3
  
   !!!!! What if $1 is 1 or $2 too long?
國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  block.htm, 
BatchFtp

☉ batch ftp a file

   ftp $1 <<%
   lien
   binary
   put $2   (OR get $2)
   quit
   %

☉ batch ftp many files

   echo "ftp $1 <<%
   lien
   binary" > script
   for i 
   do
     echo "put $i"
   done >> script
   echo "quit
   %" >> script 
   sh script
國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  batchftp.htm, 
Compute Average, Maximum and Minimum

☉ Compute average from a colume of a file

─ Usage

        avg column file

─ Script

  awk "
       {sum += \$$1;}
       END {print sum/NR}  "  $2

☉ get the maximum of any column

─ Usage

    max colume filename

─ Script

  awk "
       BEGIN { MAX = -999999}
       { 
         if ( \$$1 > MAX)
            MAX = \$$1 
       }
       END {print MAX}       "  $2
國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  avgmax.htm, 
Spelling Related Tools

☉ Dictionary look up

─ Usage

    look starting_word_segment

─ Script

   egrep "^$1"  /usr/dict/all

☉ Grep with multiple patterns

─ Usage

           mgrep pat1 pat2 pat3 ...

─ Script

        case $# in 
           0) cat ;;
           1) cat | grep $1 ;;
           2) cat | grep $1 | grep $2;;
           3) cat | grep $1 | grep $2 | grep $3;;
                 
  • esac
  • ☉ Find an English word

    ─ Usage

      e.g. looking for the word "convenient"
         sphelp con v en t 
    

    ─ Script

            pat=$1;   
            shift
            grep "^$pat"  /usr/lib/dict/all  | mgrep $*
    
            pat=$1;   
            shift
            look "$pat"  | mgrep $*
    

    ☉ spelling corrector

    spell $1 > 1.o
    comm -23 1.o excluedwords > 2.o
    sed 's/..*/1,$s\/&\/&\/g' 2.o > script
    vi script
    ex $1 < script
    

    ─ Pitfall

      May hit unwanted words
    
    1,$s/th/the/g
    w
    
      Ex is atomic - Do all or do nothing
        following script will fail
    
    1,$s/systen/system/g
    1,$s/systens/systems/g
    w
    

    ☉ Other way to do the 3rd line

    (while read w
    do
       echo "1,\$s/$w/$w/g"
    done )                 < 2.o > script
    
    國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  spell.htm, 
    Scripts for HTML

    ☉ Generate a Color Table

    echo ""
    for R in 00 33 66 99 cc ff 
    do
       echo "<TABLE>"
       for G in 00 33 66 99 cc ff 
       do
          echo "<TR>"
          for B in 00 33 66 99 cc ff 
          do
          echo "<TH bgcolor=#$R$G$B>
          <font size=3 color=BLACK>$R $G $B<br>"
          <font size=4 color=#$R$G$B>$R $G $B</TH>"
          done
          echo "</TR>"
       done
       echo "</TABLE>"
    done
    
    國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  html.htm, 
    FAQ

    ☉ 分割檔案 split

    ─ 例如將一個 2mb 的檔分成 2 個 1mb 的檔

    split -b 1m filename
    

    ☉ 要如何改一堆檔名?????

    ─ 將*.foo改成*.bar

     for i in *.foo
     do
        mv $i `basename $i .foo`.bar
     done
    

    ─ 將foo.*改成bar.*的方法

     for i in foo.*
     do
        tailname=`echo $i |  sed -e 's/^foo\.//'`
        mv $i bar.$tailname
     done
    

    ☉ 檔案截取命令

         awk '/start-here/,/end-here/' data.rec
    

    ─ 要取出start-here到end-here之間的段落. 不包括 start-here以及end-here這兩行

      用tail和head

    ─ 用awk

        awk '{if ( $0=="start-here") \
        n=1 \
        else { if($0=="end-here") \
        n=0 \
        if (n==1) \
        print}}' data.rec
    

    ☉ delete file when its size = 0 byte

    find / -size 0 \(-name \*.dat -o -name sh\*.dat \) -exec rm -f {} \;
    

    ☉ Sort and Rename Files

    #!/bin/sh
    for i in 000??.txt.all
    do
      sort -r $i > `echo $i | sed 's/\.all$/.sort/'`
    done
    

    ☉ 如何讓程式,在logout以後還能繼續執行

    nohup  command [ arguments ] &
    

    ☉ How to make a "script" to judge which the "shell" we are in?

       echo $SHELL
    

    ☉ 將一個執行檔放到unix上時 要打什麼才能使用那個執行檔呢??

      放上去後.. 打執行檔的名稱... 就會出現...command not found...
      將一個執行檔放上Unix以後, 第一應該先檢查premission 可以先 chmod 755 <your file> 然後再用 ./ 執行,
      其實有些 OS 預設就把 . 加入path裡, 所以不必加 ./
      加個./其實是好習慣.. 當你到了別台設定不一樣的機器上 不會頓時不知所措

    ☉ Delete all lines that contain some string

      我有一堆ASCII文字檔案,有沒有哪一個指令可以 搜尋到檔案中某一行字串,並且將該行刪除。 若是用VI,因為我的檔案多且檔案大,相當費時
            grep -v "string" filename > new_filename
    

    ☉ How to use sed to add a character "." at the beginning of all lines in a file ?

    sed -e 's/^/\./'
    

    ☉ 列印檔案(程式檔)時 如何讓printer 每一頁底下飲出頁碼呢??

      Use "pr" to convert your ascii file first, then print it.
      use "man pr" to know the details.

    ☉ 用perl 處理中文字, 要去掉那些特殊字元才不會有亂碼?

    ☉ Delete files with odd names

    ─ Problem

      我做網站上傳檔案'後來改名稱'結果檔名變成前一個為空白就是 _INDEX.HTM-前面那條線是空白'無法刪除'

    ─ Solution

      先移到跟那個檔案同一個目錄下 然後試試rm ./檔名吧 如果不可以的話....才試試rm -ir .(這樣比較危險一點) rm -r .的意思是砍掉目前目錄以及其下的子目錄及檔案 加上i的話,系統會在砍掉目錄或檔案前先詢問y or n 回答y才會砍,回答n就不砍,你看到要砍的那個檔案再按y 其他都按n(小心不要按錯了,砍了救不回來的)

    ☉ ftp 抓子目錄..

    ─ unix 有沒有甚麼ftp軟體 可以抓子目錄阿...
      ncftp:
        ncftp> get -R dir_name
      wget. GNU software.

    國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  faq.htm, 
    FAQ2

    ☉ Rename all the files in the currect directory to the lower case

    for file in *
    do
            lcfile=`echo $file | tr "[A-Z]" "[a-z]"`
            mv $file $lcfile
    done
    

    ☉ A ex script

    ─ I have a file with data as such :

    9600012301FDTF02FTDT03FFTF04TFDD05TDFF06TTTT*
    
      seperate this data into the format :
    96000123
    0p1FDTF
    0p2FTDT
    0p3FFTF
    0p4TFDD
    0p5TDFF
    0p6TTTT
    
    sed 's/\([0-9][0-9][A-Z][A-Z][A-Z][A-Z]\)/\
    \1/g'
    
        It reads "insert newline before every occurrence of two numbers followed by four uppercase letters."

    ☉ Remove a blank line in a file

    egrep -v '(^$)' test.txt
    
        If spaces/tabs on a line count as a blank line, then you need a more complicated expression.

    ☉ Switch first two columns

    ─ sed/vi

    :%s/^\([^ ]*\) \([^ ]*\)/\2 \1/
    

    ─ awk

     awk '{print $2 $1}'
    

    ☉ An AWK problem

      I am looking through a bunch of files in a directory. The files I am trying to isolate contain the "046*" in the 3rd record and the string "TOWER GROUP" in the 6th record. There are many files to go through and I have tried working with 'grep -l' but that only works in an ideal situation. The command 'awk' is useful but how do I display the contents of the entire file if the conditions are met.
    
    BEGIN { FS = "," }
    $3 ~ /046\*/ && $6 == "TOWER GROUP" {
      while( (getline < FILENAME) > 0)
        print
      close(FILENAME)
    }
    

    ☉ Question : Comparing Number

      I'm trying to compare two numbers. If the day of the month is less than 10 then echo "larger". Otherwise, echo "smaller".
    #!/bin/sh
    num=date" +%d"
    echo "\n $num"
    if expr "$num" \> 10
    then
    echo "larger"
    else
    echo "smaller"
    fi
    

    ☉ Filtering words

    sed "s/this//;s/is//;s/a//;s/ *//g"
      I'm trying to write a script to filter out (remove) certain words

    ─ eg.

    I want to filter out: is,this,a
    Input: this is a dumb question
    Desired Output: dumb question 
    
      if you want to filter 'is' but not "isn't", that wouldn't work; you could still use sed, but I think I'd use perl so that I don't have to code 3 sed substitutions for every word:
    perl -p -e 's/(^|\s+)is(\s+|$)//g;s/(^|\s+)this(\s+|$)//g;'
    
    perl -p -e 's/(^|\s+)(is|this)(\s+|$)//g;'
    

    ☉ Finding files less than a day old

      Use GNU find with -mmin option
       find / -amin +120 ...
    
    find . -newer thefile -print
    
    find . -mmin +120 -type f -exec rm -r {} \;
    
    國立政治大學資訊科學系 連耀南 lien@cherry.cs.nccu.edu.tw  faq2.htm,