:









          - 

                    "        "













                  

                     

                        /P 2.1










                        

                           SHELL














                           

                            1988















                        

         shell.  -
  ,      shell,
   .





























































1.  

        ,    UNIX,
 SHELL      -
 , ..     -
  .      
 , , ,   -  -
  , ,    -
,   .    ,  SHELL  
         -
 .      ,  
SHELL       -
,  , ,    -
 ,     .

        ,      
  .          -
,            
" "  .

2.   

            
      sh - -
   SHELL.

             
     .   ,
         ,
   ,      ,  
 ,     
.      ,  
   SHELL,     
      
 .  ,      
    ,    ,
      
 .

                 .
    -    -
      .   
     :  
,        -
.        
     .    -
,  /        
.

       ,    UNIX, -
        -
 .         ,
           


                           - 3 -










(..        
     ).   , -
     ,      
     -
.          (),  
       -
     .

2.1.   

             ,
 .     -
 ;            
 .  ,

                             who

   ,        
,   .  

                            ls -l

    .   -l -
    ls,     ,
     .

2.2.   

      ,   ,   
SHELL           .
      ,  
        
    ,        
.  , :

                       cc program.c &

      program.c.   
&,   ,   , 
         
  .      
       ,    
     .

2.3.    

         SHELL        
      ,       
.  ,   :

                          ls -l *.c

      ls        


                           - 4 -










  ,     .c.  *
   (..   
SHELL),     (   ).
      -
:

*           (  
       );

?         ;

[...]         
       ;    ,    ,
                
        .

     , 

                           [-]*

     ,  -
    ,  

                      /usr/nata/test/?

            
