Professional Documents
Culture Documents
www.chinaunix.net
HAWK.Li
http://bbs.chinaunix.net/forum/24/20031209/218853.html
1) shell ..............................................................................................................2
2) shell prompt(PS1) Carriage Return(CR) ......................................................4
3) echo echo echo .............................................................6
4) " "() ' '() ..................................................................................9
5) var=valueexport ....................................................................................14
6) exec source ................................................................................................19
7) ( ) { } ..........................................................................................................23
8) $(( )) $( ) ${ } ...................................................................................25
9) $@ $* ........................................................................................................30
10) && || ........................................................................................................33
11) > < ............................................................................................................38
12) if case ................................................................................................47
13) for what? while until ................................................................................51
1) shell
shell
(FIXME)
"(Operating System)"
linux "
(kernel)"
kernel
kernel "" shell kernel
kernel shell
(FIXME)
shell (interface)
(command line)
shell ---(Command Interpreter)
*
*
(log in) shell
login shell primary shell
(process) shell shell
fork
(shell script)
shell (sub shell)
primary shell sub shell sub shell script
()
kernel shell
* kernel
* kernel shell
linux shell
/etc/shells
shell ""
shell
sh
burne shell (sh)
burne again shell (bash)
csh
c shell (csh)
2
tc shell (tcsh)
korn shell (ksh)
(FIXME)
Linux shell bash
*
*
bash gnu project Unix
()
**(coursor)
**(prompt)
Linux
$
# root ()
shell prompt
* shell
shell prompt
cursor cursor
* shell prompt CR
( CR Enter
)
* (White Space)
* (Tab)
* (Enter)
4
(command-name)
*
* (alias)
* (function)
* shell (built-in)
* $PATH
-e ()
-E()
-n ( -e \c )
echo
\aALERT / BELL ()
\bBACKSPACE
\c
\EESCAPE
\fFORMFEED
\nNEWLINE
\rRETURN
\tTAB
\vVERTICAL TAB
\nASCII ( x )
\\
( O'Reilly Learning the Bash Shell, 2nd Ed.)
echo
:
$ echo -e "a\tb\tc\nd\te\tf"
a
b
c
d
e
f
\t abc def \n def
:
$ echo -e "\141\011\142\011\143\012\144\011\145\011\146"
a
b
c
d
e
f
ASCII
:
$ echo -e "\x61\x09\x62\x09\x63\x0a\x64\x09\x65\x09\x66"
a
b
c
d
e
f
ASCII
7
:
$ echo -ne "a\tb\tc\nd\te\bf\a"
a
b
c
d
f$
e (\b) e
\a
-n shell prompt
-n \a \c
shell shell script echo
echo
:
$ A=B
$ echo $A
B
$ echo $?
0
()
command line echo
...
* literal shell
* meta shell
( bash shell command line
O'Reilly Learning the Bash Shell, 2nd Edition 177 - 180
IFS CR meta
=
$ ( shell prompt )
> stdout
< stdin
|
& file descriptor
( ) nested subshell
{ } non-named function
;
&& true
|| false
! history
....
9
command line
quoting
bash quoting
* hard quote' ' () hard quote meta
* soft quote " " () soft quoe meta
( $ )()
* escape \ () escape () meta
A command line
* A=B <IFS> C
A soft quote
IFS
* A=B<space>C
soft quote hard quote Enter
:
$ A='B
>C
>'
$ echo $A
B
C
<enter> hard quote CR
:
$ A=B\
> C\
>
$ echo $A
B
C
<enter> <enter> escape
CR
<enter> CR command line
soft quote hard quote meta $
:
$ A=B\ C
$ echo "$A"
BC
$ echo '$A'
$A
echo $ soft quote
11
CU shell quoting
awk sed
awk $0 0 shell
$A 0 command line awk $$A
hard quoe
:
$ awk '{print $$A}' 1.txt
$A $ hard quote
(!)
12
:
A=0
awk "{print \$$A}" 1.txt
awk \{print\ \$$A\} 1.txt
awk '{print $'$A'}' 1.txt
awk '{print $'"$A"'}' 1.txt
.... ^_^
13
5) var=valueexport
command line bash (variable)...
""(name)"
"(value)
*(set)*
bash "="
name=value
A= B IFS
1A=B
$A=B $
a=B a=b
:
$ A=ls
$ B=la
$ C=/tmp
$ $A -$B $C
( $ shell prompt )
command line (
14
command line )
command line ( CR
)
$ ()
:
ls -la /tmp
""
:
shell IFS(Internal Field Seperator) command line
""(word)
(meta) command line
$ command line meta
shell echo
:
$ echo $A -$B $C
echo argument ""(STDOUT
)
:
ls -la /tmp
echo $A(ls)$B(la) $C(/tmp)
shell
A=B
B=$A
B A ""
""
A=B
B=C
A C
A=B
B=$A
A=C
B C
A B B C
(""
^_^)
15
""(append)
A=B:C:D
A=$A:E
A "B:C:D"
"A:B:C:E"
( : )
A=BCD
A=$AE
A $AE $A E
A=BCD
A=${A}E
{}
A BCD BCDE
( ${name}
CU
http://www.chinaunix.net/forum/viewtopic.php?t=201843
)
* export *
shell ""(local variable)
export ""(environment variable)
:
$ A=B
$ export A
:
$ export A=B
export A
export shell ""(substitution)
:
$ A=B
16
$ B=C
$ export $A
A B
$A B "" export
export process
process
**
bash unset
:
unset A
export unset ( shell
)
:
$ A=B
$ B=C
$ unset $A
B A
unset
:
$ A=
$ unset A
A ""(null value) A
:
$ A=
$ echo $A
$ unset A
$ echo $A
17
:
$ str=
# null
$ var=${str=expr}
# var
$ echo $var
$ echo $str
$ unset str
#
$ var=${str=expr}
# var
$ echo $var
expr
$ echo $str
expr
(yes, you!)
var=${str=expr} null unset
a.
b. var=${str=expr}
c.
e.
.... ^_^
18
6) exec source
CU Shell
( http://www.chinaunix.net/forum/viewtopic.php?t=194191 )
:
cd /etc/aa/bb/cc
shell shell
(process)
(parent process)
(child process)
Linux fork
( fork ... ^_^ )
(
)
""
*
""
(shell script)
shell script shell prompt
command line
script ""
script
(process + script)
* shell script sub-shell
sub-shell
:
cd /etc/aa/bb/cc
shell shell
19
:
shell script subshell
process parent process child process
child parent parent child
^_^
source
fork source
* source script shell sub-shell
shell script
script source
script
:
./my.script
:
source ./my.script
. ./my.script
/etc
script
script
script ""... ^_^
okay fork source
20
* exec script
exec source/fork
"+"
21
:
$ ./1.sh fork
$ ./1.sh source
$ ./1.sh exec
CU
http://www.chinaunix.net/forum/viewtopic.php?t=191051
...
~~~
happy scripting! ^_^
22
7) ( ) { }
... ^_^
( ) { }
shell
2*(3+4) ...
""(command group)
shell command line ( ) { }
command group
( ) { }
nested sub-shell
non-named command group
command line ( ) { } ~~~ ^_^
meta command meta ( Regular
Expression)
...
function
function command group
command group
non-named command group { }
(yes! ^_^ )
bash function
:
function function_name {
command1
command2
command3
....
}
23
:
fuction_name () {
command1
command2
command3
....
}
(Alias)
function ()
... ^_^
function ""
(library)
""...
shell
(shell script)
function command line function_name
script
shell function unset function_name
shell function
script function script
()
...
script function
script script ... ^_^
source
function
script source
RedHat Linux /etc/rc.d/init.d/functions
~~~ ^_^
okay ^_^
24
8) $(( )) $( ) ${ }
( ) { }
$( ) ${ }
bash shell $( ) ` ` ( ) (command
substitution)
:
$ echo the last sunday is $(date -d "last sunday" +%Y-%m-%d)
... ^_^
$( ) ` ` "" $( )
1, ` ` ' ' ( )
()
^_^
2, ` ` ( \` ) $( )
:
command1 `command2 `command3` `
command2 `command3` command3
command 2
command1 `command2 ...`
`command2 ` ``
:
command1 `command2 \`command3\` `
$( )
:
command1 $(command2 $(command3))
~~~ ^_^
25
$( ) ...
` ` unix shell shell script
$( ) shell bash2
... ^_^
${ } ...
$var ${var}
${ }
:
$ A=B
$ echo $AB
$A B
AB ...
${ }
:
$ echo ${A}B
BB
${ } bash
cu
http://www.chinaunix.net/forum/viewtopic.php?t=201843
${ }
file=/dir1/dir2/dir3/my.file.txt
${ }
${file#*/} / dir1/dir2/dir3/my.file.txt
${file##*/} / my.file.txt
${file#*.} . file.txt
${file##*.} . txt
${file%/*} / /dir1/dir2/dir3
${file%%/*} / ()
${file%.*} . /dir1/dir2/dir3/my.file
${file%%.*} . /dir1/dir2/dir3/my
# ( # $ )
% ( % $ )
26
${file:0:5} 5 /dir1
${file:5:5} 5 5 /dir2
$(( ))
bash $(( ))
+ - * / ""
%
& | ^ ! "ANDORXORNOT"
:
$ a=5; b=7; c=2
$ echo $(( a+b*c ))
19
$ echo $(( (a+b)/c ))
6
$ echo $(( (a*b)%c))
1
$(( )) $
$(( $a + $b * $c)) 19
$(( )) ()
umask 022
:
$ umask 022
$ echo "obase=8;$(( 8#666 & (8#777 ^ 8#$(umask)) ))" | bc
644
(( )) testing
a=5; ((a++)) $a 6
a=5; ((a--)) a=4
a=5; b=7; ((a < b)) 0 (true)
(( ))
<
>
<=
>=
==
!=
28
(( )) [ ] (
)
.. ^_^ okay...
...
29
9) $@ $*
$@ $* shell script positional parameter ...
(variable)
shell
positional parameter
shell script $0, $1, $2, $3 ...
:
script_name parameter1 parameter2 parameter3 ...
$0 shell script () $1
....
IFS IFS quoting positional
parameter
:
my.sh p1 "p2 p3" p4
p2 p3 soft quote my.sh $2
"p2 p3" $3 p4 ...
fucntion script script
^_^
function ( script ) postitional parameter
$0
my.sh fucntion my_fun , script
my_fun fp1 fp2 fp3
function $0 my.sh $1 fp1 p1 ...
my.sh script
:
#!/bin/bash
my_fun() {
echo '$0 inside function is '$0
echo '$1 inside function is '$1
echo '$2 inside function is '$2
}
echo '$0 outside function is '$0
echo '$1 outside function is '$1
30
positional parameter
* $10 10 ($1) 0
my.sh one two three four five six seven eigth nine ten command
line
my.sh $10 ten one0 ...
ten
${ } ${10}
shift
shift positional parameter
( $0 )
1 shift shift 1 $1 $2
$1$3 $2 ...
shift 3 $4 $1 ...
shift $1 ${10} ^_^
okay positional parameter
$# positional parameter
my.sh p1 "p2 p3"
p2 p3 IFS soft quote $# 2
p2 p3 quoting $# 3
function ...
shell script script
:
31
[ $# = 0 ]
0 script ...
$@ $*
soft quote ""( $0
)
32
10) && ||
... ^_^
return value
shell command function
return value
shell command line $? "" return value
my.file no.file
:
$ touch my.file
$ ls my.file
$ echo $? # first echo
0
$ ls no.file
ls: no.file: No such file or directory
$ echo $? # second echo
1
$ echo $? # third echo
0
echo ls my.file RV 0 true
echo ls no.file RV 0 false
echo echo $? RV 0 true
command return value
...
"" return value true
false
test
bash command line man test man bash
test
...
33
man
test expression
:
test expression
or:
[ expression ]
( [ ] )
(...)
bash test
* string
* integer( 0 )
* file
test expression
A=123
* [ "$A" = 123 ] $A 123 "
"
* [ "$A" -eq 123 ] $A ""
* [ -e "$A" ] 123 ""
expression ""test 0 (true) return value
0 (false)
expression " ! "() expression ""
0 0
test
* expression1 -a expression2 exrepssion true 0
0
* expression1 -o expression2 exrepssion true 0
false 0
:
[ -d "$file" -a -x "$file" ]
$file x test true
command line test ""
meta meta (
)
test expression
34
[ = abc ]
= test
:
$ [ "$A" = abc ]
$ echo $?
1
[ "" = abc ]
= soft quote test ...
test
:
$ A=123
$ [ -n "$A" ] && echo "yes! it's ture."
yes! it's ture.
$ unset A
$ [ -n "$A" ] && echo "yes! it's ture."
$ [ -n "$A" ] || echo "no, it's NOT ture."
no, it's NOT ture.
([ -n string ] string 0 true )
&& echo
test 0 RV
test 0 ...
|| echo test 0
&& ||
:
$ A=123
$ [ -n "$A" ] && echo "yes! it's ture." || echo "no, it's NOT ture."
yes! it's ture.
$ unset A
$ [ -n "$A" ] && echo "yes! it's ture." || echo "no, it's NOT ture."
no, it's NOT ture.
shell "" ^_^
36
37
:
$ mail -s test root
this is a test mail.
please skip.
^d ( crtl d )
mail stdin keyboard
stdin mail keyboard
stdin
:
$ cat /etc/passwd
cat
.... ^_^
:
$ cat
38
( ^d ...)
stdout stderr ... ... ^_^
-------------11.2
... ^_^
stdin stdout
stderr
stderr ""...
monitor
:
$ ls no.such.file
ls: no.such.file: No such file or directory
stdout stderr
monitor
:
$ touch my.file
$ ls my.file no.such.file
ls: no.such.file: No such file or directory
my.file
okay FD
FD
< (stdin)
> (stdout, stderr)
:
$ cat < my.file
my.file
:
$ mail -s test root < /etc/passwd
/etc/passwd ...
stdin keyboard ...
< FD ()
39
:
$ cat <<FINISH
first line here
second line there
third line nowhere
FINISH
cat 3 keyboard ^d
>
....
-------------11.3
okay~~~
0< stdin
redirection
* 1>
* 2>
stdout stderr
monitor
1 > 1> > stdout
ls
:
$ ls my.file no.such.file 1>file.out
ls: no.such.file: No such file or directory
monitor stderr stdout file.out
:
$ ls my.file no.such.file 2>file.err
my.file
40
file locking
:
$ ls my.file no.such.file 1>file.both 2>file.both
file system FD
stdout(1) stderr(2) file.both
""
"" stdout stderr file.out
* 1, 2, 3 stdout
* 3, 4, 5 stderr
stderr 3
stderr stdout stdout stderr
FD ""...
bingo
* 2>&1 stderr stdout
* 1>&2 >&2 stdout stderr
:
$ ls my.file no.such.file 1>file.both 2>&1
~~~ ^_^
locking
...
41
-------------11.4
okay I/O Redirction ...
(...) ~~~ ^_^
""
...
""
---
""~~~
Linux /dev/null
""
null ....
~~~ ^_^
null I/O Redirection
* FD1 FD2 /dev/null stdout stderr
* FD0 /dev/null nothing
stdout stderr
stderr ()
:
$ ls my.file no.such.file 2>/dev/null
my.file
... ^_^
>/dev/null 2>&1
:
$ ls my.file no.such.file &>/dev/null
( &> >& ~~! )
okay
:
42
:
$ set -o noclobber
$ echo "4" > file.out
-bash: file: cannot overwrite existing file
""
set -o set +o
:
$ set +o noclobber
$ echo "5" > file.out
$ cat file.out
5
... ""
43
:
$ echo "some text here" > file
$ cat < file
some text here
$ cat < file > file.bak
$ cat < file.bak
some text here
$ cat < file > file
$ cat < file
-------------11.5
~~~ ~~~ ^_^
$ cat < file > file
priority
* IO Redirection stdout stderr stdin
...
I/O Redirection sorry~~~
~~ ^_^
(~!#@!$%)
---- pipe line
pipe line
command line " | " pipe line
pipe line
... pipe
""...
input output
*
pipe line stderr stdin
2> file
pipe line
* stderr stdin
()
^_^
* stderr stdout
... (...)
cm3 stdin (
)
:
cm1 | cm2 > file ; cm3 < file
file I/O
command file I/O
shell file I/O
tee
* tee I/O stdout
:
cm1 | cm2 | tee file | cm3
tee -a
cat, more, head, tail, wc, expand, tr, grep, sed, awk, ...
pipe line command line
""... ^_^
....
I/O Redirection
shell bye... ^_^
46
12) if case
...
~~~
10 return value
...
return value && ||
command group shell script
:
comd1 && {
comd2
comd3
} || {
comd4
comd5
}
shell script
&& || ""
bash shell
:
if comd1
then
comd2
comd3
else
comd4
47
comd5
fi
shell script if
if command line true return value ( test
return value)
then else fi
keyword
if else then
( then command " " null command )
then else shell script
if shell script
...
case
if
string
:
QQ () {
echo -n "Do you want to continue? (Yes/No): "
read YN
48
:
...
if echo "$YN" | grep -q '^[Yy]\([Ee][Ss]\)*$'
...
Regular Expression ( RE)
...
case
:
QQ () {
echo -n "Do you want to continue? (Yes/No): "
read YN
case "$YN" in
[Yy]|[Yy][Ee][Ss])
QQ
;;
*)
exit 0
;;
esac
}
QQ
case ( string)
script
Linux /etc/init.d/* script
case
:
case "$1" in
start)
49
start
;;
stop)
stop
;;
status)
rhstatus
;;
restart|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/syslog ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart}"
exit 1
esac
( positional parameter 9 )
okay.... ^_^
50
:
for var in one two three four five
do
echo ----------echo '$var is '$var
echo
done
for loop
positional parameter
... ("")
""()for
:
for ((i=1;i<=10;i++))
do
echo "num is $i"
done
1) while num=1
2) (test) $num 10
3) true echo num
4) num 1+1=2 10
true
5) num 10+1=11 ...
* while true
:
while :; do
echo looping...
done
52
( daemon)
signal ( ctrl-c )
( process signal )
while loop until loop
* while until return value false
until
:
num=1
until [ ! "$num" -le 10 ]; do
echo "num is $num"
num=$(($num + 1))
done
:
num=1
until [ "$num" -gt 10 ]; do
echo "num is $num"
num=$(($num + 1))
done
...
break continue n (
)
continue 1
shell script loop script
....
----------
shell
""""...
shell ""
""""
--- ^_^
p.s.
1)
2)
Good luck and happy studying!
54