Advanced Shell Programming

Links to other sources

Unix ¤§Àu¯ÊÂI

¾Ç²ß Unix Shell Programing ¤§¤èªk

Design Objective and Procedure

Shell Language Constructs

Command Line Evaluation

File Name Expansion (Wildcard ¸U¥Î¦r¤¸)

ÅܼÆ

Undefined Variable

Variables Type

¨Ï¥ÎªÌÅܼƩR¦W³W«h

Variable Naming Convention

Variable Assignment

Variable Substitution

Uninitialized Variables - Default Values

POSIX Extension of Parameter Expansion

POSIX Extension: Variable Length

Arithmetic Expansion

Arithmetic Statement in KSH

String Concatenation

¨t²ÎÅܼơ]Àô¹ÒÅܼơ^

Env

POSIX Built-In Shell Environment Variables

Unset

Environment

°ßŪªº¨Ï¥ÎªÌÅܼÆ

¯S®íÅܼÆ

POSIX Built-In Shell Special Variables

¯S®íÅܼÆ

Accessing Shell Script Arguments - Position Parameters

Special Variables for Position Parameters $* and $@

Special Characters

Remove Meaning of Special Characters

Using Quote

When to use quote?

°õ¦æ¤@­Óµ{¦¡

Command Substitution

printf

°ßŪªº¨Ï¥ÎªÌÅܼÆ

Examples of Using Read

Examples of Using Read

¬yµ{±±¨î

Target of Condition Test

POSIX Defined Exit Status

Test

¬yµ{±±¨î - if

¬yµ{±±¨î - If ¤§¨Ò

¬yµ{±±¨î - If ¤§¨Ò

Nested if Structure

¬yµ{±±¨î - for °j°é

¬yµ{±±¨î - for

More Codes

¬yµ{±±¨î - while °j°é

¬yµ{±±¨î - break and continue

¬yµ{±±¨î - Case

Command Concatenation

Logical NOT AND and OR

File Testing Statement

String Comparison

Numerical Comparison

¬yµ{±±¨î - for °j°é

¬yµ{±±¨î - for

While and Until

ºâ¼Æ¹Bºâ (Arithmetic Expression)

¬yµ{±±¨î - break and continue

¬yµ{±±¨î - Case

Shift

Option Processing

Option Processing -getopts

Command Group

Function ( ¨ç¼Æ )

Function ( ¨ç¼Æ/¨ç¦¡ )

Function - Function Execution

Function - Passing Parameter and Variables

Function- Return Value

Function - Examples

Null Command

Debug

Eval

I/OÂà¦V»PºÞ½u (I/O Redirection and Pipe)

I/O Redirection

Avoid File From Truncated by I/O Redirection

Error Message Redirection

¨Ò of Here

Open a file for both input and output

File Descriptor Manipulation

Signal and Trap


Untitled Document
Links to other sources
Wed Apr 16 07:34:06 CST 2025 Untitled Document
Unix ¤§Àu¯ÊÂI
Wed Apr 16 07:34:06 CST 2025 Untitled Document
¾Ç²ß Unix Shell Programing ¤§¤èªk
Wed Apr 16 07:34:06 CST 2025 Untitled Document
Design Objective and Procedure
Objective: Minimize Total Time
 
If One Time Execution, Non-Repeatable - Make Coding convenient
If Data is not required - count coding time
If Data is required - count coding time AND data preparation time
 
If Repeatable Procedure - Make User Convenient
data preparation
easy to invoke
easy to memorize
easy to maintian
 
¨Ò:
put contact list in a "%name ... " markup format
Convert data into CSV file and text file for online query
download CSV file to PC and sync to PDA
 
¨Ò:
put trip list in a "%date ... " markup format
Convert data into HTML file and put into web server
Wed Apr 16 07:34:06 CST 2025 Untitled Document
Shell Language Constructs
Shell »y¨¥¤§¥D­n¤¸¯À
 
ÅÜ¼Æ (Variables) ¤Î¬ÛÃö°Ê§@
   
  • Åܼƫ¬ºA³£¬O¦r¦ê
  • ÅܼƥN´« (Variable substitution)
  • «ü¥O¥N´« (Command substitution)
  • ÀɦW¤§²£¥Í (File Name Generation)
 
¥Î©ó¬yµ{±±¨îªº»y¨¥¤¸¯À (Control Flow Primitives)
   
°j°é¦¡°õ¦æ (Repetitive Execution)
for
while
until
   
µL±ø¥óµ{¦¡¸õÅD (Unconditional transfer of control)
break
continue
exit
   
±ø¥ó¦¡°õ¦æ (Conditional Execution) |
if-then-else
case
 
¨ç¼Æ (Function)
   
  • »P¤@¯ë¶Ç²Î¨ç¼Æ¤j¦P¤p²§
  • ¨Ï¥Î¦ì¸mÅÜ¼Æ (Position Variables: $1, $2, ... $9, $*) ¶Ç»¼¨ç¼Æ¤§°Ñ¼Æ
  • ¯Ê¥F¹ï©óÅܼƤ§«OÅ@
Wed Apr 16 07:34:06 CST 2025 Untitled Document
Command Line Evaluation
   
¦b¶Ç²Îªºµ{¦¡»y¨¥¤¤¡Aµ{¦¡³]­p®v¦b°£¿ù®É¥u»Ý»EµJ¦bµ{¦¡¤º³¡ªº¿ù»~¡A «Ü¤Ö­n¦Ò¼{§@·~µ{¦¡¼h¯Åªº°ÝÃD¡A¨Ò¦p Memory Leak ³oºØ°ÝÃD¡C ¦ý¬OShell Programming «o¥²¶·¼ô±xShell ¦p¦ó°õ¦æ¤@­Ó«ü¥O¡A ¤~¯à¶¶§Q¤U¤@­Ó«ü¥O©Î³]­p¤@­Óscript¡C «Ü¦hªì¾ÇªÌ³£¥dÃö¦b¦¹¡C
Shell °õ¦æ«ü¥O¤§¨BÆJ
   
·í¨Ï¥ÎªÌ¤U¤@­Ó¦p¤Uªº«ü¥O®É¡AShell ¬O¦p¦ó°õ¦æ¥¦ªº?
#------------------------------------------------------
# Script: cmdexe1
#------------------------------------------------------ 
mycommand -l $option `echo $filelist` file*.???
¦Ó¤U­±¨â­Óscript ¦³¦ó¤£¦P? ­þ¤@­Ó¬O¤£¦w¥þªº?
# ---------------------------------------
# Script: cmdexe2 and cmdexe3
#---------------------------------------  
 [  $1  -eq 3 ] && echo "3"  || echo "not 3" 
# --------------------------------------- 
 [ "$1" -eq 3 ] && echo "3"  || echo "not 3" 
Shell ¦b¯u¥¿°õ¦æ«ü¥O¤§«e¡A¦³³\¦h«e¸m¤u§@¥²¶·¥ý°µ¡A §_«h«ü¥O«KµLªk°õ¦æ¡A ¨Ò¦p¡G©Ò­nªº°õ¦æÀɦb­þ¸Ì?ÅܼƸ̪º¤º®e¤S¬O¦p¦ó? ¦pªG©Ò»ÝÀɮתº¦WºÙ§t¦³¸U¥Î¦r¤¸¡A¤]»Ý¥ý®i¶}¨ú±o¥þ³¡ªº©Ò»ÝÀɮסC ©Ò¥H¤@­Ó Shell Programmer ¥²¶·ª¾¹D Shell ¬O¦p¦ó°õ¦æ¤@­Ó «ü¥Oªº¡A¥i¥HÁ×§K«Ü¦h°õ¦æ­±ªº¿ù»~¡A¤]¯à¸û¬°§Ö³tªº°£¿ù¡C Shell °õ¦æ«ü¥Oªº¨BÆJ¦p¤U:
Ū¶i¤@¾ã­Ó«ü¥O¡]¤@¦æ©Î¦h¦æ¡^
¶i¦æÅܼƥN´«¡A®i¶}¤§
¶i¦æ«ü¥O¥N´«¡A®i¶}¤§
³B²z I/O Âà¦V
±N°Ñ¼Æ¨Ì¾Ú¶¡¹j²Å¸¹Â_¶}¡A±o¨ì°Ñ¼Æ§Ç¦C
¦p¦³Àɮפ§¸U¥Î¦r¤¸¡A®i¶}¤§
¦p§t¦³'=', °õ¦æ¤§¡A±N'=' ¥ªÃ䤧ÅܼƥR­È
®Ú¾ÚÀô¹ÒÅÜ¼Æ PATH¤§°O¿ý¡A´`©Ò«ü©w¤§¸ô®|´M§ä$0(¨Ï¥ÎªÌ©Ò¤Uªº«ü¥O) ©Ò«ü©w¤§°õ¦æÀÉ
±N©Ò¦³°Ñ¼Æ¥æµ¹°õ¦æÀɨðõ¦æ¤§
   
®Ú¾Ú¥H¤W¨BÆJ¡A§Ú­Ì¨ÓÀ˵ø cmdexe2 ¤Î cmdexe3 ³o¨â­Ó sccript ¦b³Ì«á³Q°õ¦æ®É¬O¤°»ò¼Ë¤l¡A´N«Ü²M·¡°ÝÃD¥X¦b­þ¸Ì¡C
   
°²³]ÅÜ¼Æ $1 ¸Ì­±¬O 10, ¨S¦³°ÝÃD¡C
# ---------------------------------------
# Script: cmdexe2 and cmdexe3 
# --------------------------------------- 
[ 10 -eq 3 ] && echo "3" || echo "not 3" # ---------------------------------------
[ 10 -eq 3 ] && echo "3" || echo "not 3"
   
¦ý¦pªGÅÜ¼Æ $1 ¸Ì­±¬OªÅªº¡]©|¥¼©w¸q¡A©Î©w¸q¦¨ªÅ¦r¦ê)¡A°ÝÃD´N¥X¨Ó¤F¡C
# ---------------------------------------
# Script: cmdexe2 and cmdexe4 
# --------------------------------------- 
[ -eq 3 ] && echo "3" || echo "not 3" # ---------------------------------------
[ "" -eq 3 ] && echo "3" || echo "not 3"
¦b°õ¦æcmdexe2 ¤¤ªº test (´N¬O[ ]) «ü¥O®É¡AShell ¥æµ¹ test ¨â­Ó°Ñ¼Æ¡A "-eq" ¤Î "3"¡A µ²ªG°Ñ¼Æªº¼Æ¶q¤£¹ï¡A¦]¬°«ü¥O test ¦b°õ¦æ "-eq" ®É¥²»Ý¨ú±o¥ª¥k¨â­Ó ¼Æ­È¡A¤~¯à¹B§@¡C¤Ï¤§¡A ¦b°õ¦æcmdexe3 ¤¤ªº test «ü¥O®É¡AShell ¥æµ¹ test ¤T­Ó°Ñ¼Æ¡AªÅ¦r¦ê¡B"-eq" ¤Î "3"¡A¦p¦¹´N¨S¦³°ÝÃD¡C
Read Command Line or Structure Variable Substitution Command Substitution I/O Redirection IFS Parsing File Name Generation Variable Assignment Path Search Execute Command -> Wed Apr 16 07:34:07 CST 2025 Untitled Document
File Name Expansion (Wildcard ¸U¥Î¦r¤¸)
Wed Apr 16 07:34:07 CST 2025 Untitled Document
ÅܼÆ
Wed Apr 16 07:34:07 CST 2025 Untitled Document
Undefined Variable
ÅܼƤ§©w¸q
   
¤@­ÓÅܼƦbªì¦¸³Q¥R­È(Assignment)¡A´N¦Û°Ê³Q©w¸q¤F¡A¤£»Ý¨Æ¥ý«Å§i¡C
   
¤@­ÓÅܼƦb¥¼³Q©w¸q«e´N³Q¨Ï¥Î¡A¨ä­È¬°"ªÅ¦r¦ê"
   
¤@­ÓÅܼƥi¥H³Q¥R­È¬°ªÅ¦r¦ê¦p¤U:
 
a=
b=""
Wed Apr 16 07:34:07 CST 2025 Untitled Document
Variables Type
¸ê®Æ«¬ºA (Variable Type)
   
©Ò¦³Åܼƪº«¬ºA³£¬O¡u¦r¦ê¡v,¥]§t¤U¦C¦r¤¸¡G
  • ¤j¤p¼g­^¤å¦r¥À (upper and lower case letters)
  • ¼Æ¦r (digits)
  • newline ²Å¸¹
  • ªÅ¥Õ
  • tab
  • ¤@¨Ç¯S®í²Å¸¹
Wed Apr 16 07:34:07 CST 2025 Untitled Document
¨Ï¥ÎªÌÅܼƩR¦W³W«h
Wed Apr 16 07:34:07 CST 2025 Untitled Document
Variable Naming Convention
First character must be letter or underscore
Arbitrary sequence of letters, underscores, and digits
length depends on system implementation

Acceptable Names Unacceptable Names
Myfile2
_pay2me_
acct!
2ndfile
$owed
Wed Apr 16 07:34:08 CST 2025 Untitled Document
Variable Assignment
ÅܼƤ§¥R­È
   
¥Î"="¥i¬°ÅܼƥR­È¡A¨Ò¦p¡G
file=maillist
value=4+5
name=
FullName='John Smith'
formula='a<b'
name='Robert(Bob)'
list='read;practice;test'
list="read;practice;test"
ª`·N­n¥Î¥Î Quote ("" ©Î '') ±N¯S®í²Å¸¹«OÅ@°_¨Ó¡C

   
Åܼƥi¥R­È¬°ªÅ¦r¦ê¡G
 a= 
¤@­ÓÅܼƦb¥¼¥R­È«e¡A¤º§tªÅ¦r¦ê
Wed Apr 16 07:34:08 CST 2025 Untitled Document
Variable Substitution
ÅܼƥN´«
   
¤@­ÓÅܼƦb©w¸q®É¡AÅܼƦWºÙ«e­±¤£¶·¥['$'²Å¸¹¡A¦ý ¦b¨Ï¥Î¤@­ÓÅܼƮɡAÅܼƦWºÙ«e­±¶·¥[¤W'$'²Å¸¹¡G
name=Bob
echo $name
echo ${name}
echo $HOME
echo ${HOME}
¥²­n®É¡AÅܼƦWºÙ«e«á¥i¥[¤W¤j¬A©·'{}' ¥HÁ×§K²£¥Í²V²c¡A¨Ò¦p¡G
¨Ò °õ¦æµ²ªG
x=big
xfoot=small
a=$xfoot
b=${x}foot
c=${xfoot}
echo $a
echo $b
echo $c
small
bigfoot
small
Wed Apr 16 07:34:08 CST 2025 Untitled Document
Uninitialized Variables - Default Values
Åܼƪº¹w³]­È (Default Values in Variable Expansion)
   
·í¤@­Ó©|¥¼©w¸qªºÅܼƳQ¨Ï¥Î®É¡A­YµL¨ä¥L³]©w¡A¨ä­È±N±N¹w³]¬°ªÅ¦r¦ê¡A ¦³®É­Ô·|¹ïshell script ªº°õ¦æ²£¥Íªýê¡C POSIX ¼Ð·ÇÅýµ{¦¡³]­p®v¥i¥H¨Æ¥ý³]©w¹J¨ì¦¹ºØ±¡ªp®ÉªºÀ³¹ï¤è¦¡¡A ¥i¥H¨¾¤î³oºØ±¡§Îµo¥Í¡C¨âºØ®æ¦¡¦p¤U:
${var[-=?+]value}

${var:[-=?+]value}
«_¸¹(:)¦b³]©w¹w³]­È®Éªº·N¸q¦p¤U¡G
¦³«_¸¹ ¥¼©w¸q©Î§t¦³ªÅ¦r¦êªºÅܼƳ£·|IJµo³]©wªºÀ³¹ï
¨S¦³«_¸¹ ¶È¦³¥¼©w¸qªºÅܼƷ|IJµo³]©wªºÀ³¹ï
²Å¸¹ À³¹ï¤è¦¡ ¨Ò (Colon) ¨Ò (No Colon)
- ÅܼÆvar¦p¥¼©w¸q¡A«h¦^¶Ç¹w³]­Èdefault
  ÅÜ¼Æ var ºû«ù¤£ÅÜ
echo ${var:-default} echo ${var-default}
+ ÅܼÆvar¦p¦³©w¸q¡A«h¶Ç¦^ default ­È¡A
  var ¤§¤º®eºû«ù¤£ÅÜ
echo ${var:+default} echo ${var+default}
= ÅܼÆvar¦p¥¼©w¸q¡A«h¦^¶Ç¹w³]­Èdefault
  ¨Ã±NÅÜ¼Æ var ¥R­È¬° default
echo ${var:=default} echo ${var=default}
? ÅܼÆvar¦p¥¼©w¸q¡A«h¦L¥XMessage,
  ¦Ó Shell µ{§Ç«h²×¤î°õ¦æ
echo ${var:?message} echo ${var?message}

Wed Apr 16 07:34:08 CST 2025 Untitled Document
POSIX Extension of Parameter Expansion
¦r¦ê»PÀɮצWºÙªº½s¿è
   
Shell ¥»¨­ªº­pºâ¯à¤O«D±`¦³­­¡A¦r¦êªº³B²z³£¬O¾a¥~³¡«ü¥O¨Ó¶i¦æªº¡A¤×¨ä¬O ³Ì±`¹J¨£ªºÀɮצWºÙ³B²z¡A¨Ò¦p¡G cc ³o­Ó½s;¹¦b½sͤ@­Ó C ÀɮסA¦p example.c®É¡A·|²£¥Í¤¤¶¡ÀÉ®×'example.o'¡A©Ò¥H½s;¹ªº°õ¦æ¹Lµ{¤¤¡A ¶·±N'example.c'¥h±¼°ÆÀɦW±N¦r®Ú'example'±qÀɮצWºÙ¤¤©â¨ú¥X¨Ó¡A ¦A¶K¤W'.o'¦r¦ê¡A³Ì²×²£¥Í¤@­Ó'example.o'ªº¦r¦ê§@¬°¤¤¶¡ÀɦW¡C
   
¦b Shell ¤¤¡A±N¨â­Ó¦r¦êÂH¦b¤@°_(concatenation)¬O «Ü²³æªº¡A¨Ò¦p¡G
 $var1$var2     
#------------------------------------------- 
#±N $var1 ¤Î $var2 ®i¶}«á¡AÂH¦b¤@°_¡C
¦ý¬O¨ä¥Lªº¦r¦ê³B²z¡A´N«ÜµL¯à¬°¤O¤F¡A¥²¶·¨Ì¾a sed µ¥«ü¥O¨Ó³B²z¦r¦ê¡A¨Ò¦p:
a=`echo $var | sed 's/nccu/NCCU/'`      
#----------------------------------------------------------------
#±NÅܼÆ$varªº¤º®e®i¶}¥æµ¹sed±N¸Ì­±ªº¦r¦ênccu§ï¦¨NCCU¡AµM«á¥R­È©óÅܼÆ$a 

   
¦Ó«Ü¦hUnixª©¥»³£·|´£¨Ñ¤@­Ó«Ü¦³¥Îªº«ü¥O 'basename' ¥i¥Î¨Ó±NÀɮצWºÙ¥h±¼°ÆÀɦW¡A¦p¤U¡G
filename=example.c
rootname=`basename $filename .c`       
#----------------------------------------------------------------------
±N$filename ®i¶}¡A¥h±¼'.c' °ÆÀɦW¡AµM«á±N¦r¦ê'example'¥R­È©óÅܼÆ$rootname¤¤ 
   
»P basename Ãþ¦üªº¡A'dirname' ¥i¥Î¨Ó±NÀɮצWºÙ¤¤ªºÀɦW¥h°£¦Ó¯d¤U¥Ø¿ý¡A¦p¤U¡G
filename=/a/b/c/example.c
pathname=`dirname $filename`      
outfile=$pathname/a.out
 
#--------------------------------------------------------
±N$filename ®i¶}¡A¥h±¼'example.c'ÀɦW¡A
µM«á±N¯d¤Uªº¥Ø¿ý¤§¸ô®|¦WºÙ¥R­È©óÅÜ¼Æ $pathname¤¤¡A
³Ì«á¡A«á­±¥[¤W '/a.out' ¥R­È©óÅÜ¼Æ $outfile ¤¤

ÅܼƮi¶}¤§ POSIX ÂX¥R¥\¯à:¦r¦ê³B²z
   
basename ¤Î dirname ³£¬O¥~³¡«ü¥O¡A¨Ã«D¨C¤@­Óª©¥»ªºUnix ³£¦³´£¨Ñ¡A¦Ó¥B°õ¦æ¥~³¡«ü¥O¤]«D±`®ö¶O¨t²Î¸ê·½¡A¦]¦¹¡APOSIX ¼Ð·Ç¦bÅܼƮi¶}¤¤¥[¶i¤F¤@­Ó«D±`¦³¥Îªº¦r¦ê³B²z¥\¯à¡A§Æ±æ¯à¨ú¥N basename¡Adirname¥H¤Îsedªº¦r¦ê¨ú¥N¥\¯à¡C ³o­ÓÅܼƮi¶}¤§ÂX¥R¥\¯à«D±`¦n¥Î¡A¦p¯à¼ô²ß¡A¦bShell Programming ¤¤¯à¨ú¥N«Ü¦h¥~³¡«ü¥Oªº¨Ï¥Î¡C

»yªk ¨Ò
${var#pat}

¥ý®i¶}ÅÜ¼Æ $var¡A±N®i¶}ªº¦r¦ê±q«e­±¸òpat¤ñ¹ï¡A
­Y¦³²Å¦X¡A«h§R°£²Ä¤@­Ó²Å¦Xªº³¡¤À
path=/a/b/long.file.name 
echo ${path#/*/} 
#---------------------
 µ²ªG: b/long.file.name  
${var##pat}

¥ý®i¶}ÅÜ¼Æ $var ¡A±N®i¶}ªº¦r¦ê±q«e­±¸òpat¤ñ¹ï¡A
­Y¦³²Å¦X¡A«h§R°£©Ò¦³²Å¦Xªº³¡¤À
¥i¥H¥Î¨Ó¨ú¥N basename ¤¤¥h°£path name ªº¥\¯à
¤]¥i¥H¥Î¨ÓºI¨ú°ÆÀɦW
path=/a/b/long.file.name 
echo ${path##/*/} 
#---------------------
 µ²ªG¡G   long.file.name  
#---------------------  
#ºI¨ú°ÆÀɦW 
#---------------------  
file=myfile.txt 
echo ${file##*.} 
#c---------------------
  µ²ªG¡G   txt  
${var%pat}

¥ý®i¶}ÅÜ¼Æ $var ¡A±N®i¶}ªº¦r¦ê±q«á­±¸òpat¤ñ¹ï¡A
­Y¦³²Å¦X¡A«h§R°£²Ä¤@­Ó²Å¦Xªº³¡¤À
¥i¥H¥Î¨Ó¨ú¥N basename
#¥h±¼°ÆÀɦW (file extension)
path=/a/b/long.file.txt 
echo ${path%.*} 
#---------------------
µ²ªG¡G  /a/b/long.file 
${var%%pat}

¥ý®i¶}ÅÜ¼Æ $var ¡A±N®i¶}ªº¦r¦ê±q«á­±¸òpat¤ñ¹ï¡A
­Y¦³²Å¦X¡A«h§R°£©Ò¦³²Å¦Xªº³¡¤À
path=/a/b/long.file.name 
echo ${path%%.*} 
#---------------------
µ²ªG¡G  /a/b/long 
${var:offset:length}

Substring: ¥ý®i¶}ÅÜ¼Æ $var ¡A±N®i¶}ªº¦r¦ê±qoffset ¶}©l¬å¤U ªø«×¬°length ªºsubstring¡AÀY§À¥h°£¡C length ­Y±q¯Ê¡A«h¬å¨ì¦r¦êªº§ÀºÝ¡C offset ­Y¬°­t­È¡A«h±q¦r¦êªº§ÀºÝ¶}©l¦V«e¬å¡C ­t­È offset ¤§«e¥²¶·¥[¤WªÅ¥Õ¡A¥H§K¥X²{ ':-' ªº±¡ªp¡C length ­Y¬°­t­È¡A«h±q¦r¦êªº§ÀºÝ¶}©l§R°£(-2·N§Y±q§ÀºÝ¥h±¼2­Ó¦r¤¸)¡C
s=0123456789 
echo ${s:2:4}  
#--------------------- 
µ²ªG¡G 2345
#--------------------- 
echo ${s:2:-2}  
#--------------------- 
µ²ªG¡G 234567
#--------------------- 
length ¤§­È¬°-2 ·N§Y±q§ÀºÝ¥h±¼2­Ó¦r¤¸ 
${var/pattern/string}

 
¥ý®i¶}ÅÜ¼Æ $var¡A±N®i¶}ªº¦r¦ê¸ò'pattern'¤ñ¹ï¡A
­Y¦³²Å¦X¡A«h¥Î'string'¨ú¥N©Ò¦³²Å¦Xªº³¡¤À¡C
  ¥i¥H¥Î¨Ó¨ú¥Nsedªº¦r¦ê¨ú¥N¥\¯à¡Cµù¡G ÀɮצWºÙ¤§¸U¥Î¦r¤¸¦bpattern ¤¤¬O°_§@¥Îªº¡C
---------------------------------------
pattern ªº¶}ÀY¦pªG¦³¤U¦C²Å¸¹¡A«h¦³¯S®í·N¸q:
/ ¥u¦³²Ä¤@­Ó²Å¦Xªº pattern ³Q¨ú¥N ${var//pattern/string}
# pattern ¥u¦³¦b¦r¦ê¤§¶}ÀY¤~²Å¦X ${var/#pattern/string}
% pattern ¥u¦³¦b¦r¦ê¤§«áÀY¤~²Å¦X ${var/%pattern/string}
a=`echo $var | sed 's/nccu/NCCU/'` 
¥i¥H§ï¼g¬°
a=${var/nccu/NCCU}   
#---------------------------------  
a=`echo $var | sed 's/^nccu/NCCU/'` 
¥i¥H§ï¼g¬°
a=${var/#nccu/NCCU}   
#---------------------------------  
a=`echo $var | sed 's/nccu$/NCCU/'` 
¥i¥H§ï¼g¬°
a=${var/%nccu/NCCU}   
${!prefix*}
${!prefix@}

±N©Ò¦³ÅܼƤ¤¥H'prefix'¶}ÀYªºÅܼƦWºÙ¦C¥X¨Ó¡C
'*'©Ò¦³¦C¥XªºÅܼƬO¤@­Ó¦r¦ê
'@'¨C¤@­Ó¦C¥XªºÅܼƬO¤@­Ó¦r¦ê
#--------------------- 
a1=1
a2=2
a3=3
echo ${!a*}
  #--------------------- 
µ²ªG¡G a1 a2 a3
Wed Apr 16 07:34:08 CST 2025 Untitled Document
POSIX Extension: Variable Length
­pºâ¤@­Ó¦r¦êªºªø«× ( String-Length Operator:)
   
¦p­n­pºâ¤@­Ó¦r¦êªºªø«×¡APOSIX ¼Ð·Ç¤]¦bÅܼƮi¶}¤¤¡A´£¨Ñ¤F¤@­Ó²³æªº¿ìªk¡G
${#var}     #¶Ç¦^ÅÜ¼Æ $var ©Ò§t¦r¦êªºªø«×
¨Ò¦p:
$ x=supercalifragilisticexpialidocious 
$ echo There are ${#x} characters in $x

#-----------------------------------------------------------
µ²ªG: 
There are 34 characters in supercalifragilisticexpialidocious 
Wed Apr 16 07:34:09 CST 2025 Untitled Document
Arithmetic Expansion
   
­ì©l Bourne Shell ¬O¨S¦³¼Æ­È¹Bºâ¥\¯àªº¡A³£¬O­n¨Ì¾a¥~³¡«ü¥O¨Ó§¹¦¨¼Æ­È¹Bºâªº¡A «D±`¤£¤è«K¡A®Ä²v¤]®t¡A¦³Å²©ó¦¹¡APOSIX ¯S¦a¦b¨ä¼Ð·Ç¤¤¥[¤J¦³­­ªº¼Æ­È¹Bºâ¯à¤O¡C ¥Ñ©ó¤£¤è«K§ó°Ê¨ä¾ã­Ó»y¨¥ªº¬[ºc¡A¦]¦¹¼Æ­È¹Bºâªº¥\¯à¤]¬OÂæbÅܼƮi¶}ªº¹Lµ{¤¤¡C ¦Ó¹Bºâ¦¡ªº»yªk»P C »y¨¥ªº¹Bºâ¦¡¬Û¦P¡C¦Ó¼Æ¾Ç¹Bºâ¦¡ªº²Å¸¹¦p¤U¡G
$((...))
¨ä¤¤¡A¹ï©ó¯S®í²Å¸¹¦Ó¨¥¡AÂù¬A©·¦³¦p double quote ("" '") ¤@¯ë¡A°£¤F ¡u"¡v¤§¥~ªº¯S®í²Å¸¹¤£¶·¥t¥~¦A¥Î"\" ¨Óescape¡C
¹Bºâ¤l ·N¸q µ²¦X«ß¤è¦V
++ --
Increment and decrement,
prefix and postfix
L2R
+ - ! ~
Unary plus and minus;
logical and bitwise negation
R2L
* / %
Multiplication, division,
and remainder
L2R
+ -
Addition and subtraction
L2R
<< >>
Bit-shift left and right
L2R
< <= > >=
Comparisons
L2R
== !=
Equal and not equal
L2R
&
Bitwise AND
L2R
^
Bitwise Exclusive OR
L2R
|
Bitwise OR
L2R
&&
Logical AND
L2R
||
Logical OR
L2R
?:
Conditional expression
R2L
= += -= *=
/= %= &= ^=
<<= >>= |=
Assignment operators
R2L
   
¬A©·ªº§@¥Î»P C »y¨¥¤¤ªº¬A©·¬O¤@¼Ëªº¡C ¨Ò¦p¡G
¹Bºâ¦¡ µ²ªG
$((3 > 2)) 1
$(( (3 > 2) | | (4 <= 1))) 1
   
¦bÅÞ¿è¹Bºâ¤¤¡A«D¹sªº¼Æ­Èµ¥©ó¡u¯u¡v¡A¨Ò¦p¡G
$ echo $((3 && 4))      #Both 3 and 4 are "true" 
#-----------------------------------------
µ²ªG¡G  1 
   
¨Ò¡G
 $i=5
 $j=3
 $echo $((i+j)) 
  8
 $echo $((i++)) $i
  5 6
 $echo $((++i)) $i
  7 7
Wed Apr 16 07:34:09 CST 2025 Untitled Document
Arithmetic Statement in KSH
 
integer i=0
 
Use (( C-like expression )) in testing
 
Let C-like expression (no space or Tab is allowed)
 
'Let' can be omitted if variables are declared
Wed Apr 16 07:34:09 CST 2025 Untitled Document
String Concatenation
¦r¦êªº³s±µ(Concatenation)
   
¦b Shell ¤¤¡A¦r¦êªº³s±µ(Concatenation)¬O«D±`²³æªº¡A ±N©Ò¦³¦r¦êª½±µ³s¦b¤@°_´N¦æ¤F¡C
¨Ò µ²ªG
 
a=first
b=second
echo $a$b
echo ${a}and${b}
echo "$a and $b"
 
firstsecond
firstandsecond
first and second
Wed Apr 16 07:34:09 CST 2025 Untitled Document
¨t²ÎÅܼơ]Àô¹ÒÅܼơ^
Wed Apr 16 07:34:10 CST 2025 Untitled Document
Env
Wed Apr 16 07:34:10 CST 2025 Untitled Document
POSIX Built-In Shell Environment Variables
Wed Apr 16 07:34:10 CST 2025 Untitled Document
Unset
Wed Apr 16 07:34:10 CST 2025 Untitled Document
Environment
Åܼƪº¦³®Ä½d³ò¤ÎÀô¹ÒÅܼƤ§³]©w
 
Åܼƪº¦³®Ä½d³ò (Scope of Environment variable)
¨C¤@­Ó shell µ{§Ç¾Ö¦³¦Û¤vªºÅܼÆ
·í¤@­ÓÅܼƦb¤@­Ó µ{§Ç¤¤³Q©w¸q¤§«á¡A´N¤@ª½¦s¦bª½¨ìµ{§Çµ²§ô
¤@­ÓÅܼư£«D³Q«Å§i¬°Àô¹ÒÅܼÆ(§Q¥Îexport«ü¥O)¡A §_«h¤£·|¿ò¶Çµ¹¤lµ{§Ç¡A¦]¦¹¡A¤÷µ{§Ç»P¤lµ{§Ç¤§¶¡©¼¦¹¤£ª¾¹D¦U¦Û©Ò©w¸q ªºÅܼơC
¤lµ{§Ç©Ò©w¸qªºÀô¹ÒÅܼơA¤÷µ{§ÇµLªk¬Ý¨ì¡C
¤lµ{§Ç¥i¥H¨Ï¥Î¤÷µ{§Ç©Ò©w¸qªºÀô¹ÒÅܼƨçó§ï¤§¡A¦ý©Ò°µªº§ïÅÜ µLªk¼vÅT¨ì¤÷µ{§Ç¡A¥ç§Y¡A¤÷µ{§Ç¦b©I¥s¤lµ{§Ç«á¨äÀô¹ÒÅܼƺû«ù¤£ÅÜ¡A¤£¨ü ¤lµ{§Çªº¼vÅT¡C
 
Àô¹ÒÅܼƤ§³]©w
   
³\¦hÀ³¥Îµ{¦¡¨Ì¿à¬Y¨ÇÀô¹ÒÅܼơA¤~¯à¶¶§Q¹B§@¡A¨Ò¦p vi ´N¥²¶·¨Ì¾a $TERM ©Ò§iª¾ªº²×ºÝ¾÷«¬¸¹¡A¤~¯à±q /etc/termcap ©Î /etc/terminfo ¤¤¨ú±o©Ò»Ýªº´å¼Ð±±¨î°Ñ¼Æ¡A§_«h vi «KµLªk±±¨î´å¼Ð¤F¡C ¨Ï¥ÎªÌ¥²¶·±N $TERM ¨Æ¥ý©w¸q©ó .profile ¤¤¡AÅýlogin shell ¦b login µ{§Ç¤¤¦Û°Ê°õ¦æ '.profile'¨Ó¦Û°Ê³]©w $TERM¡A ¤~¯à»P¨Ï¥ÎªÌ¶i¦æÂù¦V·¾³q¡C§_«h¨Ï¥ÎªÌ¥i¯à³slogin: ªº²Å¸¹³£¬Ý¤£¨ì¤F¡C
   
°£¤F $TERM ¤§¥~¡AÁÙ¦³«Ü¦hÀô¹ÒÅܼÆ(¨Ò¦p $PATH) ³£¥²¶·¨Æ¥ý©w¸q©ó '.profile' ¤¤¡C ¤@­Ó¨t²ÎºÞ²zªÌ¦b·s¼W¥[¤@­Ó¨Ï¥ÎªÌ®É¡A¥²¶·°µªº¬ÛÃö ³]©w¤¤¤@©w­n°µªº´N¬O©ñ¤@­Ó¼Ð·Çª©¥»ªº '.profile' ¦b¨Ï¥ÎªÌªº $HOME ¥Ø¿ý¡A¨Ã¦b¨ä¤¤³]©w $HOME, $TERM, $PATH µ¥Àô¹ÒÅܼơA§_«h¨Ï¥ÎªÌ¤@¶i¨t²Î¡A³s'ls','cat' ³o¨Ç«ü¥O³£¤£¯à¥Î¡A¦]¬° login shell ®Ú¥»¤£ª¾¹D¦p¦ó´M§ä³o¨Ç¥~³¡«ü¥O¡C
   
¦pªG¨Ï¥ÎªÌ¤£·V¯}Ãa¤F'.profile' ³o­ÓÀɮסA¨º¤U¦¸ login ®É¡A ·|¾D¹J¨ì«Ü¤jªº³Â·Ð¡A ³ÌÃaªº±¡§Î¬O­n½Ð¨t²ÎºÞ²zªÌ¦A½Æ»s¤@¥÷¼Ð·Çªº '.profile' ¨ì $HOME ÀɮסC
   
¸Ü»¡¦^¨Ó¡A¤@­Ó±`¼g shell script ªº¤H¤£¥i¯à¤£¥h§ïÅÜÀô¹ÒÅܼơA ¨Ò¦p¡A¨Ï¥ÎªÌ±N©Ò¼gªº shell script ¶°¤¤¨ì $HOME/bin ¥h¡AµM«á ±N$HOME/bin ¥[¨ì $PATH Åܼƥh¡A¥H«K¦b¥ô·Nªº¥Ø¿ý¤U¨Ï¥Î³o¨Ç script¡C ¤@­Ó¤ñ¸û¦w¥þªº§@ªk¬O¡GºÉ¥i¯à±N©Ò­n°õ¦æªº³]©w©ñ¦b ¥t¤@­Ó shell script ¤¤¡A¨Ò '.myprofile'¡AµM«á¦b ­ì©l '.profile' ªº³Ì«á¤@¦æ¥[¤W³o»ò¤@¦æ code¡G
  
  • .myprofile
  • ¤W­±³o¦æcode ·|¦blogin shell °õ¦æ(½Ðª`·N³Ì«e­±ªº'.'¡A¨ä·N¬°¦bcurrent shell °õ¦æ)¡A °õ¦æ§¹²¦«á¡AÀô¹ÒÅܼƪº¤º®e ·|¤@ª½«O«ùµÛª½¨ì¨Ï¥ÎªÌlog out¡C¤U­±³o¨âºØ¤èªk³£¬OµL®Äªº¡A¦]¬° ¥¦­Ì³£¬O§Q¥Î¤lµ{§Ç¥h°õ¦æªº¡A¦Ó¤lµ{§Ç¬OµLªkÅܧó¤÷µ{§Ç¸ÌªºÀô¹Ò¡C
    # ---------------------------
    # µL®ÄªºÀô¹ÒÅܧó¤è¦¡
    # --------------------------- 
    sh .myprofile # ---------------------------
    .myprofile
    Wed Apr 16 07:34:10 CST 2025 Untitled Document
    °ßŪªº¨Ï¥ÎªÌÅܼÆ
    °ßŪªº¨Ï¥ÎªÌÅܼÆ
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    ¯S®íÅܼÆ
    Wed Apr 16 07:34:11 CST 2025 Untitled Document
    POSIX Built-In Shell Special Variables
    Wed Apr 16 07:34:11 CST 2025 Untitled Document
    ¯S®íÅܼÆ
    §Q¥Î¯S®íÅÜ¼Æ $$ ²£¥Í°ß¤@ªºÀɮצWºÙ
       
    Unix ¬O¤@­Ó¦h¤H¦h¤uªº¨t²Î¡AÃø§K·|¦bÀɮתº²£¥Í©Î§ó°Ê®É²£¥Í½Ä¬ð¡A¾É­P¸ê®Æªº³à¥¢©Î¿ù»~¡C³Ì±`¨£ªº¬O°õ¦æ¹Lµ{¤¤²£¥Íªº¼È¦sÀÉ¡A¨Ò¦p¥H¤Uªº swap script
    # -----------------
    # Script swap v1 
    # ----------------- 
    mv $1 tmp.o mv $2 $1 mv tmp.o $2
    ¦pªG¦³¨â­Ó session ¦b¦P¤@­Ó¥Ø¿ý¤U¦P®É°õ¦æ³o­Ó script , ¨º tmp.o ³o­Ó¤¤¶¡¼È¦sÀÉ´N·|¦]¬°¦WºÙ½Ä¬ð¦Ó³y¦¨¤j°ÝÃD¡A¾É­PÀɮתº¿ò¥¢¡C¦³®É­Ô¼È¦sÀÉ·|©ñ¦b¬Y¨Ç¯S®í¥Ø¿ý¡A¨Ò¦p¡G /tmp, ¨º°ÝÃD§ó¤j¡A¦]¬°¤£ºÞ¥ô¦ó¤H¦b­þ¤@­Ó¥Ø¿ý°õ¦æ¡A³£·|²£¥Í°ÝÃD¡G
    # -----------------
    # Script swap v2 
    # ----------------- 
    mv $1 /tmp/tmp.o mv $2 $1 mv /tmp/tmp.o $2
    ¬°¤FÁ×§K³oºØ°ÝÃD¡A¦b©R¦W®É­n¬°¨C¦¸°õ¦æ®É²£¥Íªº¤¤¶¡Àɮרú¤@­Ó¤£¦P ªº¦W¦r¡C¨ú¶Ã¼Æ¬O¤@­Ó¤èªk¡A±NÀɮצWºÙ¥[¤W®É¶¡ÂW°O¤]¬O¥t¤@­Ó¤èªk¡A ÁÙ¦³¤@­Ó§ó²³æªº¿ìªk¬O¥[¤Wµ{§Çªº§Ç¸¹¡C¤@­Óµ{§Çªº§Ç¸¹¥i±q $$ ³o­Ó¯S®íÅܼƨú±o¡C¦p¦¹¡A script swap ´N¥i§ï¼g¦¨ script v3 ³o­Ó¤ñ¸û¦w¥þªº script¡G
    # -----------------
    # Script swap v3 
    # ----------------- 
    mv $1 /tmp/tmp$$.o mv $2 $1 mv /tmp/tmp$$.o $2
    Wed Apr 16 07:34:11 CST 2025 Untitled Document
    Accessing Shell Script Arguments - Position Parameters
    Wed Apr 16 07:34:11 CST 2025 Untitled Document
    Special Variables for Position Parameters $* and $@
       
    ¨Ï¥ÎªÌ¦b¨Ï¥Î¤@­Ó«ü¥O®É¡A¦pªG¿é¤J¦ì¸m°Ñ¼Æ(Position Parameter)ªº¸ê®Æ§t¦³ªÅ¥Õ¡A¨º¦ì¸mÅܼÆ(Position Variable) ¥i¯à·|¨ú¨ì¿ù»~ªº¸ê°T¡A¨Ï¥ÎªÌ¦b¿é¤J¦ì¸m°Ñ¼Æ¸ê°T®É¡A¥²¶·¥Î¤Þ¸¹(" " ©Î ' ') «OÅ@°_¨Ó¡A¥H§K¿ù¶Ã¡C¦Ó¦b script ¤¤¨Ï¥Î $*, $@®É¤]¶·¥Î ¤Þ¸¹«OÅ@¡A§_«h§t¦³ªÅ¥Õªº°Ñ¼Æ·|³QÂ_¶}¡C ³o¨â­Ó¥Nªí¥þ³¡¦ì¸m°Ñ¼Æªº¦ì¸mÅܼƦ³¨Ç·L¤£¦P¡A ½Ð¬Ý¤U­±ªº»¡©úÁA¸Ñ¨ä°Ï§O¡C
    ¦ì¸mÅÜ¼Æ ·N¸q
    $*
    $1 $2 $3 $4 ...
    $@
    $1 $2 $3 $4 ...
    "$*"
    "$1 $2 $3 $4 ... "
    "$@"
    "$1" "$2" "$3" "$4" ...
     
    ¨Ò
    Script °õ¦æ¤Îµ²ªG
    # -------------------------------------
    # Script: myname
    # ------------------------------------- 
    echo "First Name: $1" echo "Last Name: $2"
    # -------------------------------------
    # Script: getname
    # ------------------------------------- 
    echo You have entered: $* echo '=== Passing $* ==='; myname $* echo '=== Passing $@ ==='; myname $@ echo '=== Passing "$*" ==='; myname "$*" echo '=== Passing "$@" ==='; myname "$@"
    %getname "Yao Nan" Lien
    You have entered: Yao Nan Lien
    === Passing $* ===
    First Name: Yao
    Last  Name: Nan
    === Passing $@ ===
    First Name: Yao
    Last  Name: Nan
    === Passing "$*" ===
    First Name: Yao Nan Lien
    Last  Name: 
    === Passing "$@" ===
    First Name: Yao Nan
    Last  Name: Lien
    
    Wed Apr 16 07:34:11 CST 2025 Untitled Document
    Special Characters
    ¯S®í²Å¸¹
       
    Shell §Q¥Î ASCII ½s½X¤¤ªº¬Y¨Ç²Å¸¹°µ¬°¯S®í¥Î³~ªº²Å¸¹¡G

    SP
    Tab
    NL
    =
    ;
    &
    )
    (
    >
    <
    |
    ^
    ;
    {
    }
    <
    >
    #
    *
    ?
    [
    ]
    $
    `
    \
    '
    "
    Wed Apr 16 07:34:12 CST 2025 Untitled Document
    Remove Meaning of Special Characters
    ¦b¦r¦ê¤¤¨Ï¥Î¯S®í²Å¸¹
       
    ¦r¦ê¤¤¦pªG§t¦³¯S®í²Å¸¹¡A¥²¶·«OÅ@(escape)°_¨Ó¡A§_«hShell ·|»~¸Ñ¦Ó±Ò°Ê¬ÛÀ³ªº¯S®í°Ê§@¡C¦@¦³¤TºØ«OÅ@¾÷¨î¡G
     \ 
       ' '  
       "  "  
     
    1. Backslash ('\')
    .
    Backslash '\' «OÅ@³æ¤@ªº²Å¸¹
    # ---------------------
    # example  single'\' 
    # ---------------------- 
    full=Joe\ Smith
    ¦b¤@¦æªº¥½§À±µ¤@­Ó'\'
    ¥i±N¦¸¦æ³s±µ°_¨Ó¡A
    ¦X¨Ö¦¨¦P¤@¦æ¡C
    # ---------------------
    # example '\' before NL
    # ---------------------- 
    echo this is a \ test
    ¨â­Ó³sÄòªº Backslash '\\'
    # ---------------------
    # example '\\'
    # ---------------------- 
    echo \\ is one backslash
     
    2. ³æ¤Þ¸¹(' ')
       
    ©Ò¦³¯S®í²Å¸¹§¡¥¢¥h¯S®í·N¸q¡C
     
    3. Âù¤Þ¸¹(" ")
       
    °£¤F¤U¦C¥|­Ó²Å¸¹¥~¡A¨ä¥Lªº¯S®í²Å¸¹§¡¥¢¥h¯S®í·N¸q¡C
    $
      `
      "
    \
    ¨Ò µ²ªG
    x=this is a string
    x="this is a string"
    echo $x
    echo "$x"
    echo '$x'
    echo "*"
    echo '*'
    
     
    
     is: not found
    this is a string
    this is a string
    $x
    *
    *
    
    Wed Apr 16 07:34:12 CST 2025 Untitled Document
    Using Quote
    Âù¤Þ¸¹(" ")¤§¨Ï¥Î
       
    ·í¦r¦ê¤¤¶¡§t¦³¶·®i¶}¤§ÅܼƩΫü¥O¥N´«®É¡A¥ÎÂù¤Þ¸¹¤ñ ³æ¤Þ¸¹¤è«K³\¦h¡A¨Ò¦p¡G
    # -------------------------------------
    # Script: singlequote
    # ------------------------------------- 
    name=Clinton echo 'My name is '$name' and today is '`date`
    # -------------------------------------
    # Script: doublequote
    # ------------------------------------- 
    name=Clinton echo "My name is $name and today is `date`"
    doublequote ¤ñ singlequote ¦n¥Î¦h¤F¡C¦Ó¥B¡A¦b¥H¤U³o­Ó script, ¥u¯à¥ÎÂù¤Þ¸¹¡A¦Ó¨S¦³Â²³æªº¿ìªk¨Ï¥Î³æ¤Þ¸¹¡C
    # -------------------------------------
    # Script: doublequote2
    # ------------------------------------- 
    name=Clinton output="My name is $name and today is `date`"
     
    §ó¦h Script ¹ê¨Ò
     
    Part1=One
    Part3=Three 
    echo '$Part1'$Part2"$Part3"
    #----------------------------
    µ²ªG¡G $Part1Three 
    
    Part1=One
    Part3=Three
    echo '$Part1"$Part2"$Part3'
    #----------------------------
    µ²ªG¡G $Part1"$Part2"$Part3 
    
    Part1=One
    Part3=Three
    echo "$Part1'$Part2'$Part3"
    #----------------------------
    µ²ªG¡G One''Three 
    
    # -------------------------------------
    # Script: replaceword v1
    # Usage: replaceword <filename>
    # -------------------------------------  
    OLD_WORD=abc NEW_WORD=xyz sed -e "s/$OLD_WORD/$NEW_WORD/g" < $1
    # -------------------------------------
    # Script: replaceword v2
    # Usage: replaceword <filename>
    # -------------------------------------  
    OLD_WORD=abc NEW_WORD=xyz cat $1 | sed -e "s/$OLD_WORD/$NEW_WORD/g"
    §A¯à¬Ý±oÀ´¤Ñ®Ñ¶Ü?
    date=`date`
    birthday="Tue Apr 28"
    echo $date | awk '/^'"$birthday"'/ {print $2, $3;}'
    
    Wed Apr 16 07:34:12 CST 2025 Untitled Document
    When to use quote?
    ¦ó®É¸Ó¥Î¤Þ¸¹?
       
    ·í¤@­Ó¦r¦ê¸Ì­±§t¦³ÅܼƮɡA¤j¤j´£°ª¤F¤£½T©w©Ê¡Aµ{¦¡³]­p®v ¨Ã¤£¤@©wª¾¹DÅܼƱN¨Ó·|¥R¤°»ò¼Ëªº­È¶i¨Ó¡A¥i¯à³y¦¨¦MÀI¡A¤U­± ªº shell script ´N«D±`¦MÀI¡G
    rm -rf /$var    #¦pªG $var ¬OªÅ¦r¦ê¡A·|¦³¤°»ò«áªG?
    
     
    'test' «ü¥Oªº³´¨À
    test "Clinton" = "Clinton" ¹ïªºscript
    test Clinton = Clinton ¹ïªºscript
    test "Bill Clinton" = "Bill Clinton" ¹ïªºscript
    test Bill Clinton = "Bill Clinton" ¿ùªºscript
    test "" = "Clinton" ¹ïªºscript
    test   = "Clinton" ¿ùªºscript

    ¿ùªº Script ¿ùªº Script ¹ïªºScript
    string=""
    test $string = Clinton
    
    string=""
    test '$string' = Clinton
    
    string=""
    test "$string" = Clinton
    

    ¿ùªº Script ¿ùªº Script ¹ïªº Script
    string="Bill Clinton"
    test $string = Clinton
    
    string="Bill Clinton"
    test '$string' = Clinton
    
    string="Bill Clinton"
    test "$string" = Clinton
    
    Wed Apr 16 07:34:12 CST 2025 Untitled Document
    °õ¦æ¤@­Óµ{¦¡
    Wed Apr 16 07:34:12 CST 2025 Untitled Document
    Command Substitution
    Command Substitution
       
    ¦bC »y¨¥¤º¡A°õ¦æ¤@­Ó¨ç¼Æ (Function)®É¡AFunction ©Ò¶Ç¦^ªº­È·|³Qµ{¦¡§ì¦íª½±µ¨Ï¥Î¡C ¦Ó¦b shell script ¤¤¡A¦U¥~³¡«ü¥O¥i¯à·|±N°õ¦æµ²ªG¿é¥X¨ì STDOUT¡A¦pªG­n±N«ü¥Oªº°õ¦æµ²ªG§ì¦^script¤º¹B¥Î®É¡A­n¦p¦ó°µ?
       
    Shell ¤º´£¨Ñ¼ÆºØ¤èªk:
    ¨Ò¡G command-substitution v1 °õ¦æµ²ªG
    str="Current directory is    `pwd` "
    echo $str
    

    str="Current directory is    $(pwd) "
    echo $str
    
    Current directory is /usr/lien
    
    µù: pwd ³o­Ó©R¥O¿é¥X"/usr/lien"¡A
    ¦Ó«á¾ã­Ó¦r¦ê¥N´À­ì¨Óªº`pwd`¡A
    ¶ñ¤J¦r¦ê¡A¥Î¨Ó³]©wstr ÅܼơC

     
    §ó¦h¨Ò¤l¡G
    today=`date`
    echo "Today is $today"
    
    echo "Today is `date`"
    
    echo "Today is $(date)"
    
    number=`expr $number + 1`
    
    number=$((number++))
    
    #----------------------
    # ¦L¥X¥Ø¿ý¤U©Ò¦³ÀÉ®×
    #---------------------- 
    for i in  `ls` 
    do
    echo "==== File Name: $i ===="
    cat $i
    done
    
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    printf
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    °ßŪªº¨Ï¥ÎªÌÅܼÆ
    °ßŪªº¨Ï¥ÎªÌÅܼÆ
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    Examples of Using Read
     
    §Q¥Î¡@'read' ±q /etc/passwd Àɮפ¤©â¨ú¸ê°T
    while IFS=: read user password uid realname homedir shell
    do
       echo user $user is $realname who prefers $shell 
    done < /etc/passwd
    
     
    cat /etc/passwd | 
    while IFS=: read user password uid realname homedir shell
    do
       echo user $user is $realname who prefers $shell 
    done 
    
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    Examples of Using Read
    ½Æ»s¾ã­Ó¥Ø¿ý
     
    1 §Q¥Î copy -r directory «ü¥O
     
    2. §Q¥Î¤U­±¨â­Ó script
    # -----------------------------------------------
    # Script cpdir v1
    # ----------------------------------------------- 
    find /a/b -type d -print | #Find all directories sed 's;/a/b/;/home/b/;' | #Change name sed 's/^/mkdir /' | #Insert mkdir command sh -x #Execute
    # -----------------------------------------------
    # Script cpdir v2
    # ----------------------------------------------- 
    find /a/b -type d -print | #Find all directories sed 's;/a/b/;/home/b/;' | #Change name while read newdir #Read new directory name do mkdir $newdir #Make new directory done
    Wed Apr 16 07:34:13 CST 2025 Untitled Document
    ¬yµ{±±¨î
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    Target of Condition Test
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    POSIX Defined Exit Status
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    Test
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    ¬yµ{±±¨î - if
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    ¬yµ{±±¨î - If ¤§¨Ò
    Wed Apr 16 07:34:14 CST 2025 Untitled Document
    ¬yµ{±±¨î - If ¤§¨Ò
    Wed Apr 16 07:34:15 CST 2025 Untitled Document
    Nested if Structure
       if 
          write stu1 < message
       then
          :
       else 
         if mail stu1 < message
         then 
    	echo "mail sent instead"
         else
    	echo "Could not sent instead"
         fi
       fi
    
    Wed Apr 16 07:34:15 CST 2025 Untitled Document
    ¬yµ{±±¨î - for °j°é
    ¬yµ{±±¨î - for °j°é
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    ¬yµ{±±¨î - for
    ¬yµ{±±¨î - for
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    More Codes
    for person in stu1 stu2 stu3
    do
       if 
          who | grep $person > /dev/null
       then
          write $person < message
       else 
          mail $person < message
       fi
    done
    
    Wed Apr 16 07:34:15 CST 2025 Untitled Document
    ¬yµ{±±¨î - while °j°é
    Wed Apr 16 07:34:15 CST 2025 Untitled Document
    ¬yµ{±±¨î - break and continue
    ¬yµ{±±¨î - break and continue
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    ¬yµ{±±¨î - Case
    ¬yµ{±±¨î - Case
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    Command Concatenation
       
    ¨â­Ó«ü¥O¥i¥H¥Î && ©Î || ¦³±ø¥óªº³s±µ°_¨Ó
     

     command1 && command2    
    =
     
    if command1
    then
       command2
    fi
    

    ¨Ò¡G mkdir newdir && cp thisfile newdir
    
     

     nbsp; command1 || command2   
    
    =
    if ! command1
    then
       command2
    fi
    

    ¨Ò¡G 
    rm myfile || echo "ERROR: cannot remove myfile"
    Wed Apr 16 07:34:16 CST 2025 Untitled Document
    Logical NOT AND and OR
       
    && ©Î || ¤]¥i¥H¥Î¦b ´ú¸Õ±ø¥ó¦¡ (test) ¤¤³s¦P§@¬° AND ¤Î OR ¡A³s¦P NOT §@¬° ÅÞ¿è¹Bºâªº¹Bºâ¤l (logical operator)¡C
     
    NOT
    if ! grep QQ fileX > dev/null
    then
      echo QQ not found in fileX
    fi 
    
    =
    if grep QQ fileX > /dev/null
    then
    :    # do nothing 
    else
      echo QQ not found in fileX
    fi
    
     
    AND
    if grep pattern1 myfile && grep pattern2 myfile
    then
       echo myfile contains both patterns
    fi 
    
     
    OR
    if grep pattern1 myfile || grep pattern2 myfile
    then
       echo Neither pattern1 nor  pattern2 found in myfile 
    fi
    
    Wed Apr 16 07:34:16 CST 2025 Untitled Document
    File Testing Statement
    ´ú¸ÕÀɮשʽè (Test File Status)

    test -r filename
    Exists and readable
    test -w filename
    Exists and writable
    test -x filename
    Exists and executable
    test -f filename
    Exists and regular
    test -d filename
    Exists and directory
    test -s filename
    Exists and nonzero size
    ¨Ò
    for file 
    do 
       if test -f $file
       then
          if  test -r $file
          then 
    	 if test -x $file
    	 then
    	    echo "File $file is 
                readable and executable"
    	 fi
          fi
       fi
    done
    
    for file 
    do 
       if [ -f $file ]
       then
          if  [ -r $file ]
          then 
    	 if [ -x $file ]
    	 then
    	    echo "File $file is 
                readable and executable"
    	 fi
          fi
       fi
    done
    
    for file 
    do 
       if [ -f $file -a -r $file  -a -x $file ]
       then 
           echo File $file is readable and executable
       fi
    done
    
     
    for file 
    do 
    [ -f $file -a -r $file  -a -x $file ] && echo File $file is readable and executable
    done
    
    Wed Apr 16 07:34:16 CST 2025 Untitled Document
    String Comparison
    test "s1" = "s2"
    Strings equal
    test "s1" != "s2"
    Strings not equal
    test -z "s1"
    Length of string zero
    test -n "s1"
    Length of string nonzero
    test "s1"
    Length of string nonzero
       
    • "test" ©Ò»Ýªº°Ñ¼Æ¤§¶¡¥HªÅ¥Õ¹j¶}¡C
    • ª`·N¥ÎÂù¤Þ¸¹(" ")«OÅ@°Ñ¼Æ¡A¨¾¤îªÅ¦r¦ê¯}Ãaµ{¦¡µ²ºc¡C
    ¨Ò Script 1 ¨Ò Script 2
    noset=""
    test -z  $noset
    test  "$noset" = a 
     test  $noset = a  
    q=`who -T | grep "^s8500" | cut -c10`
    if test "$q" = "+"
    then 
       write s8500 < note
    else
       mail s8500 < note
    fi
    
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    Numerical Comparison
    test "$n1" -eq "$n2"
    Equal
    test "$n1" -ne "$n2"
    Not equal
    test "$n1" -gt "$n2"
    Greater than
    test "$n1" -ge "$n2"
    Greater than or equal to
    test "$n1" -lt "$n2"
    Less than
    test "$n1" -le "$n2"
    Less than or equal to
     
    ¨Ò
    # -----------------------------------------------
    # countuser: count number of users on the system
    # ----------------------------------------------- 
    count=`who | wc -i` if [ "$count" -le "20" ] then echo "$count" users - light load else echo "$count" users - heavy load fi
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    ¬yµ{±±¨î - for °j°é
    ¬yµ{±±¨î - for °j°é
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    ¬yµ{±±¨î - for
    ¬yµ{±±¨î - for
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    While and Until
     
    Syntax
    while 
       command_lines
    do
       command_lines
    done
    
    until 
       command_lines
    do
       command_lines
    done
    

    'while' will test the condition to do the first iteration
    'until' will do the first iteration before test the condition
    Begin while marks loop start
    Control Evaluate return code from last command before do
    Middle execute all commands within body
    End done marks loop end, shell reads until end is found

     
    ¨Ò Script: Appends stdin to file outfile
    echo "
    Enter lines of input (end with ^D):
    "
    while read x
    do
       echo $x
    done >> outfile
    
    Wed Apr 16 07:34:17 CST 2025 Untitled Document
    ºâ¼Æ¹Bºâ (Arithmetic Expression)
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    ¬yµ{±±¨î - break and continue
    ¬yµ{±±¨î - break and continue
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    ¬yµ{±±¨î - Case
    ¬yµ{±±¨î - Case
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    Shift
    ¼u©Êªº¦ì¸m°Ñ¼Æ¼Æ¥Ø
       
    ¦pªG¨Ï¥ÎªÌµ¹ªº¦ì¸m°Ñ¼Æªº¼Æ¥Ø¨Ã«D©T©w¡A§Æ±æ¦³¼u©Ê¡A¥i¥Î¤U­± ªº¤è¦¡°µ¨ì¡G
    #-------------------------------------
    # Script: concatefile v2
    #------------------------------------- 
    for i     #­Y¨S¦³±Ô©ú¦ì¸m°Ñ¼Æ¡A¬Û·í©ó for i in $* 
    do
    echo "====== FILE NAME: $i  ========"
    cat -n $i   # option -n ·|±NŪ¤Jªº¸ê®Æ¥[¤W line number
    done > outfile
    
       
    ·í¨Ï¥ÎªÌµ¹ªº¦ì¸m°Ñ¼Æ¤¤¦³²§Ãþ®É¡A´N¥²¶·¨Ì¾a shift ¨Ó°µ¨ì¡C¨Ò¦p
    ¥\¯à
    ±N¼Æ­ÓÀÉ®×±µ¦b¬Y­ÓÀɮפ§«á
    ¥Îªk
    append mainfile file1 file2 file3 ...
    Script
    XX
    # -----------------------------------------------
    # Script: append
    # ----------------------------------------------- 
    outfile=$1 shift for i do echo ===== FILE: $i ===== cat $i done >>$outfile
    Wed Apr 16 07:34:18 CST 2025 Untitled Document
    Option Processing
    Option ªº³B²z
       
    ¤@­Ó¥\¯à±j¤jªº«ü¥O³q±`·|¦³³\¦hoption Åý¨Ï¥ÎªÌ¿ï¾Ü¡A§_«h«K­n»s³y¥X«Ü¦h ¤£¦Pª©¥»ªº«ü¥O¡A¤j¤jªº¼W¥[ºûÅ@ªº¦¨¥»¤Î¨Ï¥ÎªÌªº°O¾Ð­t¾á¡C¦Ó option ¥i¯à «D±`ÆF¬¡¦Ó¦³¤£¦Pªº²Õ¦X¡A¦ý³B²z°_¨Ó«K«D±`³Â·Ð¡C¥H¤Uªº¨Ò¤l§Q¥Î case/if µ¥¤è¦¡ µw¥Í¥Íªº³B²z¤@­Ó½ÆÂøªº option ²Õ¦X¡C
    file=  
    verbose=  
    quiet=  
    long=
    while [ $# -gt 0 ]   #Loop until no args left 
    do
       case $1 in   #Check first arg 
       -f) file=$2
          shift   #Shift off "-f" so that shift at end gets value in $2 
          ;;
       -v) verbose=true; quiet=  ;;
       -q) quiet=true; verbose=  ;;
       -l) long=true             ;;
       --) shift   #By convention, - - ends options 
           break
           ;;
       -*) echo $0: $1: unrecognized option >&2 ;;
        *) break  ;;    # Nonoption argument, break while loop 
       esac
       shift   #Set up for next iteration 
    done
    
       
    µù¡G·íoption ³B²z§¹¤§«á¡A³Ñ¤Uªº argument ÁÙ¯dµÛµ¥«áÄò¨Ï¥Î¡C
    Wed Apr 16 07:34:19 CST 2025 Untitled Document
    Option Processing -getopts
       
    ¬°¤F¸`¬Ù³B²z Option ªº³Â·Ð¡APOSIX ±Noption ªº®æ¦¡²Î¤@°µ¤F³W½d ¨Ã´£¨Ñ¤F¤@­Ó getopts ªº«ü¥O¡C¤j³¡¤ÀUnix ªº¨Ï¥ÎªÌ¹ï©ó¦b Shell ±±¨î¤U ¡A¦b«ü¥O«á­±¤U option ªº®æ¦¡¡AÀ³¸Ó³£¦³¤F¤@©wªºÁA¸Ñ¡A

    ¨C¤@­Óoption ³£¬O³æ¤@¦r¤¸
    ¼Æ­Óoption ¥i¥H¦X¦b¤@°_Åܦ¨¤@­Ó¦r¦ê
    ¨C¤@­Óoption ©Î¨C¤@²Õ¦X¦b¤@°_ªº option «e­±³£¦³­Ó "-" ¦r¤¸
    ¤@­Ó option «á­±¥i¥H±a¦³°Ñ¼Æ
    ¥Î "--" ¥Nªíoption ªºµ²§ô
    ¨ä¥L¦ì¸m°Ñ¼Æ±µ¦b option «á­±

    'getopts' «ü¥O¥i¨ó§U¤@­Ó shell script ­åªR option¡A´£¨Ñ¤£¤Ö¤è«K¡C µ{¦¡³]­p®v¥i¥Î¤@­Ó°j°é±N option ³v­ÓŪ¶i¨Ó¥æ…H 'getopts' ³B²z¡C 'getopts'³o­Ó«ü¥O¸ò¨ä¥L«ü¥O³Ì¤j¤£¦PÂI¬O¡A¥¦«O¦³¡u°O¾Ð¡v¡C¨ä¥L©Ò¦³ «ü¥O¨C¦¸°õ¦æ³£¬O¿W¥ßªº¡A°£¤F¸ò°ÊºA¸ê°T(¨Ò¦p®É¶¡)¦³Ãöªº«ü¥O¤§¥~¡A ¥u­n°õ¦æ±ø¥ó¬Û¦P¡A±o¨ìªºµ²ªG³£¥²©w¬Û¦P¡A¤£·|¸ò°õ¦æ¶¶§Ç¦³Ãö¡A¦ý 'getopts' ¦b¤@­Ó°j°é¤¤°õ¦æ®É¡A¨C¤@¦¸°õ¦æ³£¦³¤£¦Pªºµ²ªG¡A©Ò¥H¥¦ ¥i¥H°O¦í¤W¤@¦¸°õ¦æ¨ì­þ¤@­Ó option¡A³o¬O¸ò¨ä¥Lªº«ü¥O¤j¬°¤£¦P¤§³B¡C 'getopts' »Ý­n¨â­Ó°Ñ¼Æ¡G

    ²Ä¤@­Ó°Ñ¼Æ±Ô©ú©Ò¦³ªº option¡C¡]©Ò¦³option ªº¦r¤¸¦X¨Ö¦¨¤@­Ó¦r¦ê) option «á­±¦p»Ý­n°Ñ¼Æ¡A«h¦b¸Ó¦r¤¸«á­±¥[¤@­Ó«_¸¹(':')¡C¨Ò¦p¤U¨Ò¤¤ ªº 'f:vql'¡C
    ²Ä¤G­Ó°Ñ¼Æ¬O¤@­ÓÅܼơA¥Î¨Ó°O¿ý¨C¦¸­åªR¨Ï¥ÎªÌ©Òµ¹ªº option ªºµ²ªG

    'getopts' ¦b°õ¦æ®É¡A¬°²£¥Í¨â­ÓÅܼơA $OPTIND °O¿ý¥Ø«e­åªR¨ì²Ä´X­Ó option¡A¦Ó$OPTARG «h°O¿ýoption «á­±©Ò±aªº°Ñ¼Æ¡C¥H¤U¬O¤@­Ó¨Ï¥Î 'getopts'ªº¨Ò¤l:
     
    # -----------------------------------------------
    # getops v1
    # ----------------------------------------------- 
    file= verbose= quiet= long= while getopts f:vql opt do case $opt in #Check option letter f) file=$OPTARG ;; v) verbose=true; quiet= ;; q) quiet=true; verbose= ;; l) long=true ;; esac done shift $((OPTIND - 1)) # Remove options, leave arguments

       
    µù
    ¨Ï¥ÎªÌ©Òµ¹ªºoption ¤§«eªº'-' ²Å¸¹¦Û°Ê³Q°£±¼
    ¦p¦³'--'²Å¸¹¡A¤]¦Û°Ê³Q°£±¼
    ¦pªG¨Ï¥ÎªÌµ¹¤F¤@­Ó¤£¦s¦bªº²Å¸¹·íoption, 'getopts' ©Î¦Û°Ê²£¥Í¿ù»~°T®§¡C¦ýµ{¦¡³]­p®v¥i¿ï¾Ü¦Û¦æ³B²z¡A¥u­n ¦b'getopts' ªº²Ä¤@­Ó°Ñ¼Æ¤§³Ì«e­±¥[¤W«_¸¹(':')¡Agetopts ´N¤£·|²£¥Í¿ù»~°T®§¡A¦Ó·Ó±`­åªR¤§¡C
    # -----------------------------------------------
    # getopts v2 : ¦Û¦æ³B²z¿ù»~ªº option
    # ----------------------------------------------- 
    file= verbose= quiet= long= while getopts :f:vql opt do case $opt in Check option letter f) file=$OPTARG v) verbose=true; quiet= ;; q) quiet=true; verbose= ;; l) long=true; ;; '?') echo "$0: invalid option -$OPTARG" >&2 echo "Usage: $0 [-f file] [-vql] [files ...]" >&2 exit 1 ;; esac done shift $((OPTIND - 1)) Remove options, leave arguments

    Man Page of 'getopts'

    Wed Apr 16 07:34:19 CST 2025 Untitled Document
    Command Group
    ¸s²Õ«ü¥O
       
    ³]­p®v¥i¥H±N¼Æ­Ó«ü¥O²Õ¦¨¤@­Ó¸s²Õ¡A·í§@¤@­Ó«ü¥O¨Ó°µ¡A®æ¦¡¦p¤U¡G
     
      { command-list; }    #¦b current process °õ¦æ 
     
      ( command-list )    #¦b subshell °õ¦æ 
       
    ¸s²Õ«ü¥O¦b I/O Âà¦V®É«D±`¦³¥Î
     
    { date; ls } > filelist
    # -------------------------------
    { ...; ... } < inputfile
    # -------------------------------
    command | { ...; ... } 
    
       
    ³o¨âºØ¤è¦¡¹ï°õ¦æÀô¹Òªº¼vÅT¦U¦³¤£¦P :
    ¨Ò Script °õ¦æµ²ªG¹ïÀô¹Òªº¼vÅT
    { cd x; rm junk }
    
    the invoking shell stays in directory x
     
    ( cd x; rm junk )
    
    the invoking shell stays in original directory
    Wed Apr 16 07:34:19 CST 2025 Untitled Document
    Function ( ¨ç¼Æ )
    Wed Apr 16 07:34:19 CST 2025 Untitled Document
    Function ( ¨ç¼Æ/¨ç¦¡ )
    Wed Apr 16 07:34:19 CST 2025 Untitled Document
    Function - Function Execution
    Wed Apr 16 07:34:20 CST 2025 Untitled Document
    Function - Passing Parameter and Variables
    Wed Apr 16 07:34:20 CST 2025 Untitled Document
    Function- Return Value
    Wed Apr 16 07:34:20 CST 2025 Untitled Document
    Function - Examples
    Wed Apr 16 07:34:20 CST 2025 Untitled Document
    Null Command
    Null «ü¥O
       
    ¤@­ÓNull «ü¥O¡A¨S¥ô¦ó°Ê§@¡A¶Ç¦^ "ture"¡A®æ¦¡¦p¤U:
     
      : [parameters ...]
    

    ¨Ò Script 1 ¨Ò Script 2
    while :
    do
      commands
      if  ....
      then
         break
      fi
    done
    
    # -------------------------------
    #  Script: reverse-if 
    #          ±NIF «ü¥O¤§´ú¸Õ±ø¥óÄA­Ë
    # ------------------------------- 
    if command-list then : else commands fi done
    ³o­Ó script §â IF «ü¥Oªº´ú¸Õ±ø¥óÄA­Ë¡A
    ¦³®É­Ô¬°¤Fµ{¦¡ªºÅÞ¿è¤ñ¸û²M·¡¡A
    µ{¦¡³]­p®v·|³o¼Ë¼g µ{¦¡¡C
    Wed Apr 16 07:34:20 CST 2025 Untitled Document
    Debug
    µ{¦¡ªº°£¿ù¤u¨ã¡Gset
    Verbose
     
    set -v
    
    Execute with trace
     
    set -x
    
    Turn off -x ad -v
    set -
    
    Wed Apr 16 07:34:21 CST 2025 Untitled Document
    Eval
       
    ¦pªG­n§â¤@­ÓÅܼƸ̪º¤º®e®i¶}«á·í¦¨«ü¥O¨Ó°õ¦æ¡A
    'trap' ³o­Ó«ü¥O¥i¥H´£¨Ñ³o­Ó¥\¯à¡C
    ¿ù»~¤§¨Ò ¥¿½T¤§¨Ò
     
    y=CGI
    x='$y'
    echo $x
    
     
    y=CGI
    x='$y'
    eval echo $x   #execute echo $y 
    µ²ªG¡G $y µ²ªG¡G CGI
    Wed Apr 16 07:34:21 CST 2025 Untitled Document
    I/OÂà¦V»PºÞ½u (I/O Redirection and Pipe)
    Wed Apr 16 07:34:21 CST 2025 Untitled Document
    I/O Redirection
    Wed Apr 16 07:34:21 CST 2025 Untitled Document
    Avoid File From Truncated by I/O Redirection
    Wed Apr 16 07:34:21 CST 2025 Untitled Document
    Error Message Redirection
    ¿ù»~°T®§¤§Âà¦V
     
    ±N¿ù»~°T®§¦s¨ì¥t¤@­ÓÀÉ®×
    cat file1 file2 > today 2> err
    cat file1 file2 > today 2>> err
    
     
    ±N¥¿±`¿é¥X°T®§¤Î¿ù»~°T®§¦s¨ì¦P¤@­ÓÀÉ®×
    cat file1 file2 > day1 2>&1
    
       
    ¦b³]­p¤@­Ó¨ã¦³»P¨Ï¥ÎªÌ¤¬°Ê¯à¤OªºScript ®É¡A¸g±`·| ¹J¨£¤@­ÓÃøÃD: ¦pªG¨Ï¥ÎªÌ­n±N¿é¥X°T®§Âà¦V¨ì¤@­ÓÀÉ®×¥h¡A ¦ý¤´µM§Æ±æ¸ò Script «O«ù¹ï¸Ü¡C
    ¤U­±ªºScript ±N Script ¸ò¨Ï¥ÎªÌªº¹ï¸Ü¤ÎScript ªº¹Bºâµ²ªG³£¿é¥X¨ì STDOUT ¤W¡A¦pªG­n¦s¨ìÀɮפº¡A ¥u¯à¤@¨ÖÂà¦V¡AµLªk¤À¶}¡C
    echo Please input your name?
    read name
    echo Please input your phone number?
    read phone
    echo Please input your address?
    read address
    echo Name="$name | " Phone="$phone | " Address="$address"
    
    ³o¼Ëªº Script ¹ï¨Ï¥ÎªÌ«Ü¤£¤è«K¡A¦]¬°¦pªG±N¿é¥XÂà¦V¨ìÀÉ®× «á¡A¨Ï¥ÎªÌµLªk¸òScript ¹ï¸Ü¡C
       
    ¦pªG­n±N Script ¸ò¨Ï¥ÎªÌªº¹ï¸Ü¯d¦b¿Ã¹õ¤W¡A¤£·Q³Q¹Bºâ¿é¥X¤@¨Ö³QÂà¦V ¨ìÀɮסA¥i±N¤£·Q³QÂà¦Vªº°T®§³£°e¨ì /dev/tty ¥h¡A´N¤£·|³QÂà¦V¤F¡C ¤U­±¬O¤@­Ó¨Ò¤l:
    echo Please input your name? > /dev/tty
    read name
    echo Please input your phone number? > /dev/tty
    read phone
    echo Please input your address? > /dev/tty
    read address
    echo Name="$name | " Phone="$phone | " Address="$address"
    
    ¦p¦¹¡A¨âºØ¤£¦Pªº¿é¥X³£Åã¥Ü¦b¿Ã¹õ¤W¡A¦ý¥u¦³°e¨ìSTDOUT ªº·|³Q Âà¦V¡A¦Ó°e¨ì /dev/tty ªº°T®§¤´µM·|¯d¦b¿Ã¹õ¤W¡C ¨Ï¥ÎªÌ¤´µM ¥i¥H¸ò script Ä~Äò¹ï¸Ü¡C
    Wed Apr 16 07:34:22 CST 2025 Untitled Document
    ¨Ò of Here
    Here ¤§¨Ò
    # -----------------------------------------------
    # Send emails to top 10 disk space usage users 
    # ----------------------------------------------- 
    cd /home #Move to top of home directories du -s * | #Generate raw disk usage sort -nr | #Sort numerically, highest numbers first sed 10q | #Stop after first 10 lines while read amount name do mail -s "disk usage warning" $name << EOF Greetings. You are one of the top 10 consumers of disk space on the system. Your home directory uses $amount disk blocks. Please clean up unneeded files, as soon as possible. Thanks, Your friendly neighborhood system administrator. EOF done
    Wed Apr 16 07:34:22 CST 2025 Untitled Document
    Open a file for both input and output
    program <> file
     
    To open file for both reading and writing.
       
    It is up to program to be aware of this and take advantage of it
       
    not practically useful
       
    An undocumented feature in V7 Bourne shell
       
    Standardized in the 1992 POSIX standard,
       
    on many systems /bin/sh doesn't support it.
    Wed Apr 16 07:34:22 CST 2025 Untitled Document
    File Descriptor Manipulation
    §ó½ÆÂøªºI/OÂà¦V
     
    #------ ------------------------------------------------
    # sends standard output (file descriptor 1) to 'outfile' 
    # and standard error (file descriptor 2) to 'errfile'. 
    # ----------------------------------------------------- 
    myscript 1> ourfile 2> errfile
    # ---------------------------------------
    # sends standard output to 'outfile' 
    # and throw away standard error. 
    # --------------------------------------- 
    myscript 1> ourfile 2> /dev/null
     
    # ----------------------------------------------------------
    # sends both standard output and standard error to 'outfile'  
    # ---------------------------------------------------------- 
    myscript > ourfile 2>&1
    # -------------------------------------------------------
    # sends both standard output and standard error to  pipe 
    # ------------------------------------------------------- 
    myscript 2>&1 | nextscript
    Wed Apr 16 07:34:22 CST 2025 Untitled Document
    Signal and Trap
    Shell signal handling
       
    ¤@­ÓÀu¨qªºµ{§Ç³]­p®v¦b³]­p¤@­Óµ{¦¡®É¡A°£¤F­n¤@¨Ç ¨¾§b³]­p¥H¨¾¤î¨Ï¥ÎªÌªº¿ù»~¨Ï¥Î³y¦¨¨t²Îªº¨ü·l¤§¥~¡A ³q±`¦bµ{¦¡ªº³Ì«á­±¦³¤@¨Ç¦¬§À¤u§@»Ý­n°õ¦æ¡A ¨Ò¦p¡A±N©Ò²£¥Íªº¤¤¶¡ÀÉ®×§R°£¡A¥H§K¦û¥ÎªÅ¶¡¡A ¤]§â¥Ø¿ý§Ë±o«Ü²V¶Ã¡C
       
    ¥i¬O·í¤@­Óµ{§Ç±µ¨ì§@·~¨t²Î°e¨Óªº«H¸¹®É¡A ¤@¯ë¬O­n¥ß¨è²×¤îµ{§Ç¡A¥æÁÙ CPU ±±¨îÅv¡A ¦ý¬O¦pªG³o­Óµ{§Ç¦³¤@¨Ç¦¬§À¤u§@»Ý­n°õ¦æ¡A ª½±µ¦b¥b³~²×¤îµ{§Çªº°õ¦æ¦Ó¨S¦³°õ¦æ¦¬§À¤u§@¬O«Ü ÁV¿|ªº¤è¦¡¡A¦]¦¹¡@Shell ´£¨Ñ¤@­Ó¡@trap «ü¥O µ¹µ{¦¡³]­p®v¨Ï¥Î¡Atrap ·|ÄdºI§@·~¨t²Îªº«H¸¹¡A¥æ…H shell script¡A¦Óµ{¦¡³]­p®v´N¥i¥H¹w¥ý³]©w©Ò±ý ÄdºIªº«H¸¹¥H¤Î¬ÛÀ³ªº³B²z¤u§@¡C¦b¹ê»Ú°õ¦æ¡@script ®É­Y¹J¨ì§@·~¨t²Î°e¨Óªº«H¸¹¡A´N¯à±Ò°Ê¬ÛÃöªº¦¬§À¤u§@¡C
     
    ±`¥Îªº Unix «H¸¹
    Signal Name »¡©ú
    0
    EXIT
    Exit from the shell.
    1
    HUP
    A pseudosignal used by the shell, indicating that the standard output has hung up; sending this signal logs you out.
    2
    INT
    Sent by a <Del> keystroke; sends an interrupt to the current program.
    3
    QUIT
    Sent by a <Ctrl>\ keystroke; causes the current program to abort, leaving behind a core dump (for use in program debugging).
    9
    KILL
    Cannot be trapped or ignored; forces the receiving program to die.
    11
    SEGV
    Indicates that a segmentation violation (a memory fault within the UNIX system) has occurred. This signal cannot be trapped or ignored by a shell; it invariably terminates the receiving process and causes a core dump.
    15
    TERM
    Terminates the receiving program. (This signal should be used in preference to Signal 9, because the receiving program can catch it and carry out some shutdown operation, for example closing open files; Signal 9 forces the process to die immediately.)
       
    Trap ®æ¦¡¦p¤U:
    trap [ command-list ] signo ...
    
    ­YµL command-list¡A«h¨S¦³¹ïÀ³ªº°Ê§@¡Aµ¥©ó¬O©¿²¤©ÒÄdºIªº«H¸¹
       
    ¨Ò
    trap 'rm -f /tmp/myscript.$$; exit 1' 1 2 3 1
    ±µ¨ì«H¸¹ 1 2 3 15 ®É±N /tmp/myscript.$$ ÀÉ®×§R°£
    ¥²¶·¨Ï¥Î '' ±N«ü¥O«O¤ºªº¯S®í²Å¸¹Å@°_¨Ó¡AÁ×§K¤@¶}©l´N³Q®i¶}
    Shell ±N³o­Ó«ü¥OÀx¦s°_¨Ó¡Aµ¥signal ¨ì®É¡A¤~®³¥X¨Ó°õ¦æ
    °õ¦æ®É¡A¯S®í²Å¸¹¤w¸g¨S¦³''«OÅ@¡A·|³Qshell ®i¶}
    Wed Apr 16 07:34:22 CST 2025