Shell¤¢¤ì¤³¤ì

a graph image

  1. tcsh
    • http://www.tcsh.org/Home
    • C Shell ¤ËÂФ¹¤ë³ÈÄ¥¤ò½¸¤á¤Æ°ìËܲ½¤·¤¿¤â¤Î
    • BSD·ÏUnix¤Îɸ½àShell
    • tcsh
  2. bash
    • GNU¥×¥í¥¸¥§¥¯¥È¤Ë¤è¤ësh(Bourne Shell)¼ÂÁõ
    • born again(À¸¤Þ¤ìÊѤï¤ê)
    • Linux¤Îɸ½àshell
    • MacOS X¤Ç¤â10.3¤«¤éɸ½àshell(Åö½é¤Ïtcsh¤¬É¸½àshell)
  3. zsh
    • bash¤Î³ÈÄ¥
    • CSV¤äAnt¤ÎÊä´Öµ¡Ç½¤Ê¤É...¹âµ¡Ç½¤¹¤®¤Æ»È¤¤¤­¤ì¤Ê¤¤...
  4. º£¼«Ê¬¤Ï¤É¤Îshell¤ò»È¤Ã¤Æ¤¤¤ë¤«
    > echo $0
    -bash

bash¤Î»È¤¤Êý

  1. ¥³¥Þ¥ó¥ÉÊä´Ö
    • [tab]
  2. ¥«¡¼¥½¥ë°ÜÆ°
    • [¢«][¢ª]
    • [Ctrl]+[A] ¹ÔƬ¤Ø
    • [Ctrl]+[E] ¹ÔËö¤Ø
    • [Ctrl]+[K] ¥«¡¼¥½¥ë°ÌÃÖ¤«¤é¹ÔËö¤Þ¤Çºï½ü
    • [Ctrl]+[L] ²èÌÌ¥¯¥ê¥¢ clear ¥³¥Þ¥ó¥É¤ÈƱ¤¸
    • [Ctrl]+[Y] ºï½üUndo
  3. ¥³¥Þ¥ó¥É¥Ò¥¹¥È¥ê
    • [¢¬][¢­]
    • !! : ľÁ°¤Î¥³¥Þ¥ó¥É¤ò¼Â¹Ô
  4. ¥Ç¥£¥ì¥¯¥È¥ê¥Ò¥¹¥È¥ê
    • pushd ¥Ç¥£¥ì¥¯¥È¥ê
      • ¥Ç¥£¥ì¥¯¥È¥ê°ÜÆ°¡£¥Ç¥£¥ì¥¯¥È¥ê̾¤ò¥¹¥¿¥Ã¥¯¤ËÀѤà
    • popd
      • ¥¹¥¿¥Ã¥¯¤«¤é¥Ç¥£¥ì¥¯¥È¥ê̾¤òÆɤ߽Ф¹
    • ¼Â¹ÔÎã
      / >pushd /usr/
      /usr ~
      
      /usr >pushd local
      /usr/local /usr ~
      
      /usr/local >pushd bin
      /usr/local/bin /usr/local /usr ~
      
      /usr/local/bin >popd
      /usr/local /usr ~
      
      /usr/local >popd
      /usr ~
      
      /usr >popd
      ~
      
      / >popd
      bash: popd: directory stack empty
      / >
    • cd - : ľÁ°¤Î¥Ç¥£¥ì¥¯¥È¥ê¤ËÌá¤ë
  5. JobÁàºî
                             Foreground                  Background
    
    Shell --[ > Command ] --> JOB    ---- [Ctrl+Z] ---> JOB(Sleepgin)
                           (Running) <--- [ > fg ] ----     ¡Ã
                                                         [ > bg ]
                                                            ¢­
    Shell --[ > Command & ] --------------------------> JOB(Runnging)
    • JOB = ¼Â¹ÔÃæ¤Î¥×¥í¥°¥é¥à·²(Ê£¿ô¤Î¥×¥í¥°¥é¥à¤Ç°ì¤Ä¤Î»Å»ö¤ò¤¹¤ë¤È¤­¤Ë¤½¤ì¤é¤Î¸Ç¤Þ¤ê¤òJOB¤È¸Æ¤Ö)
    • ¥·¥§¥ë¤«¤é¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤¹¤ë¤È¡¢JOB¤Ï¥Õ¥©¥¢¥°¥é¥¦¥ó¥É¤ÇÆ°¤¯
      • [Ctrl+Z]¤ò²¡¤¹¤È¡¢¼Â¹ÔÃæ¤ÎJOB¤ÏÄä»ß¤µ¤ì¡¢¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¤Ë°Ü¤ë
      • [bg]¥³¥Þ¥ó¥É¤Ç¡¢¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¤Ë¤¢¤ëÄä»ßÃæ¤ÎJOB¤ò¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¤ÇÆ°¤«¤¹¤³¤È¤¬½ÐÍè¤ë
      • [fg]¥³¥Þ¥ó¥É¤Ç¡¢¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¤Ë¤¢¤ëÄä»ßÃæ¤ÎJOB¤ò¥Õ¥©¥¢¥°¥é¥ó¥É¤ËÌ᤹»ö¤¬½ÐÍè¤ë
    • ¥·¥§¥ë¤«¤é¥³¥Þ¥ó¥É¤ò"&"¤Ä¤­¤Ç¼Â¹Ô¤¹¤ë¤È¡¢JOB¤Ï¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¤ÇÆ°¤¯

