shell
-
" "
/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
, , -
.
-
( -
). <<.
, :
wc << filels
filels.
,
/.
>>
,
,
.
>>>>
.
, .
<<
.
<<<<
(. 4.8).
-
,
, .
, -
.
>>&
, ,
- 7 -
.
<<&
.
<<&- .
>>&- .
2.6. ,
,
,
. ,
:
ls | wc
-
. ,
"|", .
:
ls >> filels
wc << 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 >>/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 ,
( >>).
IFS -,
.
- 11 -
TERM .
2.8.
, -
, $.
:
PS1=:
:.
-
,
e >>. , -
,
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 >> $i; done
create alpha beta
alpha beta.
3.4. case
add,
:
case $# in
1) cat >>>> $1;;
2) cat >>>> $2 << $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 ...