[[TableOfContents]] ---- = µé¾î°¡¸é¼­ = ½©(shell)À̶ó´Â °Ç ÀÏÁ¾ÀÇ ¸í·É Çؼ®±âÀÔ´Ï´Ù. OS Ä¿³Î°ú »ç¿ëÀÚ »çÀÌÀÇ Áß°£ÀÚÀû ¿ªÇÒ ÀÌ»óÀ¸·Î ÇÁ·Î±×·¡¹Ö ¾ð¾î·Î¼­ÀÇ ±â´É ¶ÇÇÑ °­·ÂÇÏÁÒ. ½ºÅ©¸³Æ®(script)¶ó°íÇÏ´Â ½© ÇÁ·Î±×·¥Àº ½Ã½ºÅÛ ÄÝ, À¯Æ¿¸®Æ¼, ÀÌÁøÆÄÀÏ µîÀ» ¼­·Î ºÙÀÌ°í ¿¬°áÇؼ­ ¼Õ½±°Ô ÀÀ¿ëÇÁ·Î±×·¥À» °³¹ßÇϵµ·Ï µµ¿ÍÁÖ´Â ÅøÀ̶ó°í º¸½Ã¸é µË´Ï´Ù. »ç½Ç»ó ¸ðµç UNIX ¸í·É¾î, À¯Æ¿¸®Æ¼, Åø µîÀÌ ½ºÅ©¸³Æ®ÀÇ Àç·á°¡ µÉ ¼ö ÀÖ½À´Ï´Ù¸¸ ÀÌ°ÍÀ¸·Îµµ ÃæºÐÄ¡ ¾Ê´Ù¸é ½© ³»ºÎÀÇ Á¶°Ç¹®, ¹Ýº¹¹® µî ¿©·¯ ±¸¹®µéÀ» ¾²½Å´Ù¸é ½ºÅ©¸³Æ®°¡ Á»´õ °­·ÂÇÏ°í À¯¿¬ÇØÁö°Ú³×¿ä. ½© ½ºÅ©¸³Æ®´Â ½Ã½ºÅÛ °ü¸®(admin) ÀÛ¾÷À̳ª µ¢Ä¡ Å« ÇÁ·Î±×·¡¹Ö ¾ð¾î±îÁö´Â ÇÊ¿äÇÏÁö ¾ÊÀº ·çƾ ¹Ýº¹ÀûÀÎ ÀÛ¾÷¿¡ ƯÈ÷ ´É·ÂÀ» ¹ßÈÖÇÕ´Ï´Ù. == ¿Ö ½© ÇÁ·Î±×·¡¹ÖÀΰ¡? == ¼÷·ÃµÈ ½Ã½ºÅÛ °ü¸®ÀÚ¿¡°Ô À־ ½© ½ºÅ©¸³Æà Áö½ÄÀº ÇʼöÀÔ´Ï´Ù. ½ºÅ©¸³Æ® ÀÛ¼ºÇÒ ÀÏÀÌ ¾ø´Ù°í »ý°¢ÇÏ´Â °ü¸®ÀÚ¿¡°Ôµµ ¿¹¿Ü¾øÁÒ. ¾Æ½Ã´Ù½ÃÇÇ ¸®´ª½º ½Ã½ºÅÛÀÌ ºÎÆÃÇÒ ¶§ {{{/etc/rc.d}}} ¿¡ ÀÖ´Â ½ºÅ©¸³Æ®µéÀ» ½ÇÇàÇؼ­ ½Ã½ºÅÛ ¼³Á¤À» º¹¿øÇÏ°í ¼­ºñ½ºµéÀ» ¼¼ÆÃÇÕ´Ï´Ù. ½Ã½ºÅÛÀÇ µ¿Å¸¦ ºÐ¼®ÇÏ°í ¼öÁ¤ÇÏ·Á¸é ÀÌ·± ½Ãµ¿(startup) ½ºÅ©¸³Æ®µéÀ» ¼¼ºÎÀûÀ¸·Î ÀÌÇØÇؾ߰ÚÁÒ. ½© ½ºÅ©¸³Æ®¸¦ ÀÛ¼ºÇÏ´Â °ÍÀº ±×¸® ¾î·ÆÁö ¾Ê½À´Ï´Ù. ½ºÅ©¸³Æ®´Â ÇÑÀ԰Ÿ®ÀÇ ºÎºÐµé·Î Â¥Áý±â µÇ¾î ÀÖ°í ½© °íÀ¯ÀÇ ¿¬»êÀÚ³ª ¿É¼Çµé[[Footnote(11Àå. ³»ºÎ ¸í·É¾î¿Í ³»ÀåÇÔ¼ö ¿¡¼­ ¼³¸íÇÕ´Ï´Ù.)]]µµ ¸¹Áö ¾Ê±â ¶§¹®ÀÌÁÒ. ¹®¹ýµµ ´Ü¼øÇÏ°í Á÷°üÀûÀÌ¸ç ¸í·ÉÇà »ó¿¡¼­ À¯Æ¿¸®Æ¼µéÀ» ½ÇÇàÇÏ°í ¿¬°áÇÏ´Â ¹æ½Ä°ú ºñ½ÁÇÕ´Ï´Ù. ÀÍÇô¾ß ÇÒ ±ÔÄ¢(rule)µµ ±×¸® ¸¹Áö ¾Ê¾Æ¿ä. º¹ÀâÇÑ ÀÀ¿ëÇÁ·Î±×·¥ÀÇ ÇÁ·ÎÅäŸÀÔ(prototype)À» ÀÛ¼ºÇÏ´Â µ¥ "quick and dirty"ÇÑ ¹æ¹ýÀ¸·Î ½© ½ºÅ©¸³Æ®°¡ ÀÌ¿ëµÇ±âµµ ÇÕ´Ï´Ù. ÇÁ·ÎÁ§Æ® °³¹ßÀÇ Ã¹ ´Ü°è·Î¼­ ºÎºÐÀûÀ¸·Î ±â´ÉÀ» ±¸ÇöÇÏ´Â µ¥ ½©½ºÅ©¸³Æ®¸¦ ÀÌ¿ëÇÏ¿© Å×½ºÆ®Çϸ鼭 C, C++, Java ³ª Perl µîÀ¸·Î ÃÖÁ¾ ÄÚµùµÇ±â Àü¿¡ ·çƾÀÇ ±¸Á¶Àû ȤÀº ³í¸®Àû °áÇÔÀÌ ¹ß°ßµÇÁÒ. ½© ½ºÅ©¸³ÆÃÀº º¹ÀâÇÑ ÇÁ·ÎÁ§Æ®´Â ´õ ´Ü¼øÇÑ ¿ä¼Òµé·Î ³ª´²¼­ ÀÛ¾÷ÇÏ°í ´Ù½Ã °áÇÕÇÑ´Ù´Â °íÀüÀûÀÎ UNIX öÇÐÀ» Àß ÀÌÇàÇÏ´Â ¹æ¹ýÀÔ´Ï´Ù. ¸¹Àº »ç¶÷µéÀÌ Â÷¼¼´ëÀÇ ¿ÃÀοø(all-in-one) °í±Þ ¾ð¾î¸¦ »ç¿ëÇÏ´Â °Íº¸´Ù´Â ÀÌ·¯ÇÑ ¹æ½ÄÀÌ ´õ ÁÁ°Å³ª ¸Å·ÂÀûÀ̶ó°í »ý°¢ÇÏÁÒ. ¸ðµç »ç¶÷¿¡°Ô ¸ðµç ÀÏÀÌ °¡´ÉÇϵµ·Ï ±âȹµÈ ±×·± ¾ð¾î¸¦ »ç¿ëÇÏ·Á¸é ÀÚ½ÅÀÇ »ç°í¹æ½Ä°ú ÀýÂ÷¸¦ ±× µµ±¸¿¡ ¸Â°Ô °íÃÄ¾ß ÇÏ´Â ºÎ´ãÀº °¨¼öÇØ¾ß ÇÕ´Ï´Ù. ´ÙÀ½ÀÇ ÀÛ¾÷À» ÇÒ »ý°¢À̶ó¸é ½© ½ºÅ©¸³Æ®´Â Àû´çÇÏÁö ¾Ê½À´Ï´Ù. * ÀÚ¿ø(resource)Áý¾àÀûÀÎ ÀÛ¾÷ : ƯÈ÷ ¼Óµµ°¡ »ý¸íÀÎ ÀÛ¾÷ (Sorting, Hashing µî) * Áß¿äÇÑ ¼öÄ¡ ¿¬»ê : ƯÈ÷ ºÎµ¿ ¼Ò¼öÁ¡, ÀÓÀÇÀÇ Á¤¹Ðµµ, º¹¼Ò¼ö °è»ê µî (C++ À̳ª Æ÷Æ®¶õÀ» ¾²¼¼¿ä) * Ç÷§Æû°£ À̽ļºÀÌ ¿ä±¸µÇ´Â ÀÛ¾÷ (C¸¦ ¾²¼¼¿ä) * ±¸Á¶È­µÈ ÇÁ·Î±×·¡¹ÖÀÌ ÇÊ¿äÇÑ ÀÛ¾÷ (ÇÔ¼öÇü, º¯¼öÇü µîÀ» È®ÀÎÇØ¾ß ÇÏ´Â ÀÛ¾÷) * ȸ»çÀÇ ¹Ì·¡°¡ °É·ÁÀÖ´Â Áß´ëÇÑ ÀÀ¿ëÇÁ·Î±×·¥ * º¸¾ÈÀÌ Áß¿ä½ÃµÇ´Â »óȲ : ½Ã½ºÅÛÀÇ ¹«°á¼ºÀ» º¸ÀåÇØ¾ß Çϰųª ÇØÅ·À̳ª Å©·¡Å·À¸·ÎºÎÅÍ º¸È£ÇØ¾ß ÇÏ´Â »óȲ * ±Ô¸ð°¡ Å« ÆÄÀÏ ¿¬»êÀÌ ¿ä±¸µÇ´Â ÀÛ¾÷ (Bash´Â ¿¬¼ÓÀûÀÎ ÆÄÀÏ Á¢±Ù¿¡ Á¦ÇÑÀûÀÔ´Ï´Ù) * ´ÙÂ÷¿ø ¹è¿­ÀÌ ÇÊ¿äÇÑ ÀÛ¾÷ * ¿¬°á¸®½ºÆ®³ª Æ®¸® °°Àº ÀڷᱸÁ¶°¡ ÇÊ¿äÇÑ ÀÛ¾÷ * ±×·¡ÇÈÀ̳ª GUI¸¦ »ý¼ºÇØ¾ß ÇÏ´Â ÀÛ¾÷ * Çϵå¿þ¾î¿¡ Á÷Á¢ Á¢±ÙÇØ¾ß ÇÏ´Â ÀÛ¾÷ * Æ÷Æ®(port)³ª ¼ÒÄÏI/O¸¦ ÇÊ¿ä·Î ÇÏ´Â ÀÛ¾÷ * ±¸½Ä ÄÚµå·Î ¶óÀ̺귯¸®³ª ÀÎÅÍÆäÀ̽º¸¦ »ç¿ëÇØ¾ß ÇÏ´Â ÀÛ¾÷ * ¼ÒÀ¯±ÇÀÌ Àְųª ¼Ò½º°¡ °ø°³µÇ¾î¼­´Â ¾ÈµÇ´Â ÇÁ·Î±×·¥ À§ÀÇ ÀÛ¾÷À» Çؾ߰ÚÀ¸¸é Á»´õ °­·ÂÇÑ ½ºÅ©¸³Æ® ¾ð¾î(Perl, Tcl, Python, Ruby µî)³ª ÄÄÆÄÀÏ ¾ð¾î(C, C++, Java µî)¸¦ ¾²´Â°Ô ÁÁ½À´Ï´Ù. ¹°·Ð ÀÌ ¶§¿¡µµ ºÎºÐ ±â´ÉÀ» ±¸ÇöÇÏ´Â µ¥ ½© ½ºÅ©¸³Æ®·Î ÇÁ·ÎÅäŸÀÔÀ» ÀÛ¼ºÇغ¸´Â °Ç ¾µ¸ð°¡ ÀÖ°ÚÁÒ. ¿ì¸®°¡ ¾ÕÀ¸·Î ÇÔ²² ¹è¿öº¼ ½©Àº BashÀÔ´Ï´Ù. Bash´Â "Bourne-Again shell"ÀÇ ¸Ó¸´±ÛÀÚÀÌ°í Stephen BourneÀÇ Bourne Shell¿¡¼­ À¯·¡ÇßÁÒ. Bash´Â »ç½Ç»ó °ÅÀÇ ¸ðµç UNIX(-like)¿¡¼­ ½©½ºÅ©¸³ÆÃÀÇ Ç¥ÁØÀÌ µÇ¾ú½À´Ï´Ù. ¾ÕÀ¸·Î ¾ê±âÇÒ ´ëºÎºÐÀÇ ¿ø¸®µéÀº Korn Shell[[Footnote(Many of the features of ksh88, and even a few from the updated ksh93 have been merged into Bash.)]]À̳ª C Shell°ú °°Àº ´Ù¸¥ ½©¿¡¼­ÀÇ ½ºÅ©¸³Æÿ¡µµ Àû¿ëÇØ º¼ ¼ö ÀÖ¾î¿ä. (C Shell ÇÁ·Î±×·¡¹ÖÀº ÃßõÇÏÁö ¾Ê½À´Ï´Ù. 1993³â 10¿ù¿¡ Tom Christiansen ÀÌ C ShellÀÇ Å¸°í³­ °áÇÔ¿¡ ´ëÇØ À¯Áî³Ý¿¡ Æ÷½ºÆ®Çß¾ú³×¿ä.) À̾îÁú ³»¿ëÀº ½© ½ºÅ©¸³Æà ƩÅ丮¾ó(tutorial)ÀÔ´Ï´Ù. ½©ÀÇ ´Ù¾çÇÑ Æ¯Â¡µéÀ» ¼³¸íÇϱâ À§ÇØ ¸¹Àº ¿¹Á¦µéÀ» °çµé¿´´Âµ¥ ¸î¸îÀº ±×´ë·Î °¡Á®´Ù ½áµµ À¯¿ëÇÑ °ÍµéÀÌÁÒ. ¿¹Á¦ ½ºÅ©¸³Æ®ÀÇ ¼Ò½ºÆÄÀÏ({{{scriptname.sh}}} ¶Ç´Â {{{scriptname.bash}}})[[Footnote(»ç¿ëÀÚ°¡ ÀÛ¼ºÇÑ ½ºÅ©¸³Æ®´Â È®ÀåÀÚ¸¦ .sh ·Î ÇÏ´Â °ÍÀÌ °ü·ÊÀÔ´Ï´Ù. /etc/rc.d ¿¡ ÀÖ´Â °Í°ú °°Àº ½Ã½ºÅÛ ½ºÅ©¸³Æ®´Â ÀÌ ¸í¸í¹ýÀ» µû¸£Áö ¾Ê¾Æ¿ä.)]]À» ½ÇÇàÇÏ·Á¸é ¸ÕÀú {{{chmod u+rx scriptname}}} °ú °°ÀÌ ½ÇÇàÆ۹̼ÇÀ» ÁÖ¾î¾ß ÇÑ´Ù´Â °Í ÀØÁö ¸¶¼¼¿ä. == Sha-Bang(#!)À¸·Î ½ÃÀÛÇÏÀÚ == °¡Àå °£´ÜÇÑ ½ºÅ©¸³Æ®¸¦ º¸¸é, ½ºÅ©¸³Æ®¶ó´Â °ÍÀº ½Ã½ºÅÛ ¸í·É¾îµéÀ» ÆÄÀÏ ¾È¿¡ ³ª¿­ÇÑ °Í¿¡ Áö³ªÁö ¾Ê±¸³ª ÇÏ´Â »ý°¢ÀÌ µé°Ì´Ï´Ù. ±×·¸´Ù°í Çصµ ½ÇÇàÇÒ ¶§¸¶´Ù µ¿ÀÏÇÑ ¸í·É¾îµéÀ» ´Ù½Ã ŸÀÌÇÎÇÏ´Â ¼ö°í´Â ´ú¾îÁÖ´Â °ÅÁÒ. '''¿¹Á¦ 2-1. cleanup : /var/log ¾È¿¡ ÀÖ´Â ·Î±×ÆÄÀÏ Ã»¼ÒÇÏ´Â ½ºÅ©¸³Æ®''' {{{#!vim sh # Cleanup # Run as root, of course. cd /var/log cat /dev/null > messages cat /dev/null > wtmp echo "Logs cleaned up."}}} º¸½Ã´Â °Íó·³ ±×¸® Ưº°ÇÑ °Ç ¾ø½À´Ï´Ù. ÄܼÖ(console)À̳ª ''xterm'' ÀÇ ¸í·ÉÇà¿¡¼­ ÇÑÁÙ ÇÑÁÙ ½ÇÇàÇÒ ¼ö ÀÖ´Â ¸í·É¾îµéÀ» ¹­¾î³õÀº °Í »ÓÀ̴ϱî¿ä. ±×·¡µµ ÀÌ ÀÛ¾÷À» ÇÒ ¶§¸¶´Ù ´Ù½Ã ŸÀÌÇÎÇÏÁö ¾Ê¾Æµµ µÇ´Â ÀÌÁ¡ÀÌ ÀÖÀ¸´Ï ÀÌ ½ºÅ©¸³Æ®´Â ¿ì¸®¿¡°Ô À¯¿ëÇÑ ÅøÀÌ°í, ÇÊ¿ä¿¡ µû¶ó ½±°Ô ¼öÁ¤ÇÏ°í »õ·Î¿î ±â´ÉÀ» Ãß°¡ÇÒ ¼ö ÀÖ½À´Ï´Ù. '''¿¹Á¦ 2-2. cleanup : °³¼±µÈ clean-up ½ºÅ©¸³Æ®''' {{{#!vim sh #!/bin/bash # Proper header for a Bash script. # Cleanup, version 2 # Run as root, of course. # Insert code here to print error message and exit if not root. LOG_DIR=/var/log # Variables are better than hard-coded values. cd $LOG_DIR cat /dev/null > messages cat /dev/null > wtmp echo "Logs cleaned up." exit # The right and proper method of "exiting" from a script.}}} ÀÌÁ¦¾ß ÁøÂ¥ ½ºÅ©¸³Æ®Ã³·³ º¸À̱⠽ÃÀÛÇϳ׿ä. Á»´õ °¡º¾½Ã´Ù... '''¿¹Á¦ 2-3. cleanup : À§ ½ºÅ©¸³Æ®¸¦ °­È­ÇÏ°í ÀϹÝÈ­ÇÑ ¹öÀü''' {{{#!vim sh #!/bin/bash # Cleanup, version 3 # Warning: # ------- # This script uses quite a number of features that will be explained #+ later on. # By the time you've finished the first half of the book, #+ there should be nothing mysterious about it. LOG_DIR=/var/log ROOT_UID=0 # Only users with $UID 0 have root privileges. LINES=50 # Default number of lines saved. E_XCD=66 # Can't change directory? E_NOTROOT=67 # Non-root exit error. # Run as root, of course. if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit $E_NOTROOT fi if [ -n "$1" ] # Test if command line argument present (non-empty). then lines=$1 else lines=$LINES # Default, if not specified on command line. fi # Stephane Chazelas suggests the following, #+ as a better way of checking command line arguments, #+ but this is still a bit advanced for this stage of the tutorial. # # E_WRONGARGS=65 # Non-numerical argument (bad arg format) # # case "$1" in # "" ) lines=50;; # *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;; # * ) lines=$1;; # esac # #* Skip ahead to "Loops" chapter to decipher all this. cd $LOG_DIR if [ `pwd` != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ] # Not in /var/log? then echo "Can't change to $LOG_DIR." exit $E_XCD fi # Doublecheck if in right directory, before messing with log file. # far more efficient is: # # cd /var/log || { # echo "Cannot change to necessary directory." >&2 # exit $E_XCD; # } tail -$lines messages > mesg.temp # Saves last section of message log file. mv mesg.temp messages # Becomes new log directory. # cat /dev/null > messages #* No longer needed, as the above method is safer. cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect. echo "Logs cleaned up." exit 0 # A zero return value from the script upon exit #+ indicates success to the shell.}}} ÀÌ ¹öÀüÀº ½Ã½ºÅÛ ·Î±×¸¦ ¸ù¶¥ Áö¿ìÁö ¾Ê°í ·Î±×ÀÇ ¸¶Áö¸· ºÎºÐÀ» Á¶±Ý ³²°ÜµÓ´Ï´Ù. ÀÌÈÄ¿¡ ½ºÅ©¸³Æ® È¿À²À» ³ôÀδٵçÁö ´Ù¸¥ ÇÊ¿ä°¡ »ý±ä´ÙµçÁö ÇÒ ¶§¸¶´Ù ¿©·¯ºÐÀÌ ½ºÅ©¸³Æ®¸¦ ´Ùµë¾î °¡¼¼¿ä. ½ºÅ©¸³Æ® ¸Ç óÀ½¿¡ µîÀåÇÏ´Â ''sha-bang''(#!)Àº ¾Æ·¡ÀÇ ½ºÅ©¸³Æ®´Â ÁöÁ¤ÇÑ ¸í·É Çؼ®±â¸¦ ÅëÇؼ­ ¼öÇàµÇ¾î¾ß ÇÑ´Ù´Â °É ½Ã½ºÅÛ¿¡ ¾Ë·ÁÁÖ´Â ¿ªÇÒÀ» ÇÕ´Ï´Ù. #! ´Â »ç½Ç 2¹ÙÀÌÆ®[[Footnote(4.2BSD ±â¹ÝÀÇ ¸î¸î UNIX¿¡¼­´Â 4¹ÙÀÌÆ® ¸ÅÁ÷³Ñ¹ö¸¦ ¾²±âµµ ÇÕ´Ï´Ù. #! /sh/bash ó·³ ! ´ÙÀ½¿¡ ºóÄ­À» ³Ö¾îÁÖ¾î¾ß ÇÏÁÒ.)]] ¸ÅÁ÷³Ñ¹ö(''magic number'', ÀÚ¼¼ÇÑ ³»¿ëÀº '''man magic'''À» Ãĺ¸¼¼¿ä)ÀÔ´Ï´Ù. ''sha-bang'' ´ÙÀ½¿¡´Â °ð¹Ù·Î ''°æ·Î¸í''ÀÌ ¿Í¾ß Çϴµ¥, ½ºÅ©¸³Æ®¿¡ ¾²ÀÎ ¸í·ÉµéÀ» Çؼ®ÇÒ ÇÁ·Î±×·¥ °æ·ÎÀÌ°í ½©, ÇÁ·Î±×·¡¹Ö ¾ð¾î ¶Ç´Â À¯Æ¿¸®Æ¼ µîÀÌ ¿Ã ¼ö ÀÖ¾î¿ä. ''sha-bang''¶óÀÎÀÇ ´ÙÀ½ ÁÙºÎÅʹ ȣÃâµÈ ¸í·É Çؼ®±â°¡ ¼øÂ÷ÀûÀ¸·Î 󸮸¦ ÇÏ°í, À̶§ ÁÖ¼®(comments)Àº ¹«½ÃÇÕ´Ï´Ù.[[Footnote(sh³ª bash¿Í °°Àº ¸í·É Çؼ®±â°¡ ½ºÅ©¸³Æ® óÀ½¿¡ µîÀåÇÏ´Â #!¶óÀο¡¼­ºÎÅÍ Çؼ®À» ½ÃÀÛÇÕ´Ï´Ù¸¸ ÀÌ ÁÙÀº #·Î ½ÃÀÛÇ߱⠶§¹®¿¡ ÁÖ¼®À¸·Î º¸°í ¹«½ÃÇÕ´Ï´Ù. ¹°·Ð #!¶óÀÎÀº ¸í·É Çؼ®±â È£ÃâÀ̶ó´Â º»·¡ÀÇ ¸ñÀûÀ» ´Þ¼ºÇÑ ÀÌÈÄÀ̱⠶§¹®¿¡ »ó°üÀº ¾ø´Â°ÅÁÒ. ¸¸¾à¿¡ ½ºÅ©¸³Æ® Áß°£¿¡ ¶Ç #!¶óÀÎÀÌ ³ªÅ¸³­´Ù°í Çصµ bash´Â À̸¦ ÁÖ¼®À¸·Î ó¸®ÇÕ´Ï´Ù.)]] {{{#!/bin/sh #!/bin/bash #!/usr/bin/perl #!/usr/bin/tcl #!/bin/sed -f #!/usr/awk -f}}} À§ÀÇ ''sha-bang''¶óÀεéÀº °¢±â ´Ù¸¥ ¸í·É Çؼ®±â¸¦ È£ÃâÇϴϱî ÇÊ¿äÇÑ ´ë·Î Çϳª¸¸ ¼±ÅÃÇϼ¼¿ä. ´ëºÎºÐÀÇ »ó¾÷ÀûÀÎ UNIX·ùÀÇ ¿î¿µÃ¼Á¦¿¡¼­ ±âº»(default)À¸·Î äÅÃÇÏ°í ÀÖ´Â '''#!/bin/sh'''À» »ç¿ëÇÑ´Ù¸é ½ºÅ©¸³Æ®¸¦ ºñ¸®´ª½º ¸Ó½Å¿¡ À̽ÄÇÒ ¼ö ÀÖ°Ô µË´Ï´Ù. ¹°·Ð Bash °íÀ¯ÀÇ Æ¯Â¡µéÀº ¸ø ¾²°ÚÁö¸¸ ½ºÅ©¸³Æ®´Â POSIX[[Footnote(Portable Operating System Interface. UNIX-like OSµéÀ» Ç¥ÁØÈ­Çϱâ À§ÇÑ ½Ãµµ·Î, POSIX ½ºÆåÀº Open Group »çÀÌÆ®¿¡ Àß ³ª¿Í ÀÖ½À´Ï´Ù.)]] '''sh'''Ç¥ÁØÀ» µû¸£°Ô µÇ´Â °ÅÁÒ. ''sha-bang''¶óÀÎÀÇ °æ·Î¸íÀº À¯È¿ÇÑ °ÍÀ̾î¾ß ÇÕ´Ï´Ù. ±×·¸Áö ¾ÊÀ¸¸é ¿¡·¯ ¸Þ¼¼Áö(º¸Åë "Command not found")¸¸ µþ¶û ³»º¸³»°Åµç¿ä. ½© ³»ºÎÀÇ ¸í·ÉµéÀ» »ç¿ëÇÏÁö ¾Ê°í ÀϹÝÀûÀÎ ½Ã½ºÅÛ ¸í·Éµé¸¸ Âß ³ª¿­ÇÑ ½ºÅ©¸³Æ®¶ó¸é(À§ÀÇ Ã¹¹ø° ¿¹Á¦Ã³·³) #!¶óÀÎÀ» »ý·«ÇÒ ¼ö ÀÖ½À´Ï´Ù. À§ÀÇ µÎ¹ø° ¿¹Á¦¿¡¼­´Â ½© °íÀ¯ÀÇ ±¸¹®ÀÎ º¯¼ö ÇÒ´ç¹®('''lines=50''')À» »ç¿ëÇ߱⠶§¹®¿¡ ¹Ýµå½Ã #!¶óÀÎÀ» ½á¾ß ÇÏÁÒ. ¶Ç Çϳª ¸í½ÉÇÒ °ÍÀº '''#!/bin/sh'''Àº ±âº»(default) ½©À» ½ÇÇàÇÕ´Ï´Ù. ¸®´ª½º ¸Ó½Å¿¡¼­´Â {{{/bin/bash}}}°¡ ±âº» ½©ÀÌ¿¡¿ä. (!) ½ºÅ©¸³Æ®¸¦ ÀÛ¼ºÇÏ½Ç ¶§ ¸ðµâ¹æ½ÄÀÇ Á¢±ÙÀ» ±ÇÀåÇÕ´Ï´Ù. ¹Ýº¹ÀûÀ¸·Î »ç¿ëµÇ´Â Å丷 ÄÚµå(snippets)´Â ³ªÁß¿¡ Àç»ç¿ëÇÒ ¼ö ÀÖµµ·Ï ¸ð¾ÆµÎ¼¼¿ä. ÀÌ·¸°Ô ÇÏ´Ùº¸¸é ¸¶Ä§³»´Â ¸ÚÁø ¶óÀ̺귯¸®°¡ ¿Ï¼ºµÇ°ÚÁÒ. ¿¹¸¦ µé¾î, ¾Æ·¡ÀÇ Å丷 ÄÚµå´Â ½ºÅ©¸³Æ®°¡ ½ÇÇàµÉ ¶§ ³Ñ°Ü¹ÞÀº ÆĶó¸ÞÅÍ(parameter)ÀÇ ¼ö°¡ ¿Ã¹Ù¸¥Áö °Ë»çÇÏ´Â °Ì´Ï´Ù. {{{#!vim sh E_WRONG_ARGS=65 script_parameters="-a -h -m -z" # -a = all, -h = help, etc. if [ $# -ne $Number_of_expected_args ] then echo "Usage: `basename $0` $script_parameters" # `basename $0` is the script's filename. exit $E_WRONG_ARGS fi}}} óÀ½¿¡´Â ¿¹Á¦ 2-1ó·³ ÇÑ°¡Áö ƯÁ¤ ÀÛ¾÷¸¸ ¼öÇàÇÏ´Â ½ºÅ©¸³Æ®¸¦ ÀÛ¼ºÇÏ°ÚÁö¸¸, ³ªÁß¿¡´Â ºñ½ÁÇϰųª Á¶±Ý ´Ù¸¥ ÀÛ¾÷¿¡µµ ÀÌ¿ëÇÒ ¼ö ÀÖµµ·Ï ½ºÅ©¸³Æ®¸¦ ÀϹÝÈ­ÇÏ°Ô µÉ °Ì´Ï´Ù. ±×·²·Á¸é ¸ÕÀú, ¹Ýº¹µÈ Äڵ带 ÇÔ¼ö·Î ¹­´Â °Íó·³, ¹®ÀÚ »ó¼ö¸¦ º¯¼ö·Î ¹Ù²Ù¼¼¿ä. (¿¹Á¦ 2-1¿¡¼­ 2-2·Î º¯È­µÈ Á¡À» »ìÆ캸¼¼¿ä.) === ½ºÅ©¸³Æ® ½ÇÇàÇϱâ === ½ºÅ©¸³Æ®¸¦ ÀÛ¼ºÇßÀ¸¸é '''sh scriptname'''[[Footnote(ÁÖÀÇ: "sh scriptname"À¸·Î Bash ½ºÅ©¸³Æ®¸¦ ½ÇÇàÇϸé Bash °íÀ¯ÀÇ ±â´ÉÀº ²¨Áý´Ï´Ù. µû¶ó¼­ ½ÇÇàÀÌ ¾ÈµÉ ¼öµµ ÀÖ¾î¿ä.)]]³ª '''bash scriptname'''À» Ãļ­ ½ÇÇàÇØ º¸¼¼¿ä. (Not recommended is using sh ÁÖ¼®¶óÀÎ ¾È¿¡ ¸í·É¹®À» ¾²½Ã¸é ¸¶Âù°¡Áö·Î ÁÖ½Äó¸® µÇ´Ï±î ´ÙÀ½ ¸í·É¹®Àº »õ·Î¿î ÁÙ¿¡ ¾²¼¼¿ä. #·Î Çѹø ½ÃÀÛÇÑ ÁÖ¼®¶óÀÎÀ» ±× ÁÙ¿¡¼­ Á¾·á½Ãų ¹æ¹ýÀº ¾ø½À´Ï´Ù. (./) ¹°·Ð, '''echo'''¹®¿¡¼­ À̽ºÄÉÀÌÇÁ ó¸®ÇÑ #Àº ÁÖ¼®À¸·Î º¸Áö ¾Ê½À´Ï´Ù. ÆĶó¸ÞÅÍ Ä¡È¯½ÄÀ̳ª ¼ýÀÚ »ó¼ö Ç¥Çö½Ä, ÆÐÅÏ ¸ÅĪ ¿¬»ê¿¡¼­ ¾²ÀÌ´Â #µµ ¸¶Âù°¡ÁöÁÒ. {{{#!vim sh echo "The # here does not begin a comment." echo 'The # here does not begin a comment.' echo The \# here does not begin a comment. echo The # here begins a comment. echo ${PATH#*:} # Parameter substitution, not a comment. echo $(( 2#101011 )) # Base conversion, not a comment. # Thanks, S.C.}}} #¸¦ À̽ºÄÉÀÌÇÁ ó¸®ÇÏ´Â µ¥ " ' \ ¿Í °°Àº ¹®ÀÚ°¡ »ç¿ëµË´Ï´Ù. ; '''¸í·É ±¸ºÐÀÚ ![semicolon]'''. ÇÑÁÙ¿¡ µÑ ÀÌ»óÀÇ ¸í·É¹®ÀÌ °¡´ÉÇÏ°Ô ÇÏÁÒ. {{{#!vim sh echo hello; echo there if [ -x "$filename" ]; then # Note that "if" and "then" need separation. # Why? echo "File $filename exists."; cp $filename $filename.bak else echo "File $filename not found."; touch $filename fi; echo "File test complete."}}} {{{;;}}} '''__case__ ±¸¹®¿¡¼­ ¼±Åù® Á¾°áÀÚ ![double semicolon]'''. {{{#!vim sh case "$variable" in abc) echo "\$variable = abc" ;; xyz) echo "\$variable = xyz" ;; esac}}} . '''"dot" command ![period]'''. Equivalent to source (see Example 11-20). This is a bash builtin. . '''"dot", ÆÄÀÏ¸í¿¡¼­'''. ÆÄÀϸíÀÌ "."À¸·Î ½ÃÀÛÇÏ¸é ¼û±èÆÄÀÏÀÌ µË´Ï´Ù. {{{bash$ touch .hidden-file bash$ ls -l total 10 -rw-r--r-- 1 bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo 877 Dec 17 2000 employment.addressbook bash$ ls -al total 14 drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./ drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../ -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressbook -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressbook.bak -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.addressbook -rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file}}} µð·ºÅ丮¸íÀ» ´ë½ÅÇØ ¾µ ¼öµµ Àִµ¥, "."Àº ÇöÀç µð·ºÅ丮¸¦, ".."Àº »óÀ§ µð·ºÅ丮¸¦ ÀǹÌÇÕ´Ï´Ù. {{{bash$ pwd /home/bozo/projects bash$ cd . bash$ pwd /home/bozo/projects bash$ cd .. bash$ pwd /home/bozo/}}} "."Àº ÆÄÀÏÀ» À̵¿½ÃÅ°´Â ¸í·É¿¡¼­ ¸ñÀûÁö(µð·ºÅ丮)¸¦ ´ë½ÅÇؼ­ ÀÚÁÖ ¾²ÀÔ´Ï´Ù. {{{bash$ cp /home/bozo/current_work/junk/* . }}} . '''"dot", Á¤±ÔÇ¥Çö½Ä¿¡¼­'''. Á¤±ÔÇ¥Çö½Ä¿¡¼­ "."Àº ¹®ÀÚ Çϳª¿¡ ¸ÅÄ¡µË´Ï´Ù. " '''partial quoting ![double quote]'''. "STRING" preserves (from interpretation) most of the special characters within STRING. See also Chapter 5. ' '''full quoting ![single quote]'''. 'STRING' preserves all special characters within STRING. This is a stronger form of quoting than using ". See also Chapter 5. , '''ÄÞ¸¶(comma) ¿¬»êÀÚ'''. ÄÞ¸¶ ¿¬»êÀÚ´Â ÀÏ·ÃÀÇ ¼öÄ¡ ¿¬»êÀ» ¿¬°áÇÕ´Ï´Ù. ¿¬°áµÈ ¿¬»ê½Ä ¸ðµÎ¸¦ Æò°¡ÇÏÁö¸¸ ¸¶Áö¸· °Í¸¸ ¹ÝȯÇØ¿ä. {{{#!vim sh let "t2 = ((a = 9, 15 / 3))" # Set "a = 9" and "t2 = 15 / 3"}}} \ '''À̽ºÄÉÀÌÇÁ ¹®ÀÚ ![backslash]'''. A quoting mechanism for single characters. \X "escapes" the character X. This has the effect of "quoting" X, equivalent to 'X'. The \ may be used to quote " and ', so they are expressed literally. See Chapter 5 for an in-depth explanation of escaped characters / '''ÆÄÀÏ¸í °æ·Î ±¸ºÐÀÚ ![forward slash]'''. {{{/home/bozo/projects/Makefile}}}ó·³ ÆÄÀÏ¸í¿¡¼­ ±¸ºÐÀÚ ¿ªÇÒÀ» Çϴµ¥, »ê¼ú¿¡¼­ ³ª´°¼À ¿¬»êÀڷεµ ¾²ÀÌÁÒ. ` '''command ġȯ [backquote, backtick]'''. `·Î °¨½Ñ command´Â commandÀÇ Ãâ·Â¹°À» º¯¼ö¿¡ ´ëÀÔÇÒ ¶§ ÀÌ¿ëÇÕ´Ï´Ù. : '''null command ![colon]'''. "NOP" (no op, a do-nothing operation)¿Í °°½À´Ï´Ù. ½© ³»ÀåÀÇ true¿Í µ¿ÀǾî·Î º¸½Ã¸é µË´Ï´Ù. ":" Àº ±× ÀÚü·Î ¾²ÀÏ ¼ö ÀÖ°í exit°ªÀº "true" (0)ÀÔ´Ï´Ù. {{{#!vim sh : echo $? # 0}}} ¹«ÇÑ ·çÇÁ: {{{#!vim sh while : do operation-1 operation-2 ... operation-n done # Same as: # while true # do # ... # done}}} Placeholder in if/then test: {{{#!vim sh if condition then : # Do nothing and branch ahead else take-some-action fi}}} Provide a placeholder where a binary operation is expected, see Example 8-2 and default parameters. {{{#!vim sh : ${username=`whoami`} # ${username=`whoami`} Gives an error without the leading : # unless "username" is a command or builtin...}}} ---- [[Footnote]]