bash¤Î´Ä¶­ÊÑ¿ô

¥×¥í¥ó¥×¥È

´Ä¶­ÊÑ¿ô PS1 ¤Ë¼¡¤Îʸˡ¤Ç¥×¥í¥ó¥×¥È¤Ë½Ð¤·¤¿¤¤¤â¤Î¤òÄêµÁ¤¹¤ë

\aASCII ¤Î¥Ù¥ëʸ»ú (07)
\d"ÍËÆü ·î Æü"¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ë¤è¤ëÆüÉÕ¤± (Îã "Tue May 26")
\eASCII ¤Î¥¨¥¹¥±¡¼¥×ʸ»ú (033)
\hºÇ½é¤Î"."¤Î¤È¤³¤í¤Þ¤Ç¤Î¥Û¥¹¥È̾
\H¥Û¥¹¥È̾
\n²þ¹Ô
\rÉüµ¢
\s¥·¥§¥ë̾¡¢$0 ¤Î¥Ù¡¼¥¹Ì¾ (ºÇ¸å¤Î¥¹¥é¥Ã¥·¥å¤Î¸å¤í¤ÎÉôʬ)
\t24»þ´ÖÀ©¤Î HH:MM:SS ¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ë¤è¤ë»þ´Ö
\T12»þ´ÖÀ©¤Î HH:MM:SS ¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ë¤è¤ë»þ´Ö
\@am/pm ¤ò¤Ä¤±¤¿12»þ´ÖÀ©¤Î¥Õ¥©¡¼¥Þ¥Ã¥È¤Ë¤è¤ë»þ´Ö
\u¸½¥æ¡¼¥¶¡¼¤Î¥æ¡¼¥¶¡¼Ì¾
\vbash ¤Î¥Ð¡¼¥¸¥ç¥ó(Îã 2.00)
\Vbash ¤Î¥ê¥ê¡¼¥¹Èֹ桢¥Ð¡¼¥¸¥ç¥ó¤È¥Ñ¥Ã¥Á¥ì¥Ù¥ë (Îã 2.00.0)
\w¸½ºß¤Î¥Ç¥£¥ì¥¯¥È¥ê
\W¸½ºß¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Î¥Ù¡¼¥¹Ì¾
\!¸½ºß¤Î¥³¥Þ¥ó¥É¤Î¥Ò¥¹¥È¥ê¡¼ÈÖ¹æ
\#¸½ºß¤Î¥³¥Þ¥ó¥É¤Î¥³¥Þ¥ó¥ÉÈÖ¹æ
\$UID¤¬0¤Ê¤é#¡¢¤½¤¦¤Ç¤Ê¤±¤ì¤Ð$
\nnn8¿Ê¿ônnn¤ËÂбþ¤¹¤ëʸ»ú
\\¥Ð¥Ã¥¯¥¹¥é¥Ã¥·¥å
\[ɽ¼¨¤µ¤ì¤Ê¤¤Ê¸»úÎó¤Î³«»Ï¡£Ã¼ËöÀ©¸æ¥·¡¼¥±¥ó¥¹¤ò¥×¥í¥ó¥×¥È¤ËËä¤á¹þ¤à¡£
\]ɽ¼¨¤µ¤ì¤Ê¤¤Ê¸»úÎó¤Î½ª¤ê¡£
 