/usr/nata/test.       , -
  ,       
  .

                
    ,     . 
         .  ,


                    echo /usr/nata/*/core

       core  -
   /usr/nata ( echo  -
  ,        ,
  ).

         ,      ,   
  .    '.'     
  .   :

                           echo *

       , -
    ,  :

                           echo .*



                           - 5 -










  ,   .     -
     "."  "..", 
       -
.

2.4.     

        SHELL        
(       .2 ).
  ,        ,
      ( \ -
      ,      \
).  , :

                          echo \?

    '?',  :

                           echo \\

    \.       
    ,    
 \n .

      \       -
  .        -
 ,     -
.      
 . , :

                       echo '****'



                          ****


        \n,   , 
  . ,   
  .

            -
      .

2.5.    /

              
  .     -
      .   
        . ,
  :




                           - 6 -










                       ls -l > filels

 "> filels"     -
         ls 
 (    filels).      -
 ,    .  
,        -
,  .

          ,    
    ,   >> 
 >>>>. :

                        ls >>>> filels

  ,    ,   -
  .

          -
      (   -
  ).          <&lt;.
, :

                        wc <&lt; filels

    filels.

          ,  
   /.

>&gt; 
           ,
             , 
       .

>&gt;>&gt; 
          .
        ,   .

<&lt; 
           .

<&lt;<&lt; 
        (.  4.8).   
             -
           ,      
     ,       .
             
     ,      -
         .

>&gt;& 
     ,        ,


                           - 7 -










         .

<&lt;& 
             
     .

<&lt;&-       .

>&gt;&-       .

2.6.   ,   

     ,       
,      
     .   ,
   :

                           ls | wc

        -
.     ,     
"|",  .        
  :

                     ls >&gt; filels
                     wc <&lt; filels
                     rm filels

  ,     -
        .     
           ,
    .   -
 -   ,     
-   ,  
  .

       , ,   
    ,    -  
     .   -
      grep, ,  -
    ,  , 
          -
  .  , :

                       ls | grep old

      ,  
      old.    
     .   ,  -
:

                   ls | grep bak | wc -l



                           - 8 -










         -
  ,    -
 bak.

2.7.  

           -
  ,      ,  
.

           
          =, 
     ,     -
  ,      .  
     $  -
     ( 
,          
'...'   $   \).

     :

USER=nata HOME=/usr/nata
        USER  HOME;

null=
         ;

echo $USER
                
     ( -  nata).

            -
.  ,   :

           b=/usr/nata/bin
           mv pgm $b

 pgm        -
 /usr/nata/bin.

              
:

           ${_}

          
 ,       .
,     :

          =
          =
          echo  ${} $



                           - 9 -










  :

           


       SHELL     ,
       (-
   ?,    -
   ):

?          -      
       .   ,  -
       ,      
     .     ,    
     ,     .

#      .

$      .     
         ,      
              
     . :

                ps a >&gt;/tmp/ps$$
                . . .
                rm /tmp/ps$$


!      ,    -
     .

-      ( )   sh
      -x  -v.

         SHELL,  
         
:

MAIL           
        ,      
       .         
         ,   
      you have mail (  ).     -
              
       .profile.

      :

                     MAIL=/usr/mail/nata


HOME      cd  .   
      ,             /,


                           - 10 -










         e.    -
             cd.  ,
      :

                      cd /usr/nata/bin


         /usr/nata/bin.  :

                          cat show


           show   -
        /usr/nata/bin.  cd   -
       :

                          cd $HOME


             -
         .profile.

PATH         ,
            .    
       PATH    ,    
        :  , -
       /bin   /usr/bin.     
         PATH   -
        :.  :

                   PATH= :/usr/nata/bin:/bin:/usr/bin


      ,         
        :   ( 
            :),  /usr/nata/bin,  /bin   
      /usr/bin.  ,   
          ,      
      ,        .   
          /  (..    
          ),      
      .           
        .

PS1     (      
       - $).

PS2    ,   
      (  >&gt;).

IFS    -,   
      .



                           - 11 -










TERM      .

2.8.   

        ,     -
    ,     $.
    :

                          PS1=:

          
:.

            -
  ,        
e     >&gt;.  , -
  ,      
    PS2.

, :

                      PS2=:

     :.

2.9.   

            
  :

                     { _; }
                      (_)

    _   .
   -  _  ,  
  .

     , :

                       (cd x; rm junk)

  rm junk   x,    -
 ,  

                     { cd x; rm junk; }

     ,      x  
.

2.10.   

      ,    -  -
   .  ,  


                           - 12 -










,        
  ,  .     -
  ,        
  .  , 
  (    ),
      .      
      
      ,  
,         
 /.

               
  :

:             ,    
        .     -
       .

. 
          .   
     ,   ,  -
      PATH.

cd []
        .     
       ,        
       HOME.       cd
       chdir.

eval [ ...]
        ,    ,  
     eval       
        .

exec [ ...]
           -
       .     
      /.

exit [N]
          .   
         N.      N  , 
               
     .

export [ ...]
          
       .     ,
        .

login [ ...]
            
      (  exec login  ...).


                           - 13 -










newgrp [ ...]
             
     (  "exec newgrp  ...").

read  ...
              .
         ...  -
       ,      .   
              , 
     ,    ,     
      .

readonly [ ...]
          -
     .        ,   
     ,   readonly (  
     ).

set [-ekntuvx [ ...]]
          SHELL.
        :

      -e       -
                -
           .

      -k       
            .

      -n      .

      -t          ,  
               .

      -u       
            .

      -v      .

      -x              
          .

      -     -v  -x.

              -.
        set  
       "1, 2, ...".     
     ,       -
     .

shift
       "2, 3, ..."  -
       "1, 2, ..." .


                           - 14 -










times
      ,     
      .

trap [] [N ...]
            -
      .

umask [ddd]
           
           -
        ddd.   '0'  -
           .  
        ,    
     chmod.         ,  
         rw.    
     ,      .  -
     ,  umask 000    ,    
           
          .

wait [N]
            (N)  
          ?.

3.   

                 
,    .   , -
        
  SHELL.       (-
 ,  , )    
.

           
.     ,   
 SHELL - sh,   ,    -
  .    -      (-
    ).    -
,     " ".

                  
,  ,  ,   -
.         
      ,  -
     sh.

     , :

                     sh file []

        file.
         ,  


                           - 15 -










        "1",
"2"  ..

     ,   wg  :

                        who | grep $1

 

                         sh wg nata

  

                       who | grep nata


      ,       -
  ,    ( -
).  ,   ,    -
      .  
       -
 .      chmod.
,    :

                         chmod + wg

 wg    .

           
 :

-        .

-         -  -
       ,   -, 
       ,       
     .         -
      ,     ,      
     .

-        -
        "0", "1", "2",  ..

-        "#" 
      .

-          (    )
         *.   ..   $*
        ,      
     .

     ,    show  :



                           - 16 -










           echo '
             $0 =' $0 '
            $1 =' $1 '
            $2 =' $2 '
             =' $# '
            $* =' $*

    :

                     show  

   :

             $0 = show
            $1 = 
            $2 = 
             = 2
            $* =  


3.1.  
    SHELL    -
,        -
.       #    -
    .     , -
   #,   ,  
    .

     :

     #         
     ls    #    
           #  ls (  )

 ,   #     ,
       -   ,  
   csh.

3.2.   test

      test,          
SHELL,      .   -
        
          ,    
      .

     ,   

                        test -f file

 ,   file ,      -  
 .



                           - 17 -










          
 :

test s
     ,   s    ;

test -f file
     ,   ;

test -r file
     ,    ;

test -w file
     ,     ;

test -d file
     ,    .

3.3.   for

       SHELL    
  -        
 .           
 for,    :

       for  [in 1 2 ...]
          do _
          done

 _ -        -
  ,   ;  -
   .    do  done
    ,     -
      .  -    -
      SHELL,           
_         1
2  ....    in 1 2 ... -
,            
  (..        in  $*).  
     tel,    -
  /usr/lib/pfone,   :

             ...
              ..   224 01 01
              ..   123 07 07
               ..   224 44 94
             ...


       tel:

          for i
          do grep $i usr/lib/pfone; done


                           - 18 -










        :

                         tel 

        /usr/lib/pfone,  
   ,  :

                      tel  

    ,    
 ,    ,   
.

             for  
  create:

                     for i do >&gt; $i; done

  

                      create alpha beta

     alpha  beta.

3.4.   case

                 add,
  :

     case $# in
          1) cat >&gt;>&gt; $1;;
          2) cat >&gt;>&gt; $2 <&lt; $1;;
          *) echo ': add [] ';;
     esac

     , :

                          add file

 $#   '1',     cat  
          file.
:

                       add file1 file2

   file1      file2.   
  ,    add,   1 
 2,    ": add  []".

       case:





                           - 19 -










    case  in
     [[|] ...) _;;]
     ...
     [[|] ...) _[;;]]
    esac


          
  .    -
    _,  , -
    .    *  
,          .    
_  (  )  -
  ;;.

            -
      .      
,      *,      -
 :

         case $# in
              *) ...;;
              *) ...;;
         esac


      case      -
 .     -
  cc:

    for i
     do case $i in
       -[ocs]) ...;;
           -*) echo '  $i';;
          *.c) lib/c0 $i ...;;
            *) echo '  $i';;
        esac
    done


           -   _
    ,      
case         |.   ,
:

         case $i in
            -) echo $i
            -y) echo $i
         esac

    :




                           - 20 -










         case $i in
            -|-y) echo $i
         esac


           
     , 
  :

         case $i in
            ?) ...

   ?.

3.5.   while  until

      while        ,
      ,   -
     .

        while:

           while __1
           [do __2]
           done


            __1.
  while       
   :       ,  
__2   ,  -  
.

     ,    :

           while test $1
           do ...
              shift
           done



           for i
           do ...
           done

   .

      shift      
"2, 3, ..."   "1, 2, ..." ; 
 "1" .




                           - 21 -










           -
  until:

           until __1
           [do __2]
           done

   while  until      ,
         __1 
   .

3.6.   break  continue

      break  continue    
for, while  until.

      break   ,    
  .          -
,     , -
   %:

    for i
    do
    case $i in
      %) break;;
      *) echo $i;;
    esac
    done


      continue     -
 .      , 
   ,    :

     for i
     do
       case $i in
            [--]*) echo $i;;
            [A-Za-z]*) echo $i;;
                    *) continue;;
       esac
     done


3.7.   if

       :

            if  __1
            then __2
            [else __3]
            fi



                           - 22 -










               
__1:              ,   
__2,  - __3.

      if        
test, ,    :

           if test -f $1
           then  echo " $1 "
           else  echo " $1  "
           fi


         , -
:

            if ...
            then    ...
            else    ...
                    if ...
                    then    ...
                    else    if ...
                            ...
                            fi
                    fi
            fi

   :

            if ...
            then    ...
            elif    ...
            then    ...
            elif    ...
            ...
            fi


     

           if 1
           then  2
           fi

    :

                    1 && 2

 

                    1 || 2

2     ,   1  


                           - 23 -










   .     
     .

3.8.   

       tel,   ,  
  grep    /usr/lib/pfone.  -
        
.         .  
 :

         for i
         do grep $i <&lt;<&lt;!
            ...
             ..   224 01 01
             ..   123 07 07
              ..   224 44 94
            ...
         !
         done

,      <&lt;<&lt;!    !,  
  grep         .
- !     :  ,
      , 
  <&lt;<&lt;.

         <&lt;<&lt;      
     \,      
 e,        -
.

     ,   edg   :

           ed $3 <&lt;<&lt; %
           g/$1/s//$2/g
           w
           %

 :

                  edg 1 2 file

  ,     -
   :

           ed file <&lt;<&lt; %
           g/1/s//2/g
           w
           %





                           - 24 -










3.9.    

      SHELL        
.   :

                           set -v

           .  
      .  
       -
 , :

                        sh -v proc...

 proc -   .  -v    -
    -n,   -
    ( set -n    
,       EOF).

     

                           set -

     .     -x
 -v   

                            set -

   -     -
 -.

4.      

     ,       ,  
          .
       
   -  ,      
,  :

-      ;

-            
         ;

-          
            -
     .

     ,   :

                     USER=nata 

             USER


                           - 25 -










   nata.     -k -
   =          
  .       -
.      
   "1", "2", ... N (    -
          ,
   ).

            -
      
set. ,   

                           set *

 "1"        
  ,    "2" -    
..

           -
 :

-      , :

                           $user


-       , :

                           `pwd`


       ,       -
        ,  -
       eval. ,   -
      X   $y,   y - pqr,
      

                        eval echo $X


       pqr.

         eval    
         SHELL,      
      .  , 

                       wg='eval who | grep'
                       $wg fred


      

                       who | grep fred


                           - 26 -










          eval   ,    
        |         
     .

-     -.

     ,       
      ,    ,  -
      .       ,
         .     
        IFS;       
       ,      
      .       -
             ,      
        
     .  , a

                          echo ''


        ,      
           echo,     
     

                         echo $null


        ,   -
        null        
      .

-      .

             *,
     ?    [...] (   )  
            ,    
       .      
       .

       ,          for,
     .    case
    , -
    case.

      ,   SHELL    
    (     \,
'...'  "...").  ,    ,
      -
  .    , 
          -
 :




                           - 27 -










$      ;

`       ;

       ;

\      $, `, "  \.

     ,  

                          echo $

  x   .
,  

                          echo $*

      -
 ;    :

                       echo $1 $2 ...

 $@  $* ,       
. 

                          echo $@



                       echo $1 $2 ...

.         
   .

       1     -
      SHELL.

                 

                 $  *  `  "  '
            -|------------------|
           ' | -  -  -  -  -  t |
           ` | +  -  -  t  -  - |
           " | +  +  -  +  t  - |
            _|__________________|

       +    ;
       -     ;
       t     .

                   .1




                           - 28 -










4.1.   

      ,         
     .   -
,  ,      -
:     ,      
( ). , :

                      export USER BOX

  ,     USER 
BOX.          -
.     
        .   
         
      .  ( -
   ).

       ,      ,
     readonly (  -
).             
export:

                      readonly ...

       -
   .

4.2.    

        SHELL     ,
          .     


                        echo ${d-.}

   d,      ,
   '.' -   .   echo -
          
. ,    d
  ,  

                       echo ${d-'*'}

  *. , 

                        echo ${d-$X}

  d ,   ,  
 -   X.

            -
  :


                           - 29 -










                        echo ${d=.}

  d  ,    -
  '.'  (    ${...=...}
 ).

     

                    echo ${d?}

   d,     ,  
  -   .    -
   .      -
,        .
 ,      -
 ,    :

              : ${USER?} ${HOME?} ${BIN?}
              ...

 :          
SHELL,      .
       USER, HOME  BIN
  ,     
 .

4.3.     

         SHELL      
      .  , -
   pwd      -
,             ,  ,
/usr/nata/bin,  

                          d=`pwd`

  

                      d=/usr/nata/bin

,     `...`, -
          
 .      
       , 
 ,      
 "`"   \. , 

                        ls `echo $1`

 

                           ls $1



                           - 30 -










     ,       ,
         
 (      ).  -
       .  
    SHELL , -
      .     
 basename,         -
.  ,    

                     basename main.c .c

    main. -
  basename    -
  cc:

            case $A in
                 ...
                 *.c) B=`basename $A .c`
                 ...
            esac

    B   -
 A,     .c.

4.4.   

           
  :      -
.      
    ,   /   
      -i.

       ,      
 :

     -    / (, 
             );

     -    ,        
       ;

     -   ,        
         .

          , ,
       , 
   .

                
   :

     -      (while,
       until, if, for);


                           - 31 -










     -     .

                 
            , 
     -e.

4.5.   

     ,         ,
  .1.

           ,    
        .  
  - ,  (     -
 )    ,  
  .       -  ,
        , 
   ,    
.

                                                    1

                     

------------------------------------------------------------
 
------------------------------------------------------------
0         (    ,  
         )
1      
2      
3*      (CTRL/Q   core,    
         )
4*       (   )
5*       BPT ( )
6*        / IOT
7*       EMT
8*         
9        (     -
       )
10*     
11*      
12*        
13             
        -
14        
15       (  kill)
16      
------------------------------------------------------------

     ,   *,   
  . ,   -
  .


                           - 32 -










           -
    ,      
  .        
    ,        
    .        
  SHELL    trap.  ,


                trap 'rm /tmp/ps$$; exit' 2

  2 ()   

                     rm /tmp/ps$$; exit

    exit      
.

         :

1.   (          
    ).

2.   (   
    ).

3.       .     
   .

            , 
   trap.

        trap   , 
  .      -
   nohup:

                      trap '' 1 2 3 15

    ,    
    .

              -
, ,  :

                          trap 2 3

        
  

                            trap


          trap ( -
        )  


                           - 33 -










scan,       -
      ,   ,
    _    .   
      .
  scan   :


        for i in *
        do if test -d $d/$i
           then cd $d/$i
                while echo $i:
                    trap exit 2
                    read x
                do trap : 2; eval $x; done
           fi
        done

  read x        -
        -
 x.  _    
     ,   .

5.   

           ( 
  login)     SHELL.  
      .pro-
file,            
,     (,
  .profile  ).

          :

-c 
         .

-s        .   -
               
     .

-i     .

         "0"   -,  
    .profile.

6.      SHELL









                           - 34 -










                                                    2

                     

------------------------------------------------------------
 
------------------------------------------------------------
#      ;
|         ;
~         ;
;       ;
&        ,    -
       ;
&&       ,    -
         ;
||       ,    -
          ;
'...'   ;
"..."       ,  
          ;
\             -
       .
------------------------------------------------------------

                                                    3

                         -

------------------------------------------------------------
  
------------------------------------------------------------
[d]<&lt;             , -
                   
                d (  d=0);
[d]>&gt;             , -
                   
                d (  d=1);
[d]>&gt;>&gt;           >&gt;,   ,  
                     
             ;
[d]<&lt;<&lt;            ,    -
                ,  
              ;
[d]<&lt;&        
                ;
[d]>&gt;&         -
                 ;
[d]<&lt;&-          ;
[d]<&lt;>&gt;             ().
------------------------------------------------------------





                           - 35 -










                                                    4

                      

------------------------------------------------------------
  
------------------------------------------------------------
?              ;
*               ( .. );
[...]            ;
              ,   -, -
                  ;
------------------------------------------------------------

                                                    5

                

------------------------------------------------------------
   
------------------------------------------------------------
$n             $n  n;
$*            "1 2 ..."
$@            "1" "2" ...
$#              ;
$-            ,            
                set;
$?               ;
$$              ;
$!                ;
=   ;
${-}      ,     ,
               - ;
${=}    ;     -
                 -        
              ;
${?}    ,   ,
               -     -
                ;
${+}   ,   , 
              -   ;
$HOME             cd  ;
$PATH              ;
$MAIL            ;
$PS1            (  $);
$PS2              
               (  >&gt;);
$IFS              (  -
               ,     );
------------------------------------------------------------





                           - 36 -










                                         5

------------------------------------------------------------
   
------------------------------------------------------------
$TERM          .
------------------------------------------------------------
















































                           - 37 -










                        


    for  [in ...]
    do _
    done


    case  in
         [1 [|2]...) _;;]
         ...
    esac


    if _
    then _
    [elif _ then _]
    [else _]
    fi


    while _
    [do _]
    done


    until _
    [do _]
    done


    (_)


    { _; }


break [n]
        for, case, while  until   n;

continue
              for,  while  
     until;

exit [N]
         N;

:       ,    -
       .






                           - 38 -










                      


. 
           ;

eval [ ...]
               "
     ...";

exec [ ...]
            
     ...;

export [ ...]
               
      ;

read  ...
              
      ;

readonly [ ...]
       ;

set [-ekntuvx [ ...]]
         (  -
      .    sh);

shift
       ; 1=2 ...;

times
      ,    ;

trap [] [n]
      ,    n;

umask [nnn]
        -      (  
     );

wait [pid]
          pid.











                           - 39 -










                         



     .........................................    2

1.   ..........................................    3

2.    ..................................    3
2.1.    .................................    4
2.2.    ................................    4
2.3.     .............................    4
2.4.      .......    6
2.5.    / .....................    6
2.6.   ,    ............    8
2.7.   .................................    9
2.8.    ................................   12
2.9.    ...............................   12
2.10.   ..............................   12

3.    ...................................   15
3.1.   .....................................   17
3.2.   test ....................................   17
3.3.   for ....................................   18
3.4.   case ...................................   19
3.5.   while  until .........................   21
3.6.   break  continue ......................   22
3.7.   if .....................................   22
3.8.    ..................................   24
3.9.     ........................   25

4.       ......   25
4.1.    .............................   29
4.2.     .................   29
4.3.      .......   30
4.4.    ................................   31
4.5.    ............................   32

5.    ..............................   34

6.      SHELL ...........   34














                           - 40 -




Last-modified: Mon, 29 Jun 1998 14:15:05 GMT
: