¹ (6)

ҧʤԻ

ҡͧҧʤԻҧ SHELL (2) ʤԻԧҧչءСͺѹ˹ͧ
ҡ
$ sh			#call subshell
$ echo "Hello world"	#print "Hello world"
$ date			#print date information
$ pwd			#print current directory
$ exit			#exit from subshell
شҧö ѴʤԻ سҧ "ex1" ŧ öʹ "vi" "pico" ͺѴ سѧԸҹ "vi" "pico" ҹҡ͡͸Ժҹʹͧ͹
echo "Hello world"
date
pwd
ѹ֡ŧ "ex1" öҹͧԸ
  1. ҹ ѧ
  2. $ sh ex1
    
    Ըҹ "ex1" ͧ͹حҵöҹ (readable) öҹ
  3. çҡ
  4. $ ex1
    
    ѺԸ "ex1" ͧ͹حҵöҹ (executable) ʸҹ (Permission denied) ö˹͹حҵҹ
    $ chmod u+x ex1		: ͹حҵҹдѺ
    $ chmod g+x ex1		: ͹حҵҹдѺ
    $ chmod o+x ex1		: ͹حҵҹдѺؤ
    $ chmod +x ex1 
    
    $ chmod a+x ex1		: ԴҹءдѺ
    
ҡ˹͹حҵҹ ҹ ҡѧʸҹ (ex1: command not found) ʴҺ ŧҡ˹ ѧ ֧ͧкҧԧѺá ֧Һͧҡá ԸкҧԧѺáͧԸ
  1. Reletive Pathname ԸкҧԧҡáѨغѹ
    $ ./ex1
    
  2. Full Pathname кҧ óص á /home/user1
    $ /home/user1/ex1
    
ҹ¡ҹͧкá ͹ҧͧҡ ҡáͧ¡ /home/user1/shell Ѻ¡ ҡá ҧԧ֧ͧҡ кչءԸ˹öҹ ҡá˹к á١кʧǹͧк PATH (¡ search path) öǨͺá¡ҹкá
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
ҡѾҧ ҡҹкáѧóҧ ҡáк PATH /usr/local/bin, /bin, usr/bin /usr/X11R6/bin á١ͧ colon ":" á PATH ١˹èá ҹ ( "ls" á "/bin" "ftp" á "/usr/bin" ) Ѻ ex1 ͧͧҡ á /home/user1 ѧ֧ö ͧáŧ PATH
Ըáѧ
$ PATH=$PATH:/home/user1
$ export PATH
Ѻáͧá ($PATH) Ѻá (:/home/user1) ҡкá ($PATH) ͧͧ ö¡ ( ls) ҡͧҹáѨغѹ (current directory) ͧ
$ PATH=$PATH:.
$ export PATH
ҧáѨغѹ (:.) ŧ PATH ͧҡ ش (dot) "." ͧáѨغѹ "echo" Ǩá PATH º
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/user1:.

Ѻ˹áѨغѹ (:.) ŧ PATH ӹ֧֧ͧͧкѡʹͧҧ кԡǹ˭дѺ wheel root дѺѡк áѨغѹ (:.) PATH

˹͹حҵҹ ˹ serch path ºöҹͧкá

ѾҡҹٻѹѺҧ ¡ʤԻҹ ֧ʤԻѺ ͹ʤԻ¡Ѻ ѧҡءʤԻͧ͡ҡѺ "exit"

֧ exit ʤԻ º͹ exit ʤԻ ҡҹáԴҴԴ "0" (return code 0) ͡ ( exit Ţ return code 0) ҹá ҨԴҴԴ "0" ͡ "1") Ѻ return code ѧͺ͹Ѻ

¡Ѻ͡ҡѺŧʤԻ ʤԻҹ ͧŧѵѵ ءʤԻͧҹѺ ҹѺӡѴѧ͸Ժ͹ (SHELL (5)) Ѻöҹҡ ҡ export ͹ҧԸѧѺʤԻҹѨغѹ¡Ѻ ѧ͸Ժ

ԸʤԻҹҧ

ٻҹʤԻ 4 Ẻѧ
  1. $ script-name ʤԻͧ execute permission subshell
  2. $ sh script-name ʤԻͧ read permission subshell
  3. $ .script-name ʤԻ current shell
  4. $ exec scriptname ʤԻ current shell ѺѨغѹ

͹͸ԺٻҹʤԻ Ѻǹͧͧͧ ͧ" SHELL (5) ͹ ͧҡ͸Ժҧԧ֧ͧʺҧǹҡʹǡ ͧͧҡ

[Shell Process]
Ҿͧչء