Îã:

export PS1="[\w]\$ "
 
[/opt/local/java/db-derby-10.3.3.0]$ ls
CHANGES.html			docs
KEYS				frameworks
LICENSE				frameworks.DEPRECATED.txt
NOTICE				index.html
RELEASE-NOTES.html		javadoc
bin				lib
demo				test
[/opt/local/java/db-derby-10.3.3.0]$

¥·¥§¥ë¥¹¥¯¥ê¥×¥È

´ðËܹ½Ê¸

  1. abc.sh ¤ÎÀèƬ¹Ô¤Ë°Ê²¼¤Î¤è¤¦¤Ëµ­½Ò¤·¤Æ¡¢£²¹ÔÌܰʹߤ˥³¥Þ¥ó¥É¤òÍåÎ󤹤ë
    #!/bin/bash
    Linux¤Ç¤Ï¡¢/bin/sh ¤Ï /bin/bash ¤Î¥·¥ó¥Ü¥ê¥Ã¥¯¥ê¥ó¥¯¤Ê¤Î¤Ç /bin/sh ¤È¤·¤Æ¤âÎɤ¤¤¬¡¢Solaris¤Ç¤ÏÊÌʪ¡£sh ¤È bash ¤Ç¡¢testʸ¤Î¿¶¤ëÉñ¤¤¤¬°ã¤Ã¤¿¤ê¤¹¤ë¤Î¤Ç /bin/bash ¤È»ØÄꤷ¤Æ¤ª¤¯¤Î¤¬ÌµÆñ
  2. abc.sh ¤Ë¼Â¹Ô¸¢¸Â¤òÍ¿¤¨¤ë
    > chmod +x abc.sh
  3. ¤³¤ì¤ÇÉáÄ̤Υ¢¥×¥ê¤È¤·¤Æ¼Â¹Ô½ÐÍè¤ë
    > ./abc.sh

