  BASH α׷ Թ Ͽ
  by Mike G mikkey@dynamo.com.ar
  v0.07, 22 June 2000
 
       ũƮ α׷ ϴ  
  ֱ   Ѵ. ̰   ˰   ɼ 
     ƴϴ.  پų Ȥ   α׷Ӱ
  ƴϰ,       ÿ ٸ 鿡 
  ̹Ƿ  ִ  ̴.   Ȥ ÷ ̶  
  شٸ ׵ ߿ Ϻδ   ߰  Ե ̴.  
  ______________________________________________________________________



  1. ϸ鼭

     1.1   ֽ  ԼϷ
     1.2 ʿ 
     1.3   Ȱ

  2.  ũƮ

     2.1  hello world ũƮ
     2.2    ũƮ

  3. 𷺼  

     3.1 ǿ  
     3.2  : ǥ  Ϸ
     3.3  : ǥ  Ϸ
     3.4  : ǥ  ǥ 
     3.5  : ǥ  ǥ 
     3.6  : ǥ  ǥ  Ϸ

  4. 

     4.1  Ұ װ ϴ 
     4.2  : sed   
     4.3  : ls -l *.txt   

  5. 

     5.1 :   Hello World!
     5.2 :       ũƮ
     5.3  

  6. ǹ

     6.1  ؼ....  
     6.2  : if .. then  ϴ ⺻ ǥ
     6.3  : if .. then ... else  ϴ ⺻ ǥ
     6.4  :  ϴ ǹ

  7. for, while ׸ until  ϴ 

     7.1 For  ϴ 
     7.2 C   for
     7.3 While  ϴ 
     7.4 Until  ϴ 

  8. Լ
     8.1 Լ  
     8.2 Ű  Լ  

  9.  ̽

     9.1 ܼ ޴   select  ̿ϱ
     9.2  ̿ϱ

  10.   ˱

     10.1 read   Է ޱ
     10.2  
     10.3 bash ã
     10.4 α׷ ǵ  ޱ
     10.5   ĸóϱ
     10.6  ҽ 

  11. ǥ
     11.1 ڿ  
     11.2 ڿ  
     11.3  
     11.4   
     11.5  ɵ

  12. ٸ ũƮ

     12.1 丮   Ͽ ϱ
     12.2    ũƮ(   )
     12.3  ̸ ٲ ִ ũƮ
     12.4  ̸ ٲ ִ ũƮ( ) 

  13.   

     13.1 BASH ȣϴ 

  14.   Ͽ 

     14.1   
     14.2 Thanks to
     14.3 
     14.4   


  ______________________________________________________________________

  1.  ϸ鼭
  
  1.1.    ֽ  ԼϷ  ϶.()
  http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html


  1.2.  ʿ 

  GNU/Linux  ü迡 ͼϰ  α׷  ˰ 
    ȴ. ̰  α׷ ° ƴϱ 
  ּ ⺻  ־ ϴ ̴.


  1.3.    Ȱ

      Ȳ ϰ Ǿ  ִ.

  ?  α׷ֿ  ̵ ö װ  ũƮ ۼ
  ⸦  

  ?   α׷ֿ   ĸ  ְ, ׿  
   ʿϴٰ  .

  ?   α׷ ϱ⿡ ռ    ũƮ
  ⸦  .

  ?    ġ  ⸦ ϰų Ȥ ̹ 
   .

  ?  Ϻ ٺ,  Ͽ  о   Ǯ .-_- 


  2.   ũƮ

   HOW-TO   shell α׷ֿ ־ Ʈ ֱ 
   ϰ ִ.

   ǿ    ũ ϴ  ־  
  ũƮ    ̴.


  2.1.   hello world ũƮ

                 #!/bin/bash
                 echo Hello World

   ũƮz   ٷ ̷ ִ. ù ° ٿ   ϴ
   ʿ α׷ ý  ִ θ ش.  ° 
     κ "Hello World" ͹̳λ ϴ ̴.

  2.2.    ũƮ

               #!/bin/bash
               tar -cZf /var/my-backup.tgz /home/me/

   ũƮ ͹̳ο ޽ ϴ  ϰ, 츮 츮 Ȩ
  丮 tar   ϴ ̴. ̰ Ҹ   ȴ.
  Ҹ  ũƮ     κп   ̴.

  3. 𷺼  

  3.1.  ǿ  

  3  ڰ ִ. ǥ Է, ǥ , ǥ  װ̴. 

  ⺻    ִ   .:

  1. ǥ  Ϸ 

  2. ǥ  Ϸ 

  3. ǥ  ǥ  

  4. ǥ  ǥ  

  5. ǥ  ǥ  Ϸ 

  6. ǥ  ǥ  ǥ  

  7. ǥ  ǥ  ǥ  

     1 'represents' stdout and 2 stderr.

  ̰͵     Ҹ ڸ :   ɾ Ͽ
   ǥ (ۿ  ִ) ǥ  ȭ󿡼   ,
   `` '' ϴ  ۿ  ̴.

  3.2.   : ǥ Ϸ 

  ̰  α׷   Ϸ ϴ ̴.

               ls -l > ls-l.txt

  ,  'ls-l.txt' Ҹ   'ls -l'  Ͽ  
  ȭ鿡   ִ  ϴ ·  ̴.

  3.3.   : ǥؿ Ϸ 

  ̰  α׷ ǥ   Ͽ  ̴.

               grep da * 2> grep-errors.txt

  ,  'grep-errors.txt'     ̰ ̰ 
   'gerp da *'    ȭ鿡 µǾ ǥ 
  ϰ  ̴.

  3.4.   : ǥ  ǥ 

  ̰  α׷ ǥ  ,  Ͽ ϴ  
  ǥ  ̴ ̴. 

               grep da * 1>&2

  ,  ǥ  ° ǥ  .  ̰ ٸ
  ε    ̴.

  3.5.   : ǥ  ǥ .  

  ̰ α׷ ǥ    Ͽ ϴ 
  ǥ  ̴ ̴.

               grep * 2>&1

  ,  ǥ  ° ǥ ¿ .    
  ̱   Ѵٸ  δ Ҹ (ǥ 
  μ  ͵) ǥ μ  տ ̴    ̴.

  3.6.  : ǥ ° ǥ  Ϸ.
   
  ̰ α׷   Ϸ  ̴. ̰  
   ǰ Ѿ⸦  cron ⿭  ︮⵵ Ѵ. 

               rm -f $(find / -name core) &> /dev/null

  ̿ (cron ⿭  츦 .)  core  Ҹ
     丮 ȿ  ִ   ̴. 
    °  ;ϴ   Ǵ  ϰ 
  ʿ䰡 ִ.
  
  4. 

      ϴĿ       
  ͵ Ұϰ,    ϰ Ǵ 캸 ȴ.

  4.1.  Ұ װ ϴ 

     α׷  ٸ  Է ȯϷ  
   ϰ  ۾ ϰ Ѵ.

  4.2  : sed   

  ̰  ϴ   ̴.

               ls -l | sed -e "s/[aeio]/u/g"

  , ̷ Ͽ ռ   ls -l Ͽ   ȭ鿡
  ϴ ,   sed α׷  sed  
  ϰ ȴ.

  4.3  : ls -l *.txt   

  Ϲ, ls -l *.txt   ߿  ٷο 
  ̰   μ ȭ  Ǯ ؼ ׷
   ƴϴ.

               ls -l | grep "\.txt$"

  ,  ls -l  grep Ѿ "\.txt$" ġǴ ٸ 
  ȭ鿡 ϰ  ̴. 

  5.   

    α׷     ̴. ⼭
    Ÿ   ʿ. bash  , , ڿ
  θ ϱ ̴. 

     ʿ䵵 . ׳  ҿ ϴ 
  ϴ ̴.

  5.1.   :   Hello World!

                   #!/bin/bash
                   STR="Hello World!"
                   echo $STR

  ι°  "Hello World!" ڿ üϴ  STR Ѵ.
  ׸    $̶ ڰ  տ   ȴ. 
      $ ǥø  ʴ´ٸ µǴ α׷
    ϴ Ͱ 繵 ٸ ̶ ̴.

  5.2.   :       ũƮ

                  #!/bin/bash
                  OF=/var/my-backup-$(date +%Y%m%d).tgz
                  tar -cZf $OF /home/me/

   ũƮ ٸ  Ұϰ ִ. ,  ι° ٿ 
    ͼ   ȴ.  ǥ  
    ũƮ ϸ  ȿ ִ  Ͽ  
  ϴ  ȴٴ ̴. 

   ũƮ µǴ  ̸ ϸ ޶. ̰ 
  data ɿ  Ŀ   ̸ ٲ  ̴. 
  ̰  ϴ ٸ  ٲ  ִ.

   ణ   :

  echo ls

  echo $(ls)

  5.3.   

       .


                       #!/bin/bash
                       HELLO=Hello
                       function hello {
                               local HELLO=World
                               echo $HELLO
                       }
                       echo $HELLO
                       hello
                       echo $HELLO

      ϴ  ־ ϴٰ ȴ.

  6.  ǹ

  ǹ    ϴ° Ȥ  ʴ°  ǥϴ
  ̴. 

  6.1.  ؼ....

  ǹ   ǥ  ִ.  ⺻  if () 
  then ( )  Ǵ      
    ȴ.  

  ǹ   ִ ̷ ĵ ִ. if () then (  1)
  else (  2)   Ŀ  ̸   1
  ǰ, ׷    2 Ǵ ̴. 

  ǹ  ٸ ,  if ( 1) then (  1)
  else if ( 2) then (  2) else (  3) ִ.
  ̿  Ŀ else if ( 2) them (  2)  ߰Ǿ
  ִ´, ̰   1  ʰ  2 ϸ   2
   ǰ ׷    3 ȴٴ ̴. ٸ ǥ
  ĵ鿡 ؼ ݱ  ͵  Ͽ    . 

   Ͽ:

  if bash ϴ    ̴.

  if [expression];
  then
  code if 'expression' is true.
  fi

  6.2.   : if .. then  ϴ ⺻ ǥ

                   #!/bin/bash
                   if [ "foo" = "foo" ]; then
                      echo expression evaluated as true
                   fi

   ڵ if   翬 ϹǷ then ǹ ġ 
   ڵ fi  ȴ.

  6.3.   : if .. then ... else  ϴ ⺻ ǥ

                   #!/bin/bash
                   if [ "foo" = "foo" ]; then
                      echo expression evaluated as true
                   else
                      echo expression evaluated as false
                   fi

  6.4.   :  ϴ 

                   #!/bin/bash
                   T1="foo"
                   T2="bar"
                   if [ $T1 = $T2 ]; then
                       echo expression evaluated as true
                   else
                       echo expression evaluated as false
                   fi


  7.  for, while ׸ until  ϴ 

   ǿ  for, while ׸ until  ϴ  ˰Եȴ.

  for ϴ  ٸ α׷ ʹ ణ ٸ. ⺻
  ̰  Ϸ 󿡼 Ǯϰ Ѵ. 

   ϴ ǥڰ true Ÿ  ȯ ϰ, ׷
   (ǥڰ false ) ߰ Ǵ ̶  ǥμ
  while ڵ  κп   ִ. break Ǵ ڵ ȿ ִ
  ͵ . 

    κ while   ڵ忡 ǥڸ false  ִ
  κ ῩǾ  ÿ Ͼ. 

   "while until   Ƴ?" ϰ Ѵٸ  
  ̴.

  7.1.  For  ϴ 

               #!/bin/bash
               for i in $( ls ); do
                   echo item: $i
               done

   ° ٿ 츮 i $( ls )   ϴ μ ߴ.

   °  ʿϴٸ  ų Ȥ      ִ.

    ȯ  $i ο  . 

   ũƮ  ܼ   Ͽ   ִ  
    ִ.

  7.2.  C   for

  Ű  ٸ  ִ. ̰ for Ͽ  
  C perl  ϴ for  ̴.

               #!/bin/bash
               for i in `seq 1 10`;
               do
                       echo $i
               done

  7.3.  While  ϴ 

                #!/bin/bash
                COUNTER=0
                while [  $COUNTER -lt 10 ]; do
                    echo The counter is $COUNTER
                    let COUNTER=COUNTER+1
                done

   ũƮ (C, Pascal, perl, python, Ÿ  Ͽ)  ˷
  for  䳻 ִ.

  7.4.  Until  ϴ 

                #!/bin/bash
                COUNTER=20
                until [  $COUNTER -lt 10 ]; do
                    echo COUNTER $COUNTER
                    let COUNTER-=1
                done

  8.  Լ

    α׷ ,  ڵ  Լ 
   Ȥ  п    ִ.

  Լ  function Լ̸ { ڵ }   ƶ.

  Լ Ҹ   ̸ ̴  ٸ α׷ .

  8.1.  Լ 

             #!/bin/bash
             function quit {
                 exit
             }
             function hello {
                 echo Hello!
             }
             hello
             quit
             echo foo

  2° ٿ 4°  quit  Լ, 5° ٿ 7°  
  hello Լ Ѵ.  ̿  ũƮ  
  Ǵ   ٸ   ־ Ѵ.

  Լ  Ư    ʿ䰡 ٴ Ϳ .

   ũƮ  hello ȣϰ,   quit Լ ȣѴ. 
  ׸ quit Լ ũƮ    α׷ 10° 
   Ѵ.

  8.2.  Ű ϴ Լ 

                       #!/bin/bash
                       function quit {
                          exit
                       }
                       function e {
                           echo $1
                       }
                       e Hello
                       e World
                       quit
                       echo foo

   ũƮ   Ͱ   ̴.  ޶  e
  Լ̴.  Լ ù° μ ޾ Ѵ. Լ μ
  ũƮ  μ    ޵ȴ.

  9.   ̽

  9.1. ܼ ޴   select  ̿ϱ

             #!/bin/bash
             OPTIONS="Hello Quit"
             select opt in $OPTIONS; do
                 if [ $opt = "Quit" ]; then
                  echo done
                  exit
                 elif [ $opt = "Hello" ]; then
                  echo Hello World
                 else
                  clear
                  echo bad option
                 fi
             done
    ũƮ ϸ  α׷ӵ ٶ ܼ
  ؽƮ  ޴   ̴.  ̰  ܾ 
  $OPTION ġ  ȴٴ  'for'  ϴٴ 
  ָ ʿ䰡 ִ. 

  9.2.  ̿ϱ 

                 #!/bin/bash
                 if [ -z $1 ]; then
                     echo usage: $0 directory
                     exit
                 fi
                 SRCD=$1
                 TGTD="/var/backups/"
                 OF=home-$(date +%Y%m%d).tgz
                 tar -cZf $TGTD$OF $SRCD

   ũƮ  ſ  ϰ ٰ ̴. ù° 
  ׽Ʈ ǥ α׷ ($1)̶ μ ް ϰų Ȥ
  ׷ ʰ ڿ ª  ޽  ̴.  ũƮ 
   ̷  ܼ ̴. 

  10.   ˱

  10.1. read   Է ޱ

     Ʈ 󿡼  Է ޾ ׿  
  ϵ α׷ ۼϱ⸦  ̴.    ߿ ̰ 
   Ϲ ̴.

                  #!/bin/bash
                  echo Please, enter your name
                  read NAME
                  echo "Hi $NAME!"

  ,  پ  о    ̴.   װ
    ش. 

                       #!/bin/bash
                       echo Please, enter your firstname and lastname
                       read FN LN
                       echo "Hi! $LN, $FN !"

  10.2.  

   󿡼  Է .:

  echo 1 + 1

   '2' ⸦ Ѵٸ, и  ̴.  ڸ
  ԷϿ Ͽ  μ ϴ   ؼ BASH  
   ؾ Ѵ.:

  echo $((1+1))

  ̰     ̷. ̰   ǥ̴.
   ̿  Ÿ  ִ. :

  echo $[1+1]

   ̳ Ȥ    ϴ   ʿ
  Ѵٸ,  bc ϴ  .

    "echo $[3/4]" ࿡ Ͽٸ, ̰ 0 
  ´. װ bash   ϱ ̴.  
  "echo 3/4|bc -l"  Ѵٸ  ϴ  0.75   ִ.

  10.3. bash ã

  (ũ  ̴.  ϰ ְ.....)

  츮 ݱ ٷ  츮 #!/bin/bash  ÷ ߴ.

  bash  ִ ã  ؾ ұ?  ġ Ȯ .

  ls -l /bin/bash
  ls -l /sbin/bash
  ls -l /usr/local/bin/bash
  ls -l /usr/bin/bash
  ls -l /usr/sbin/bash
  ls -l /usr/local/sbin/bash

  ּ  ã   ̴. ٸ ýۿ ٸ  
  ̴. Ȥ  'wich bash' ̿  ִ.

  10.4. α׷ ǵ  ޱ

  bash, α׷ ǵ  Ư  $?  ϰ ȴ.

    α׷ ǵ  ĸóϱ  ̴.  dada 
  丮   ϰ ִ. (̰  ũ ǰ̴.)

               #!/bin/bash
               cd /dada &> /dev/null
               echo rv: $?
               cd $(pwd) &> /dev/null
               echo rv: $?



  10.5.    ĸóϱ

   ª ũƮ  ̽   ̺  ش.(
  MySQL ϰ   ̴̾߱.) , mysql  ̸ 
  н带   ִ. 

               #!/bin/bash
               DBS=`mysql -uroot  -e"show databases"`
               for b in $DBS ;
               do
                       mysql -uroot -e"show tables from $b"
               done

  10.6.  ҽ 

    ҽ   ϵ ÿ   ִ. 

  __  __

  11. ǥ

  11.1. ڿ  

  (1) s1 = s2
  (2) s1 != s2
  (3) s1 < s2
  (4) s1 > s2
  (5) -n s1
  (6) -z s1

  (1) s1   s2  .
  (2) s1   s2   ʴ.
  (3) __  __
  (4) __  __
  (5) s1   ڿ ƴϴ.(   ̻ ڸ ϰ ִ.)
  (6) s1   ڿ̴. 

  11.2. ڿ  

   ڿ . 

               #!/bin/bash
               S1='string'
               S2='String'
               if [ $S1=$S2 ];
               then
                       echo "S1('$S1') is not equal to S2('$S2')"
               fi
               if [ $S1=$S1 ];
               then
                       echo "S1('$S1') is equal to S1('$S1')"
               fi

  11.3.  

  +
  -
  *
  /
  % ()

  11.4.   

  -lt (<)
  -gt (>)
  -le (<=)
  -ge (>=)
  -eq (==)
  -ne (!=)

  C α׷ӵ ̰͵     ̴. 

  11.5.  ɵ

  sed  ( )
  gawk
  grep (ġϴ  ִ  ˻)
  wc   (ܾ Ȥ   Ȯ)
  sort
  bc   ()
  cut  ( Ѵ.)
  tput (Ϲ ͹̳ο   ´)

  ̰͵ α׷ 鼭 ϰ   ϰ  ͵̴.
  (׷ ͵ ߿ ̴ּ!)  α׷ ſ ࿡
    ġ   ̴.


  12.  ٸ ũƮ

  12.1. 丮   Ͽ ϱ 

  12.2.    ũƮ(   )

              #!/bin/bash
              SRCD="/home/"
              TGTD="/var/backups/"
              OF=home-$(date +%Y%m%d).tgz
              tar -cZf $TGTD$OF $SRCD

  12.3.  ̸ ٲ ִ ũƮ

               #!/bin/sh
               # renna: rename multiple files according to several rules
               # written by felix hudson Jan - 2000

               #first check for the various 'modes' that this program has
               #if the first ($1) condition matches then we execute that portion of the
               #program and then exit

               # check for the prefix condition
               if [ $1 = p ]; then

               #we now get rid of the mode ($1) variable and prefix ($2)
                 prefix=$2 ; shift ; shift

               # a quick check to see if any files were given
               # if none then its better not to do anything than rename some non-existent
               # files!!

                 if [$1 = ]; then
                    echo "no files given"
                    exit 0
                 fi

               # this for loop iterates through all of the files that we gave the program
               # it does one rename per file given
                 for file in $*
                   do
                   mv ${file} $prefix$file
                 done

               #we now exit the program
                 exit 0
               fi

               # check for a suffix rename
               # the rest of this part is virtually identical to the previous section
               # please see those notes
               if [ $1 = s ]; then
                 suffix=$2 ; shift ; shift

                  if [$1 = ]; then
                   echo "no files given"
                  exit 0
                  fi

                for file in $*
                 do
                  mv ${file} $file$suffix
                done

                exit 0
               fi

               # check for the replacement rename
               if [ $1 = r ]; then

                 shift

               # i included this bit as to not damage any files if the user does not specify
               # anything to be done
               # just a safety measure

                 if [ $# -lt 3 ] ; then
                   echo "usage: renna r [expression] [replacement] files... "
                   exit 0
                 fi
               # remove other information
                 OLD=$1 ; NEW=$2 ; shift ; shift

               # this for loop iterates through all of the files that we give the program
               # it does one rename per file given using the program 'sed'
               # this is a sinple command line program that parses standard input and
               # replaces a set expression with a give string
               # here we pass it the file name ( as standard input) and replace the nessesary
               # text

                 for file in $*
                 do
                   new=`echo ${file} | sed s/${OLD}/${NEW}/g`
                   mv ${file} $new
                 done
               exit 0
               fi

               # if we have reached here then nothing proper was passed to the program
               # so we tell the user how to use it
               echo "usage;"
               echo " renna p [prefix] files.."
               echo " renna s [suffix] files.."
               echo " renna r [expression] [replacement] files.."
               exit 0

               # done!

  12.4.  ̸ ٲ ִ ũƮ( )

            #!/bin/bash
            # renames.sh
            # basic file renamer

            criteria=$1
            re_match=$2
            replace=$3

            for i in $( ls *$criteria* );
            do
                src=$i
                tgt=$(echo $i | sed -e "s/$re_match/$replace/")
                mv $src $tgt
            done


  13.   

  13.1. BASH ȣϴ  

   ù° ٿ   ߰ϴ   ̴.

            #!/bin/bash -x

  ̰ ̷ο    .

  14.   Ͽ 

   , Ȥ    ̸  κп  
  Ӵ.  ׷   Ʈ ݿ ̴.
  (Ʒ   ʽϴ. : ű ΰ )

  14.1.  

  This documents comes with no warranty of any kind.  and all that

  14.2. Thanks to


  ? Nathan Hurst for sending a lot of corrections.

  ? Jon Abbott for sending comments about evaluating arithmetic
     expressions.

  ? Laurent Martelli for  translating this document to French (soon
     here the URL)
H

  ? Felix Hudson for writing the renna script

  ? Kees van den Broek (for sending many corrections)

  ? Mike (pink) made some suggestions about locating bash and testing
     files

  ? fiesh make a nice suggestion for the loops section.

  14.3. 

  Samples added on string comparison.

  v0.8 More little additions.

  v0.7 More corrections and some old TO-DO sections written.

  v0.6 Minor corrections.

  v0.5 Added the redirection section.

  v0.4 disapperd from its location due to my ex-boss and thid doc found
  it's new place at the proper url: www.linuxdoc.org.

  prior:  I don't rememeber and I didn't use rcs nor cvs :(



  14.4.    

  Introduction to bash (under BE)
  http://org.laol.net/lamug/beforever/bashtut.htm

  Bourne Shell Programming http://207.213.123.70/book/



