Linux³ÌÐòÉè¼ÆÈëÃÅ - bash, Shell Scripts
ÖÚËù½ÔÖªµØ£¬UNIXÉÏÒÔС¹¤¾ß×ÅÃû£¬ÀûÓÃÐí¶à¼òµ¥µÄС¹¤¾ß£¬À´Íê³ÉÔ±¾ÐèÒª
´óÁ¿ÈíÌ忪·¢µÄ¹¤×÷£¬ÕâÒ»µãÌØÉ«£¬Ê¹µÃUNIX³ÉΪÐí¶àÈËÐÄÄ¿ÖÐÀíÏëµÄϵͳƽ
̨¡£
ÔÚÖÚ¶àµÄС¹¤¾ßÖУ¬Shell ScriptËãµÃÉÏÊÇ×î»ù±¾¡¢×îÇ system is FreeBSD"
echo "Do FreeBSD stuff here..."
;;
*)
echo "Unknown system : $SYSTEM"
echo "I don't what to do..."
;;
esac
select name [ in word; ] do list ; done
select¹ËÃû˼Òå¾ÍÊÇÔÚwordÖÐÑ¡ÔñÒ»Ïî
·¶Àý
#!/bin/sh
WORD="a b c"
select i in $WORD ; do
case $i in
a)
echo "I am A"
;;
b)
echo "I am B"
;;
c)
echo "I am C"
;;
*)
break;
;;
esac
done
Ö´Ðнá¹û
[foxman@foxman bash]# ./select_demo
1) a
2) b
3) c
#? 1
I am A
1) a
2) b
3) c
#? 2
I am B
1) a
2) b
3) c
#? 3
I am C
1) a
2) b
3) c
#? 4
[foxman@foxman bash]#
if list then list [ elif list then list ] ... [ else list ] fi
¼¸ÖÖ¿ÉÄܵÄд·¨
if list then
do something here
fi
if list then
do something here
else
do something else here
fi
if list then
do something here
elif list then
do another thing here
fi
if list then
do something here
elif list then
do another thing here
else
do something else here
fi
ÕâÀïҪǨ³¶µ½Exit StatusµÄÎÊÌ⣬µÈÎÒ½«Exit StatusµÄÎÊÌâ˵Ã÷ÍêÔÙ»ØÀ´¼Ì
Ðø¡£
while list do list done/until list do list done
·¶ÀýÒ» : ÎÞÏÞ«¿È¦Ð´·¨
#!/bin/sh
while : ; do
echo "do something here"
sleep 5
done
·¶Àý¶þ
ÕâÀïҪǨ³¶µ½Exit StatusµÄÎÊÌ⣬µÈÎÒ½«Exit StatusµÄÎÊÌâ˵Ã÷ÍêÔÙ»ØÀ´¼Ì
Ðø¡£
[ function ] name () { list; }
·¶Àý
function func(arg1,arg2) {
echo $arg1
echo $arg2
return 1
}
Ààͬì¶PascalÖеÄfunction¡£
bashÄÚ½¨Ö¸Á
. filename [arguments]
source filename [arguments]
ÓÉfilenameÖжÁÈ¡ÃüÁ²¢Ö´ÐС£
Äú»áÔÚ/etc/rc.d/*Öз¢Ïֺܶà
. /xxxx
µÄÖ¸Á¶øxxxxµÄpermission¶¼²»ÊÇ¿ÉÖ´Ðеġ£ÊÂʵÉÏ£¬ÔÚtcshÖУ¬ÐèÒªÓÃ
source /xxxx
À´×öͬÑùµÄÖ¸Áî¡£
×¢Òâµ½"."µÄááÃæÊÇÓпոñµÄ¡£filenameÊÇÄÚº¬Ö¸ÁîµÄ´¿ÎÄ×Öµµ¼´¿É£¬ÎÞÐë
chmod 755 filename¡£
·¶Àý
filename : my_source
DEV=lo
IP=127.0.0.1
NETMASK=255.0.0.0
BROADCAST=127.255.255.255
ifconfig $IP netmask $NETMASK broadcast $BROADCAST dev $DEV
½ÓÏÂÀ´
. /path/my_source
»ò
source my_source
±ã¿ÉÖ´ÐиÃscript£¬¶ø²»ÐèÒª"chmod 755 my_source"
alias [name[=value] ...]
¡¡³Æ
ÀýÈçÄúÈç¹ûÀ´×ÔDOSµÄÊÀ½ç£¬¶ÔUNIXµÄÖ¸Áϰ¹ß£¬¿ÉÓÃaliasÀ´Ð޸ģ¬ÒÔ·ûºÏ
ÄúµÄÏ°¹ß¡£
·¶Àý
alias ls="ls --color"
alias dir="ls"
alias cd..="cd .."
alias copy="cp -f" # dangerous, recommend, cp -i
alias del="rm -f" # dangerous, recommend, rm -i
alias move="mv -f" # dangerous, recommend, mv -i
alias md="mkdir"
alias rd="rmdir"
unalias [-a] [name ...]
unaliasÈ¡ÏûaliasµÄÉ趨¡£unalias -a½«È«²¿aliasÈ¡Ïû¡£
·¶Àý
unalias copy
bg [jobspec]
fg [jobspec]
jobs [-lnp] [ jobspec ... ]
jobs -x command [ args ... ]
kill [-s sigspec | -sigspec] [pid | jobspec] ...
kill -l [signum]
wait [n]
bind [-m keymap] [-lvd] [-q name]
bind [-m keymap] -f filename
bind [-m keymap] keyseq:function-name
break [n]
¿ØÖÆ«¿È¦ÖÐʹÓÃ
·¶Àý
continue [n]
¿ØÖÆ«¿È¦ÖÐʹÓÃ
·¶Àý
exit [n]
À뿪³ÌÐò¡£nÊÇExit Status¡£
return [n]
ÔÚfunctionÖÐʹÓá£nΪ·µ»ØÖµ£¬Æä×÷ÓÃÓëExit StatusÒ»Ñù¡£
builtin shell-builtin [arguments]
cd [dir]
command [-pVv] command [arg ...]
declare [-frxi] [name[=value]]
typeset [-frxi] [name[=value]]
dirs [-l] [+/-n]
echo [-neE] [arg ...]
enable [-n] [-all] [name ...]
eval [arg ...]
exec [[-] command [arguments]]
export [-nf] [name[=word]] ...
export -p
set [--abefhkmnptuvxldCHP] [-o option] [arg ...]
unset [-fv] [name ...]
fc [-e ename] [-nlr] [first] [last]
fc -s [pat=rep] [cmd]
getopts optstring name [args]
hash [-r] [name]
help [pattern]
history [n]
history -rwan [filename]
let arg [arg ...]
local [name[=value] ...]
logout
popd [+/-n]
pushd [dir]
pushd +/-n
pwd
read [-r] [name ...]
readonly [-f] [name ...]
readonly -p
shift [n]
suspend [-f]
test expr
[ expr ]
times
trap [-l] [arg] [sigspec]
type [-all] [-type | -path] name [name ...]
ulimit [-SHacdfmstpnuv [limit]]
umask [-S] [mode]
bashÄÚ½¨ÈþÊý
bashµÄÄÚ½¨ÈþÊýºÜ¶à£¬Äã¿ÉÒÔ×ÔÐÐ"man bash"²éÒ»²é¡£ÕâÀïÎÒֻ˵Ã÷һЩ³£ÓÃ
¼°ÖØÒªµÄ¡£
PPID : ¸ÃbashµÄºô½ÐÕßprocess ID.
PWD : Ä¿Ç°µÄ¹¤×÷Ŀ¼¡£
OLDPWD : ÉÏÒ»¸ö¹¤×÷Ŀ¼¡£
HOSTTYPE : »úÆ÷ÖÖÀà¡£
OSTYPE : ×÷ҵϵͳÃû³Æ¡£
PATH : ÃüÁîËÑѰ·¾¶¡£
PATH="/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:."
HOME : ĿǰʹÓÃÕßµÄhome directory;
PS1 : The value of this parameter is expanded (see PROMPTING
below) and used as the primary prompt string. The
default
value is ``bash\$ ''.
PS2 : The value of this parameter is expanded and used as the
secondary prompt string. The default is ``> ''.
PS3 : The value of this parameter is used as the prompt for the
select command (see SHELL GRAMMAR above).
PS4 : The value of this parameter is expanded and the value is
printed before each command bash displays during an
execu-
tion trace. The first character of PS4 is replicated
mul-
tiple times, as necessary, to indicate multiple levels
of
indirection. The default is ``+ ''.
OK STATION, Webmaster, Brian Lin