ÊÑ¿ô

  1. ÊÑ¿ô¤òÄêµÁ¤¹¤ë¤È¤­¤Ë¤Ï¡¢$¤ÏÉÔÍ×
    COUNT=1
  2. ÊÑ¿ô¤ò»²¾È¤¹¤ë¤È¤­¤Ë¤Ï > ¤â¤·¤¯¤Ï ${ } ¤ò¤Ä¤±¤ë
    echo $COUNT
    echo ${COUNT}
  3. variable.sh
    #!/bin/bash
    
    VAL1=1023
    VAL2=1024
    
    echo "$VAL1 + 10     : $(( $VAL1 + 10 ))"
    echo "$VAL1 - 10     : $(( $VAL1 - 10 ))"
    echo "$VAL1 * 10     : $(( $VAL1 * 10 ))"
    echo "$VAL1 / 10     : $(( $VAL1 / 10 ))"
    echo "$VAL1 % 10     : $(( $VAL1 % 10 ))"
    echo "$VAL1 & 255    : $(( $VAL1 & 255 ))"
    echo "$VAL1 | 255    : $(( $VAL1 | 255 ))"
    echo "$VAL1 ^ 255    : $(( $VAL1 ^ 255 ))"
    echo "NOT $VAL1      : $(( !$VAL1 ))"
    echo "NOR $VAL1      : $(( ~$VAL1 ))"
    echo "$VAL1>$VAL2 ?  : $(( $VAL1 > $VAL2 ))"
    echo "$VAL1<$VAL2 ?  : $(( $VAL1 < $VAL2 ))"
    echo "$VAL1>=$VAL2 ? : $(( $VAL1 >= $VAL2 ))"
    echo "$VAL1<=$VAL2 ? : $(( $VAL1 <= $VAL2 ))"
    echo "$VAL1==$VAL2 ? : $(( $VAL1 == $VAL2 ))"
    echo "$VAL1!=$VAL2 ? : $(( $VAL1 != $VAL2 ))"
    echo "$VAL1<$VAL2 && $VAL1!=$VAL2 ? : $(( $VAL1 < $VAL2 && $VAL1 != $VAL2 ))"
    echo "$VAL1>$VAL2 && $VAL1!=$VAL2 ? : $(( $VAL1 > $VAL2 && $VAL1 != $VAL2 ))"
    echo "$VAL1<$VAL2 || $VAL1>$VAL2 ?  : $(( $VAL1 < $VAL2 || $VAL1 > $VAL2 ))"
    
    LITERATURE="ABCDEFGABCDEFGHIJKLMN"
    
    echo "ORIGINAL           : $LITERATURE"
    echo "REPLACE FIRST 'BCD': ${LITERATURE/BCD/XXX}"
    echo "REPLACE ALL 'BCD'  : ${LITERATURE//BCD/XXX}"
    echo "CUT FORMER (FIRST FIND PATTERN): ${LITERATURE#*D}"
    echo "CUT FORMER (LONGEST PATTERN)   : ${LITERATURE##*D}"
    echo "CUT LATTER (FIRST FIND PATTERN): ${LITERATURE%D*}"
    echo "CUT LATTER (LONGEST PATTERN)   : ${LITERATURE%%D*}"
  4. ¼Â¹Ô·ë²Ì
    > ./variable.sh
    1023 + 10     : 1033
    1023 - 10     : 1013
    1023 * 10     : 10230
    1023 / 10     : 102
    1023 % 10     : 3
    1023 & 255    : 255
    1023 | 255    : 1023
    1023 ^ 255    : 768
    NOT 1023      : 0
    NOR 1023      : -1024
    1023>1024 ?  : 0
    1023<1024 ?  : 1
    1023>=1024 ? : 0
    1023<=1024 ? : 1
    1023==1024 ? : 0
    1023!=1024 ? : 1
    1023<1024 && 1023!=1024 ? : 1
    1023>1024 && 1023!=1024 ? : 0
    1023<1024 || 1023>1024 ?  : 1
    ORIGINAL           : ABCDEFGABCDEFGHIJKLMN
    REPLACE FIRST 'BCD': AXXXEFGABCDEFGHIJKLMN
    REPLACE ALL 'BCD'  : AXXXEFGAXXXEFGHIJKLMN
    CUT FORMER (FIRST FIND PATTERN): EFGABCDEFGHIJKLMN
    CUT FORMER (LONGEST PATTERN)   : EFGHIJKLMN
    CUT LATTER (FIRST FIND PATTERN): ABCDEFGABC
    CUT LATTER (LONGEST PATTERN)   : ABC