ѺҹʤԻ 1 2 ö͸Ժѧٻ (A) ͡Թ ҹ (չء bash) ¡ Parent Shell ʤԻ ҹٻ 1-2 º͹¡ѺԴ ( sh) ¡ѺҹʤԻѺ Parent Shell ( exit) óҡʤԻͧҹ Parent Shell ͧ export ҡ Parent Shell ͹

Ѻҹ 3 ö͸Ժѧٻ (B) ԸҹʤԻ ʤԻҹѨغѹѹ ( Parent Shell) ҹʤԻ ¡Ѻ öҹ Parent Shell ѹ ͧ export ͹

Ѻҹ 4 ö͸Ժѧٻ (C) Ըҹ Parent Shell ١кʤԻ ͧ Parent Shell ١ ѧʤԻҹѺ͡Թѵѵ

ͧҹʤԻ "ex1" աͧԸ ͧǨͺѾ

кͧ

ҹٷԵԡ ǹ˭кͻѺٷԵ ҹҧҹŹöҹٷԵٻͧ ͻͧ §ҧѹ ͻͧź dash "-" ˹ ǹ ͧź˹
ҧͧ
$ ls -al /usr/bin 
"ls" ͧ "-al" "/usr/bin" "-al" ͻ "/usr/bin" ҧ֧áͧҺ ͧѺѹ öкҹҧҹŹҧª ͧѺ ѧ
$0 :ͧʤԻ ١¡ҹ
$1-$9 :Positional Parameter ͧ ҧ֧٧ش 9 ԸѴѺӹǹҡ 9 ѧ͸Ժ
$* : ءԧѹ "$1 $2 $3...$9"
$@ : ҧ֧ء ԧ˹ԧ
$# : ӹǹ
$- : ͻͧ
$$ : ŢШ (PID) ͧ
$! : ŢШ (PID) ͧҹѧҡش
$? : return code ͧ exit
ʴԸҹͧѺ ʴͧ ˹֧ ӹǹͧ
echo "Name of shell script is" $0
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
echo "No. of argument is" $#
echo "Arguments are" $*
echo "Arguments are" $@
ѹ֡ҧͧ "ex2" ҡҹöͧԸѧ
  1. $ sh ex2 a b c

  2. Ըͧ¹ͧҹ (executable) öҹ ѧա a, b c
  3. $ ex2 a b c

  4. Ըͧ¹ͧҹ (executable) ͹ ͧк search path ֧áè ѧա a, b c
Ѿҡҹѧ
Name of shell script is ex2
Parameter 1 is a
Parameter 2 is b
Parameter 3 is c
No. of argument is 3
Arguments are a b c
Arguments are a b c
ö͸Ժҧ ҧԧѺͧѧ

Ѻ $* $@ Ѿ͡ٻѹ "a b c" ҹѡɳҧҧͧٻ

͹˹ͧ

ö͹˹ͧ "shift" Positional Parameter ¹ҧ֧Ѵ "ex2" ѡɳѧ
echo "Name of shell script is" $0
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
echo "No. of argument is" $#
echo "Arguments are" $*
echo "Arguments are" $@

#Now we will shift arguments 
shift
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
ҹѧ
$ ex2 a b c d
ҹѾѧ
Name of shell script is ex2
Parameter 1 is a
Parameter 2 is b
Parameter 3 is c
No. of argument is 4
Arguments are a b c d
Arguments are a b c d
Parameter 1 is b
Parameter 2 is c
Parameter 3 is d
÷Ѵͧ "#" к÷Ѵ˵ (comment) ÷Ѵ ÷Ѵͧŧ ʴ֧ "shift" Ѻ ҧ Positional Parameter Ѻ͹ѧ "shift" ѧ ͹ shift
$1 ҧ֧˹ = "a"
$2 ҧ֧ͧ = "b"
$3 ҧ֧ = "c"
ѧ shift
$1 ҧ֧ͧ = "b"
$2 ҧ֧ = "c"
$3 ҧ֧ = "d"
ͧ "echo" ѺͧѺ

set command

öͧ "set" ͧҧ١˹ "set" ö¹ŧͧա ҡ ex2 "set" ͹شͧ
set PARA1 PARA2 PARA3 PARA4
echo "Name of shell script is" $0
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
echo "No. of argument is" $#
echo "Arguments are" $*
echo "Arguments are" $@

Now shift
shift
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
ͧҹ "a", "b", "c" "d" Ẻ
$ ex2 a b c d
Ѿѧ
Name of shell script is ex2
Parameter 1 is PARA1
Parameter 2 is PARA2
Parameter 3 is PARA3
No. of argument is 4
Arguments are PARA1 PARA2 PARA3 PARA4
Arguments are PARA1 PARA2 PARA3 PARA4
Parameter 1 is PARA2
Parameter 2 is PARA3
Parameter 3 is PARA4
ʹҹŹ ѹҡ set ᷹ ѧ
͹ shift
$1 ҧ֧˹ = "PARA1"
$2 ҧ֧ͧ = "PARA2"
$3 ҧ֧ = "PARA3"

