shell½Å±¾
1.1 shellÔËËã·û
1.1.1 ËãÊõÔËËã·û
shellÖ§³ÖËãÊõÔËËã·û
#!/bin/bash aa=123 bb=456 echo `expr ${aa} + ${bb}` |
Êä³ö½á¹û£º579
ÔÚ½øÐÐËãÊõÔËËã·ûµÄʱºò£¬ÒªÊ¹ÓÃexpr²Å¿ÉÒÔ£¬ÆäʵÕâ¸ö``ÊÇesc¼üϵģ¬½øÐÐËãÊõÔËËãµÄʱºò£¬ÒªÔÚ·ûºÅ×óÓÒÁ½±ßÓпոñ£¬µ÷ÓñäÁ¿µÃÒªÓõ½$
ËãÊõÔËËã·ûÄÜÓõÄÊÇ£º+ - * / % = == !=
ÆäÖÐ*µÄʱºò£¬ÐèÒªÔÚ*Ç°¼ÓÉÏ\£¬Ò²¾ÍÊÇ\*£¬ÔÒòÊÇÒòΪ*Ôڽű¾ÖÐÊÇͨÅä·û
ÓÃÓÚÔËËãµÄ±äÁ¿£¬±ØÐëÊÇÊý×Ö
1.1.2 ¹ØϵÔËËã·û
shellÒ²Ö§³Ö¹ØϵÔËËã·û
¹ØϵÔËËã·ûÒ»°ã¶¼¸úifÓï¾ä½øÐдîÅä
-eq equal ÏàµÈ£¬Èç¹ûÁ½¸öÊýÏàµÈ£¬ÄÇô·µ»ØÕæÖµ£¬·ñÔò·µ»Ø¼Ù
-ne not equal ²»ÏàµÈ£¬Èç¹ûÁ½¸öÊý²»ÏàµÈ£¬ÄÇô·µ»ØÕæÖµ£¬·ñÔò·µ»Ø¼Ù
-gt great than ´óÓÚ£¬×ó±ßµÄÖµÊÇ·ñ´óÓÚÓұߵģ¬Èç¹ûÊÇ£¬ÄÇô·µ»ØÕæÖµ£¬·ñÔò·µ»Ø¼Ù
-lt less than СÓÚ£¬×ó±ßµÄÖµÊÇ·ñСÓÚÓұߵÄÖµ£¬Èç¹ûÊÇ£¬ÄÇô·µ»ØÕ棬·ñÔò·µ»Ø¼Ù
-ge great equal ´óÓÚµÈÓÚ£¬×ó±ßµÄÖµÊÇ·ñ´óÓÚ»òÕßµÈÓÚÓұߵģ¬Èç¹ûÊÇ£¬ÄÇô·µ»ØÕæÖµ£¬·ñÔò·µ»Ø¼Ù
-le less equal СÓÚµÈÓÚ£¬×ó±ßµÄÖµÊÇ·ñСÓÚµÈÓÚÓұߵÄÖµ£¬Èç¹ûÊÇ£¬ÄÇô·µ»ØÕ棬·ñÔò·µ»Ø¼Ù
²¼¶ûÔËËã·û£¬×Ö·û´®ÔËËã·û£¬ÎļþÔËËã·ûµÈ±ðµÄÔËËã·û£¬Çë²éÔÄ¡¶shell½Å±¾±à³Ì-A¡·11Ò³
1.2 shellÊý×é
Shell ÔÚ±à³Ì·½Ãæ±È Windows Åú´¦ÀíÇ¿´óºÜ¶à£¬ÎÞÂÛÊÇÔÚÑ»·¡¢ÔËËã¡£
bash Ö§³ÖһάÊý×飨²»Ö§³Ö¶àάÊý×飩£¬²¢ÇÒûÓÐÏÞ¶¨Êý×éµÄ´óС¡£ÀàËÆÓë C ÓïÑÔ£¬Êý×éÔª
ËصÄϱêÓÉ 0 ¿ªÊ¼±àºÅ¡£»ñÈ¡Êý×éÖеÄÔªËØÒªÀûÓÃϱ꣬ϱê¿ÉÒÔÊÇÕûÊý»òËãÊõ±í´ïʽ£¬Æä
ÖµÓ¦´óÓÚ»òµÈÓÚ 0¡£
Êý×éµÄ¶¨Ò壺array=(array[0] array[1] array[2] array[3] ...)
»¹¿ÉÒÔµ¥¶À¶¨ÒåÊý×éµÄ¸÷¸ö·ÖÁ¿£º
array [0]=value0
array [5]=value5
array[10]=value10
¿ÉÒÔ²»Ê¹ÓÃÁ¬ÐøµÄϱ꣬¶øÇÒϱêµÄ·¶Î§Ã»ÓÐÏÞÖÆ¡£
${#array[@]} : ¼ÆËãarrayÊý×éµÄÔªËصĸöÊý
${#array[n]} : ¼ÆËãarray[n]ÔªËصij¤¶È
1.3 shellÅжÏÓï¾äºÍÑ»·Óï¾ä
1.3.1 ifÓï¾ä
if Óï¾äͨ¹ý¹ØϵÔËËã·ûÅжϱí´ïʽµÄÕæ¼ÙÀ´¾ö¶¨Ö´ÐÐÄĸö·ÖÖ§¡£Shell ÓÐÈýÖÖ if ... else
Óï¾ä£º
if ... fi Óï¾ä£»
if ... else ...fi Óï¾ä£»
if ... elif ...else ... fi Óï¾ä
ifÓï¾äµÄÓï·¨£º
if [ ±í´ïʽ ]
then
±í´ïʽΪÕ棬¾ÍÖ´ÐÐÓï¾ä¡£
fi
Èç¹û ±í´ïʽ ·µ»Ø true£¬then ºó±ßµÄÓï¾ä½«»á±»Ö´ÐУ»Èç¹û·µ»Ø false£¬²»»áÖ´ÐÐthenÖ®ºóµÄÓï¾ä
#!/bin/bash A=456 B=456 if [ $A -lt $B ] then echo A less than B elif [ $A -eq $B ] then echo A equal B else echo A great than B fi |
ÒÔÉÏÊÇif elif elseÓï¾äµÄÓ÷¨£¬Ò»°ã¸ú¹ØϵÔËËã·û´îÅä
1.3.2 caseÓï¾ä
case ... esac ÓëÆäËûÓïÑÔÖÐµÄ switch ... case Óï¾äÀàËÆ£¬ÊÇÒ»ÖÖ¶à·Ö֦ѡÔñ½á¹¹¡£
case Óï¾äÆ¥ÅäÒ»¸öÖµ»òÒ»¸öģʽ£¬Èç¹ûÆ¥Åä³É¹¦£¬Ö´ÐÐÏàÆ¥ÅäµÄÃüÁî¡£case Óï¾ä¸ñʽÈçÏ£º
case ±äÁ¿ in
ģʽ 1)
command1
command2
command3
;;
ģʽ 2£©
command1
command2
command3
;;
*)
command1
command2
command3
;;
case ¹¤×÷·½Ê½ÈçÉÏËùʾ¡£È¡ÖµºóÃæ±ØÐëΪ¹Ø¼ü×Ö in£¬Ã¿Ò»Ä£Ê½±ØÐëÒÔÓÒÀ¨ºÅ½áÊø¡£È¡Öµ¿É
ÒÔΪ±äÁ¿»ò³£Êý¡£Æ¥Åä·¢ÏÖÈ¡Öµ·ûºÏijһģʽºó£¬Æä¼äËùÓÐÃüÁʼִÐÐÖ±ÖÁ ;;¡£
;; ÓëÆäËûÓïÑÔÖÐµÄ break ÀàËÆ£¬Òâ˼ÊÇÌøµ½Õû¸ö case Óï¾äµÄ×îºó
°¸Àý£º
#!/bin/bash aa=$1 case $aa in 10)echo 'aa = 10';; 20)echo 'aa = 20';; 30)echo 'aa = 30';; *)echo 'aa = *';; esac |
µ±Ç°Ã涼²»Äܱ»Æ¥Åäµ½µÄʱºò£¬»áÆ¥Åäµ½×îºóµÄ*µÄ·ÖÖ§£¬*ÊÇͳÅä·û£¬ÎÞÂÛÊäÈëʲô£¬Ö»ÒªÄÜÀ´µ½ÕâÀһ¶¨»á±»Æ¥Åäµ½£¬Õâ¾ÍÀàËÆÓÚCÓïÑÔÖеÄswitchÓï¾äÖеÄdefault¹Ø¼ü×Ö£¬Ôڽű¾ÖпÉÒÔʹÓÃÕâ¸ö·½Ê½
1.3.3 forÑ»·
ÓëÆäËû±à³ÌÓïÑÔÀàËÆ£¬ShellÖ§³Ö for Ñ»·¡£
for Ñ»·Ò»°ã¸ñʽΪ£º
for ±äÁ¿ in Áбí
do
command1
command2
commandN
done
ÁбíÊÇÒ»×éÖµ£¨Êý×Ö¡¢×Ö·û´®µÈ£©×é³ÉµÄÐòÁУ¬Ã¿¸öֵͨ¹ý¿Õ¸ñ·Ö¸ô¡£Ã¿Ñ»·Ò»´Î£¬¾Í½«ÁÐ
±íÖеÄÏÂÒ»¸öÖµ¸³¸ø±äÁ¿¡£
#!/bin/bash for num in 1 2 3 4 5 do echo "num = $num" done |
[root@localdemo]# bash for.sh
num = 1
num = 2
num = 3
num = 4
num = 5
ÓëCÓïÑÔÀàËƵÄÓ÷¨Ò²ÓУº
for((i=1;i<=10;i++)) »òÕß for i in $(seq 10) |
ÒÔÉϵÄÕâ2ÖÖ·½Ê½£¬½á¹ûÊÇÒ»ÑùµÄ£¬µ±ÁбíµÄÖµÊǹæÂɵÝÔö»òÕߵݼõ£¬ÄÇô¿ÉÒÔÓúóÃæµÄ2ÖÖ·½Ê½£¬Èç¹ûÁбíµÄÖµÊDz»¹æÂɵģ¬ÄÇôֻÄÜÓõÚÒ»ÖÖÁË
1.3.4 whileÑ»·
while Ñ»·ÓÃÓÚ²»¶ÏÖ´ÐÐһϵÁÐÃüÁҲÓÃÓÚ´ÓÊäÈëÎļþÖжÁÈ¡Êý¾Ý£»ÃüÁîͨ³£Îª²âÊÔÌõ¼þ¡£
Æä¸ñʽΪ£º
while command
do
Statement(s) to be executed if command is true
Done
ÃüÁîÖ´ÐÐÍê±Ï£¬¿ØÖÆ·µ»ØÑ»·¶¥²¿£¬´ÓÍ·¿ªÊ¼Ö±ÖÁ²âÊÔÌõ¼þΪ¼Ù¡£
#µÚÒ»ÖÖwhileÑ»·Ð´·¨ i=1 while ((i <= 5)) do echo $i i=`expr $i + 1` done #µÚ¶þÖÖwhileÑ»·Ð´·¨ num=1 while [ $num -le 5 ] do echo $num num=`expr $num + 1` done |
1.4 shellº¯Êý
º¯Êý¿ÉÒÔÈÃÎÒÃǽ«Ò»¸ö¸´ÔÓ¹¦ÄÜ»®·Ö³ÉÈô¸ÉÄ£¿é£¬ÈóÌÐò½á¹¹¸ü¼ÓÇåÎú£¬´úÂëÖظ´ÀûÓÃÂʸü
¸ß¡£ÏñÆäËû±à³ÌÓïÑÔÒ»Ñù£¬Shell Ò²Ö§³Öº¯Êý¡£Shell º¯Êý±ØÐëÏȶ¨ÒåºóʹÓá£
Shell º¯ÊýµÄ¶¨Òå¸ñʽÈçÏ£º
function_name () {
list of commands
[ return value ]
}
º¯Êý·µ»ØÖµ£¬¿ÉÒÔÏÔʽÔö¼Ó return Óï¾ä£»Èç¹û²»¼Ó£¬»á½«×îºóÒ»ÌõÃüÁîÔËÐнá¹û×÷Ϊ·µ»ØÖµ
µ÷ÓÃÒ»¸öº¯Êý£¬Ö±½ÓÊäÈ뺯ÊýÃû¼´¿É£¬²»ÐèÒªÀ¨ºÅ
Èç¹ûҪɾ³ýÒ»¸öº¯Êý£¬¸úɾ³ý±äÁ¿Ò»Ñù£¬Ö±½ÓʹÓÃunset£¬µ«ÊÇÒªÔÚÖ®ºó¼ÓÉÏ.fÉùÃ÷Ҫɾ³ýµÄÊÇÒ»¸öº¯Êý
#!/bin/bash func(){ echo 1234567890 func1 } func1(){ echo 0987654321 } #unset .f func1 func echo $? |
[root@local demo]# ./function.sh
1234567890
0987654321
0
º¯Êý´«²Î¸úshell½Å±¾µÄ´«²Î·½Ê½ÊÇÒ»ÑùµÄ
#!/bin/bash func(){ echo "func µÚÒ»¸ö´«²Î£º$1" echo "func µÚ¶þ¸ö´«²Î£º$2" echo "func µÚÈý¸ö´«²Î£º$3" echo "func µÚËĸö´«²Î£º$4" echo "func µÚÎå¸ö´«²Î£º$5" echo "func µÚÁù¸ö´«²Î£º$6" echo "func µÚÆ߸ö´«²Î£º$7" echo "func µÚ°Ë¸ö´«²Î£º$8" echo "func µÚ¾Å¸ö´«²Î£º$9" echo "func µÚÊ®¸ö´«²Î£º$10" echo "func µÚÊ®¸ö´«²Î£º${10}" } func 1 2 3 4 5 6 7 8 9 11 11 |
[root@local demo]# ./function2.sh
func µÚÒ»¸ö´«²Î£º1
func µÚ¶þ¸ö´«²Î£º2
func µÚÈý¸ö´«²Î£º3
func µÚËĸö´«²Î£º4
func µÚÎå¸ö´«²Î£º5
func µÚÁù¸ö´«²Î£º6
func µÚÆ߸ö´«²Î£º7
func µÚ°Ë¸ö´«²Î£º8
func µÚ¾Å¸ö´«²Î£º9
func µÚÊ®¸ö´«²Î£º10
func µÚÊ®¸ö´«²Î£º11
´ÓÔËÐнá¹û¿ÉÒÔ¿´µ½×îºó2¸ö½á¹ûÓÐËù²îÒ죬ÔÚʹÓÃÍⲿ´«²ÎµÄʱºò£¬$ºó±ßÈç¹ûûÓл¨À¨ºÅ£¬ÄÇôֻÄÜʶ±ðµÚÒ»¸ö×Ö·û£¬Èç¹ûҪʹÓõÚÊ®¸ö´«²Î£¬¾Í±ØÐëµÃʹÓÃ${10}ÕâÖÖÐÎʽ£¬¶ø$10ÕâÖÖÐÎʽÊÇ´íÎóµÄ£¬Ï൱ÓÚÔÚ$1ºóÃæ¸úÁËÒ»¸ö0£¬¶ø²»ÊǵÚÊ®¸ö´«²ÎµÄÒâ˼
ÌØÊâ´«²Î£º
$#£º´«²Î¸öÊý
$@£ºËùÓд«²Î
$*£ºËùÓд«²Î
$?£ºº¯Êý·µ»ØÖµ
$n£ºµÚn¸ö´«²Î