¥³¥Þ¥ó¥É°ú¿ô

  1. arg.sh
    #!/bin/bash
    
    echo "TOTAL LENGTH OF ARGS : $#"
    
    CNT=1
    
    echo "ARG(0)=$0"
    
    until [ -z $1 ]
    do
      echo "ARG($CNT)=$1 (OTHER ARGS : $#)"
      CNT=$((CNT+1))
      shift
      
    done
    • $# : °ú¿ô¤Î¸Ä¿ô
    • $0 : ¼Â¹Ô¥³¥Þ¥ó¥É̾
    • shift¥³¥Þ¥ó¥É : for( n=1 ; n<($#-1) ; n++ ){ Âèn°ú¿ô <-- Âèn+1°ú¿ô; } $# = $# -1;
    • shift¥³¥Þ¥ó¥É¤Ç¤Ï¡¢Âè0°ú¿ô(¥³¥Þ¥ó¥É̾)¤ÏÊѤï¤é¤Ê¤¤¤³¤È¤ËÃí°Õ
  2. ¼Â¹Ô·ë²Ì
    > ./arg.sh arg1 arg2 arg3
    TOTAL LENGTH OF ARGS : 3
    ARG(0)=./arg.sh
    ARG(1)=arg1 (OTHER ARGS : 3)
    ARG(2)=arg2 (OTHER ARGS : 2)
    ARG(3)=arg3 (OTHER ARGS : 1)

´Ø¿ô

  1. function.sh
       #!/bin/bash
       
       GLOBAL_VAL=123
       
    ­¡ func1(){
         echo "----------FUNC1 START----------"
    
    ­¢   echo "FUNC1 ARG(0)=$0"
    ­£   echo "FUNC1 ARG(1)=$1"
    ­¤   echo "FUNC1 ARG(2)=$2"
         echo "GLOBAL_VAL  =$GLOBAL_VAL"
    
         GLOBAL_VAL=321
         PRIVATE_VAL=987
    
         echo "----------FUNC1 END  ----------"
    
    ­¥   return 0
       }
    
       echo "---------------SHELL START---------------"
    
    ­¦ if func1 A1 A2
       then
         echo "FUNC1 RETURNED TRUE"
       else
         echo "FUNC2 RETURNED FALSE"
       fi
    
    
    ­§ echo "SHELL ARG(0)=$0"
    ­¨ echo "SHELL ARG(1)=$1"
    ­© echo "SHELL ARG(2)=$2"
    
    ­ª echo "GLOBAL VALUE =$GLOBAL_VAL"
    ­« echo "PRIVATE VALUE=$PRIVATE_VAL"
    
       echo "--------------SHELL END  ---------------"
  2. ¼Â¹Ô·ë²Ì
    > ./function.sh S1 S2
       ---------------SHELL START---------------
       ----------FUNC1 START----------
    ­¢ FUNC1 ARG(0)=./function.sh
    ­£ FUNC1 ARG(1)=A1
    ­¤ FUNC1 ARG(2)=A2
       GLOBAL_VAL  =123
       ----------FUNC1 END  ----------
    ­¦ FUNC1 RETURNED TRUE
    ­§ SHELL ARG(0)=./function.sh
    ­¨ SHELL ARG(1)=S1
    ­© SHELL ARG(2)=S2
    ­ª GLOBAL VALUE =321
    ­« PRIVATE VALUE=987
    --------------SHELL END  ---------------
  3. ²òÀâ
    • ­¡ ´Ø¿ô¤Ï¤³¤ó¤ÊÉ÷¤ËÄêµÁ¤·¤Þ¤¹¡£()¤Ï¤¿¤À¤Î¾þ¤ê¤Ç°ú¿ô¤ÏÆþ¤ì¤Þ¤»¤ó
    • ­¦ ´Ø¿ô¤Ï¤³¤ó¤ÊÉ÷¤Ë¸Æ¤Ó½Ð¤·¤Þ¤¹¡£´Ø¿ô¤Ø¤Î°ú¿ô¤Ï¥¹¥Ú¡¼¥¹¶èÀÚ¤ê¤ÇÅϤ·¤Þ¤¹
    • ­¢¡Á­¤ ´Ø¿ô¤Ø¤Î°ú¿ô¤Ï¡¢Ä̾ï¤Î¥·¥§¥ë¸Æ¤Ó½Ð¤·¤ÈƱÍͤ˼õ¤±¼è¤ê¤Þ¤¹
    • ­¥ ´Ø¿ô¤ÎÊÖÃÍ¤Ï return ¥³¥Þ¥ó¥É¤ÇÊÖ¤·¤Þ¤¹¡£¤³¤ì¤âC¸À¸ì¤È¤ÏµÕ¤Ë 0 ¤¬¿¿¡¢1 ¤¬µ¶
    • ­§¡Á­© ¥·¥§¥ë¡¦´Ø¿ô¤Î°ú¿ô¤Ï¶É½êÊÑ¿ô
    • ­ª¡Á­« ÊÑ¿ô¤Ï¤É¤³¤ÇÄêµÁ¤µ¤ì¤Æ¤âÂç°èÊÑ¿ô¤Ë¤Ê¤ë

¾¤Î¥·¥§¥ë¥Õ¥¡¥¤¥ë¤ÎÆɤ߹þ¤ß

  1. common.sh
    #!/bin/bash
    
    sqrt(){
      return $(($1*$1))
    }
    
    isPositive(){
      if [ $1 -gt 0 ]
      then
        return 0
      else
        return 1
      fi
    }
    
    isNegative(){
      if [ $1 -lt 0 ]
      then
        return 0
      else
        return 1
      fi
    }
  2. calc.sh
    #!/bin/sh
    
    . common.sh
    
    sqrt $1
    echo "SQRT($1)=$?"
    
    if isPositive $2
    then
      echo "$2 is Positive"
    else
      echo "$2 is not Positive"
    fi
    
    if isNegative $3
    then
      echo "$3 is Negative"
    else
      echo "$3 is not Negative"
    fi
    • "."¥³¥Þ¥ó¥É¤Ç¾¤Î¥·¥§¥ë¥Õ¥¡¥¤¥ë¤òÆɤ߹þ¤á¤ë¡£"."¤Ï¡¢source¥³¥Þ¥ó¥É¤Î¥¨¥¤¥ê¥¢¥¹
  3. ¼Â¹Ô·ë²Ì
    > ./calc.sh 2 3 4
    SQRT(2)=4
    3 is Positive
    4 is not Negative

½ªÎ»¥³¡¼¥É(¥·¥§¥ë¤ÎÊÖÃͤÏ8bitÀ°¿ô¤Ê»ö¤ËÃí°Õ)

  1. exit.sh
    #/bin/bash
    exit $1
  2. ¼Â¹Ô·ë²Ì
    > ./exit.sh 0
    > echo $?
    0
     
    > ./exit.sh 1
    > echo $?
    1
     
    > ./exit.sh -1
    > echo $?
    255
     
    > ./exit.sh 1023
    > echo $?
    255

ifʸ

  1. ifʸ¤Î¹½Â¤
    if ¥³¥Þ¥ó¥É
    then
      ¥³¥Þ¥ó¥ÉÀ®¸ù»þ(ÊÖÃÍ0¤Î»þ)¤Î½èÍý
    else
      ¥³¥Þ¥ó¥É¼ºÇÔ»þ(ÊÖÃÍ0°Ê³°¤Î»þ)¤Î½èÍý
    fi
  2. ifʸ¤Î¸å¤Î¥³¥Þ¥ó¥ÉÊÖÃͤˤè¤Ã¤Æ¾ò·ïʬ´ô¤ò¹Ô¤¦
  3. ¿¿µ¶ÃͤÏC¸À¸ì¤ÈµÕ
  4. ¾ò·ïȽÃÇ¤Ë¤Ï AND / OR ¤â»È¤¨¤ë
    • if ¥³¥Þ¥ó¥É1 && ¥³¥Þ¥ó¥É2
    • if ¥³¥Þ¥ó¥É1 || ¥³¥Þ¥ó¥É2
  5. ¿¤¯¤Î¾ì¹ç¾ò·ïȽÃÇ¤Ë¤Ï test ¥³¥Þ¥ó¥É¤ò»È¤¦
  6. ½ô»ö¾ðÍ­¤Ã¤Æ then ¤Ç²¿¤â¤·¤¿¤¯¤Ê¤¤¤È¤­¤Ë¤Ï¡¢²¿¤â¤·¤Ê¤¤¥³¥Þ¥ó¥É : ¤ò¼Â¹Ô¤¹¤ë
    if ¥³¥Þ¥ó¥É
    then
      :
    else
      ¥³¥Þ¥ó¥É¼ºÇÔ»þ(ÊÖÃÍ0°Ê³°¤Î»þ)¤Î½èÍý
    fi

while/untilʸ

  1. while
    while ¥³¥Þ¥ó¥É
    do
      ¥³¥Þ¥ó¥ÉÊÖÃͤ¬¿¿(0)¤Î»þ¤Î½èÍý
    done
  2. until
    until ¥³¥Þ¥ó¥É
    do
      ¥³¥Þ¥ó¥ÉÊÖÃͤ¬µ¶(0°Ê³°)¤Î»þ¤Î½èÍý
    done
  3. while.sh
    #!/bin/bash
    
    COUNT=1
    
    ### WHILE LOOP ###
    
    while [ $COUNT -le 10 ]
    do
      echo -n "$COUNT ,"
      COUNT=$COUNT + 1
    done
    
    echo WHILE LOOP END : COUNT = $COUNT
    
    ### UNTIL LOOP ###
    
    until [ $COUNT -le 0 ]
    do
      echo -n "$COUNT ,"
      COUNT=`expr $COUNT - 1`
    done
    
    echo UNTIL LOOP END : COUNT = $COUNT
  4. ¼Â¹Ô·ë²Ì
    > ./while.sh
    1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,WHILE LOOP END : COUNT = 11
    11 ,10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1 ,UNTIL LOOP END : COUNT = 0
  5. °ì¹Ô¤º¤ÄÆɤ߹þ¤ß
    • ¥³¥Þ¥ó¥É·ë²Ì
      sed -e '/s/hoge/geho/g' foo.txt | while read line;
      do
        echo $line
      done
    • ¥Õ¥¡¥¤¥ë¤«¤é¤ÎÆɤ߹þ¤ß
      while read line;
      do
        echo $line
      done < file.txt
    • ¥¹¥¯¥ê¥×¥ÈÆâ¤Îʸ¾Ï
      while read line;
      do
        echo $line
      done <<END
      hoge
      geho
      END

test¥³¥Þ¥ó¥É

  1. ¥³¥Þ¥ó¥É°ú¿ô¤Î¿¿µ¶¤òȽÃǤ¹¤ë
    • ¡Ö test ¥³¥Þ¥ó¥É°ú¿ô ¡×¤È¡Ö test (¥³¥Þ¥ó¥É°ú¿ô) ¡×¤ÏÅù¤·¤¤
    • ¡Ö test ¥³¥Þ¥ó¥É°ú¿ô ¡×¤È¡Ö test !¥³¥Þ¥ó¥É°ú¿ô ¡×¤Ï¿¿µ¶¤¬µÕ¤Ë¤Ê¤ë
  2. [ ]¥³¥Þ¥ó¥É¤¬¥¨¥¤¥ê¥¢¥¹¤·¤Æ¤¤¤ë
    [~]$ which [
    /bin/[
    • ¡Ö test -f /bin/sh ¡×¡á¡Ö [ -f /bin/sh] ¡×
  3. ¥Õ¥¡¥¤¥ë¤Î¸ºß¤òȽÃǤ¹¤ë
    • test -e FILE ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¤«¡©
    • test -d FILE ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤·¡¢¤½¤ì¤Ï¥Ç¥£¥ì¥¯¥È¥ê¤«¡©
    • test -f FILE ¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤·¡¢¤½¤ì¤ÏÉáÄ̤Υե¡¥¤¥ë¤«¡©
  4. ʸ»úÎó¤òȽÃǤ¹¤ë
    • test -n STRING 0ʸ»ú°Ê¾å¤Îʸ»úÎ󤫡©
    • test -z STRING 0ʸ»ú¤Îʸ»úÎ󤫡©
    • test STRING1 = STRING2
    • test STRING1 != STRING2
    • ½ÅÍ×: ÊÑ¿ô¤ò"" ¤Ç°Ï¤àɬÍפ¢¤ê!
      [~]$ ./cmp.sh 
      $ARG=
      "test -n $ARG" is 0
      "test -z $ARG" is 0
      "test -n "$ARG"" is 1
      "test -z "$ARG"" is 0
      [~]$ ./cmp.sh aiueo
      $ARG=aiueo
      "test -n $ARG" is 0
      "test -z $ARG" is 1
      "test -n "$ARG"" is 0
      "test -z "$ARG"" is 1
      (0:TRUE / 1:FALSE) C¸À¸ì¤ÈµÕ
  5. À°¿ôÃͤòȽÃǤ¹¤ë
    • test INTEGER1 -eq INTERT2 EQual
    • test INTEGER1 -ne INTERT2 Not Equal
    • test INTEGER1 -gt INTERT2 INTEGER1 is Greater Than INTEGER2
    • test INTEGER1 -ge INTERT2 INTEGER1 is Greater than or Equal to INTEGER2
    • test INTEGER1 -lt INTERT2 INTEGER1 is Less Than INTEGER2
    • test INTEGER1 -le INTERT2 INTEGER1 is Less than or Equal to INTEGER2

Àµµ¬É½¸½¤Ë¤è¤ëÈæ³Ó

#!/bin/bash

WORD="I'm AHO."

if [[ $WORD =~ ^I.*[.]$ ]]
then
  echo "\"${WORD}\"¤Ï¡¢A¤Ç¤Ï¤¸¤Þ¤ê.¤Ç½ª¤ï¤ê¤Þ¤¹"
fi

if [[ $WORD =~ AHO ]]
then
  echo "\"${WORD}\"¤Ï¡¢AHO ¤ËÉôʬ°ìÃפ·¤Þ¤¹"
fi

if [[ ! $WORD =~ BAKA ]]
then
  echo "\"${WORD}\"¤Ï¡¢BAKA ¤ËÉôʬ°ìÃפ·¤Þ¤»¤ó"
fi

case $WORD in
  "I'm "?*)
    echo "\"${WORD}\"¤Ï¡¢\"I'm\"¤Ê¤ó¤È¤«¤Ç¤¹"
    ;;
  "You're "?*) 
    echo "\"${WORD}\"¤Ï¡¢\"You're\"¤Ê¤ó¤È¤«¤Ç¤¹"
    ;;
  *)
    echo "Not Match"
    ;;
esac
 
[~]$ ./regex.sh 
"I'm AHO."¤Ï¡¢A¤Ç¤Ï¤¸¤Þ¤ê.¤Ç½ª¤ï¤ê¤Þ¤¹
"I'm AHO."¤Ï¡¢AHO ¤ËÉôʬ°ìÃפ·¤Þ¤¹
"I'm AHO."¤Ï¡¢BAKA ¤ËÉôʬ°ìÃפ·¤Þ¤»¤ó
"I'm AHO."¤Ï¡¢"I'm"¤Ê¤ó¤È¤«¤Ç¤¹

forʸ

  1. ´ðËܵ¡Ç½¤Ï¥¹¥Ú¡¼¥¹¡¦²þ¹Ô¶èÀÚ¤ê¤Îʸ»úÎó¤òÀÚ¤ê½Ð¤¹
    # !/bin/bash
    
    for str in abc def ghi jkl mno pqr stu vwx yz
    do
      echo $str
    done
  2. ¼Â¹Ô·ë²Ì
    > ./for.sh
    abc
    def
    ghi
    jkl
    mno
    pqr
    stu
    vwx
    yz
  3. ÉáÄ̤ϡ¢¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê°Ê²¼¤Î¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ¥à¥Ë¥ã¥à¥Ë¥ã¤¹¤ë¤È¤¤¤¦»È¤¤Êý¤ò¤¹¤ë
    # !/bin/bash
    
    for file in *.txt
    do
      echo "$file --> /tmp/$file"
      cp $file /tmp/$file
    done
  4. ¼Â¹Ô·ë²Ì
    > ./for.sh
    gol.txt --> /tmp/gol.txt
    log.txt --> /tmp/log.txt

caseʸ

  1. ¤Û¤Ü¥³¥Þ¥ó¥É°ú¿ô¤Î²ò¼á¤Ë¤·¤«»È¤ï¤Ê¤¤¡£
    #!/bin/bash
    
    until [ -z $1 ]
    do
      case $1 in
      -v)
        shift ; VERBOSE=ON
        ;;
      -src)
        shift ; SRC=$1 ; shift
        ;;
      -dest)
        shift ; DEST=$1 ; shift
        ;;
      *)
        echo "Usage case.sh [-v] [-src] [SOURCE FILE] [-dest] [DEST_FILE]"
        exit -1
        ;;
      esac
    done
    
    echo "VERBOSE_MODE =${VERBOSE:=OFF}"
    echo "SOURCE_FILE = ${SRC:=UNDEF}"
    echo "DEST_FILE = ${DEST:=UNDEF}"
  2. ¼Â¹Ô·ë²Ì
    > ./case.sh -src abc.dat -dest def.txt
    VERBOSE_MODE =OFF
    SOURCE_FILE = abc.dat
    DEST_FILE = def.txt