ѧ shift
$1 ҧ֧ͧ = "PARA2"
$2 ҧ֧ = "PARA3"
$3 ҧ֧ = "PARA4"

͹

  1. ${varname:-word}
  2. ٻҹ ҡ varname (return) varname Ѻ ( null) word Ѻ ѧҧ
    $ STUDENT=${NAME:-"Peter"}
    $ echo $STUDENT
    Peter
    $ NAME="John"
    $ STUDENT=${NAME:-"Peter"}
    $ echo $STUDENT
    John
    
    ҡҧ͹á˹Ѻ NAME ѧҡٻ ֧ "Peter" Ѻ STUDENT echo $STUDENT ֧ "Peter" ˹ NAME "John" STUDENT ֧١˹ "John"
  3. ${varname:=word}
  4. ٻҡ varname ͧ varname Ѻ varname ҹͧ͹ ˹ word Ѻ varname word Ѻ ѧҧ
    $ STUDENT=${NAME:="Peter"}
    $ echo $STUDENT
    Peter
    $ echo $NAME
    Peter
    $ NAME="John"
    $ STUDENT=${NAME="Peter"}
    $ echo $STUDENT
    John
    
    ҡҧҡ NAME NAME ١˹ "Peter" "Peter" Ѻ STUDENT (ҧѺá˹Ѻ NAME, ѧ) ҡ NAME ( "John") ͧ NAME ͹ѹ
  5. ${varname:?message}
  6. Ը˹ ҡ varname ԴҴԴ ˹ message varname ͧ varname ѧҧ
    $ STUDENT=${NAME:?"Not assigned"}
    bash: NAME: Not assigned
    $ NAME="John"
    $ STUDENT=${NAME:?"Not assigend"}
    $ Echo $STUDENT
    John
    
    ҡҧ˹Ѻ NAME ԴҴ ( NAME) ԴҴ˹ ( "Not assigned") STUDENT NAME ѧ˹ ( null) ҡ NAME ١˹ "John" "John" Ѻ STUDENT
  7. ${varname:+word}
  8. Ը˹شҧҡԸҧ varname null word null ҧҡҧ ҡ varname ١˹
    $ STUDENT=${NAME:+1}
    $ echo $STUDENT
    
    $ NAME="John"
    $ STUDENT=${NAME:+"exists"}
    $ echo $STUDENT
    exists
    
    ҡҧ NAME STUDENT ( null) ˹ "John" Ѻ NAME ͧ "exist" Ѻ STUDENT

ٻ˹͹ҧ öǨͺѴѺ˹ͧ

˹ͻ ¡ͻ

ö˹ ͻѺ ͻ˹˹ѡɳͧѡʤԻ͡ ͻѡɳѡ (*,[]) Ը˹ͻͧö˹ҹ set ѧҧ˹ѡ öǨͺԴҴͧʤԻ§ "set -x" ÷Ѵáͧ "ex2"
set -x
set PARA1 PARA2 PARA3 PARA4
echo "Name of shell script is" $0
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
echo "No. of argument is" $#
echo "Arguments are" $*
echo "Arguments are" $@

Now shift
shift
echo "Parameter 1 is" $1
echo "Parameter 2 is" $2
echo "Parameter 3 is" $3
ҹ ex2 Ѿ
$ sh ex2
+ set PARA1 PARA2 PARA3 PARA4
+ echo Name of shell script is ex2
Name of shell script is ex2
+ echo Parameter 1 is PARA1
Parameter 1 is PARA1
+ echo Parameter 2 is PARA2
Parameter 2 is PARA2
+ echo Parameter 3 is PARA3
Parameter 3 is PARA3
+ echo No. of argument is 4
No. of argument is 4
+ echo Arguments are PARA1 PARA2 PARA3 PARA4
Arguments are PARA1 PARA2 PARA3 PARA4
+ echo Arguments are PARA1 PARA2 PARA3 PARA4
Arguments are PARA1 PARA2 PARA3 PARA4
+ shift
+ echo Parameter 1 is PARA2
Parameter 1 is PARA2
+ echo Parameter 2 is PARA3
Parameter 2 is PARA3
+ echo Parameter 3 is PARA4
Parameter 3 is PARA4
ҹ͡ҡѾҡ÷ѴͧʤԻ ѧ ҹ͹ԴѾ (÷Ѵ "+") ͧº ԸöǨͺԴҴͧʤԻ ͻ


HTML developed by Kaiwal Development Team (kaiwal@geocities.com)