¥Ç¥Ð¥Ã¥¯

> bash -x ./case.sh -src abc.dat -dest def.txt
+ '[' -z -src ']'
+ case $1 in
+ shift
+ SRC=abc.dat
+ shift
+ '[' -z -dest ']'
+ case $1 in
+ shift
+ DEST=def.txt
+ shift
+ '[' -z ']'
+ echo 'VERBOSE_MODE =OFF'
VERBOSE_MODE =OFF
+ echo 'SOURCE_FILE = abc.dat'
SOURCE_FILE = abc.dat
+ echo 'DEST_FILE = def.txt'
DEST_FILE = def.txt

Tips

¥Ñ¥¤¥×¤ò»È¤¦¤ÈÊÌ¥×¥í¥»¥¹¤Ë¤Ê¤ë¤³¤È¤ËÃí°Õ

Shell Script Æâ¤Ç¡¢¼«Ê¬¤ÎÃÖ¤«¤ì¤Æ¤¤¤ëÀäÂХѥ¹¤òÆÀ¤ë¤Ë¤Ï


Computer


*1 java -cp -Xms16m -Xmx1024m -XX:MaxPermSize?=256m com.snail.myApp

¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS   sitemap
Last-modified: 2012-08-14 (²Ð) 19:37:36 (4494d)
Short-URL:
ISBN10
ISBN13
9784061426061