ºìÁªLinuxÃÅ»§
Linux°ïÖú

Shell³£¼û֪ʶ

·¢²¼Ê±¼ä:2008-10-15 09:44:27À´Ô´:ºìÁª×÷Õß:jerry520
Shell³£¼û֪ʶ
Ò»¡¢Óû§µÇ½½øÈëϵͳºóµÄϵͳ»·¾³±äÁ¿
$HOME ʹÓÃÕß×Ô¼ºµÄĿ¼
$PATH Ö´ÐÐÃüÁîʱËùËÑÑ°µÄĿ¼
$TZ ʱÇø
$MAILCHECK ÿ¸ô¶àÉÙÃë¼ì²éÊÇ·ñÓÐеÄÐżþ
$PS1 ÔÚÃüÁîÁÐʱµÄÌáʾºÅ
$PS2
µ±ÃüÁîÉÐδ´òÍêʱ£¬Shell ÒªÇóÔÙÊäÈëʱµÄÌáʾºÅ
$MANPATH man Ö¸ÁîµÄËÑѰ·¾¶
¶þ¡¢ÌØÊâ±äÁ¿
$0 Õâ¸ö³ÌÐòµÄÖ´ÐÐÃû×Ö
$n Õâ¸ö³ÌÐòµÄµÚn¸ö²ÎÊýÖµ£¬n=1..9
$* Õâ¸ö³ÌÐòµÄËùÓвÎÊý
$# Õâ¸ö³ÌÐòµÄ²ÎÊý¸öÊý
$$ Õâ¸ö³ÌÐòµÄPID
$!
Ö´ÐÐÉÏÒ»¸öÖ¸ÁîµÄPID
$? Ö´ÐÐÉÏÒ»¸öÖ¸ÁîµÄ·µ»ØÖµ
Èý¡¢shellÖеıäÔª
* ÈÎÒâ×Ö·û´®
? Ò»¸öÈÎÒâ×Ö·û
[abc] a, b, cÈýÕßÖÐÖ®Ò»
[a-n] ´Óaµ½nµÄÈÎÒ»×Ö·û
ËÄ¡¢¼¸¸öÌØÊâ×Ö·û±íʾ
\b ÍË»Ø
\c ´òÓ¡Ò»ÐÐʱûÓл»Ðзû Õâ¸öÎÒÃǾ­³£»áÓõ½
\f »»Ò³
\r »Ø³µ
\t ÖƱí
\v ´¹Ö±ÖƱí
\\ ·´Ð±Ïß±¾Éí
Îå¡¢ÅжÏÎļþµÄÊôÐÔ
¸ñʽ£º-²Ù×÷·û filename
-e Îļþ´æÔÚ·µ»Ø1£¬ ·ñÔò·µ»Ø0
-r Îļþ¿É¶Á·µ»Ø1,·ñÔò·µ»Ø0
-w Îļþ¿Éд·µ»Ø1,·ñÔò·µ»Ø0
-x Îļþ¿ÉÖ´Ðзµ»Ø1,·ñÔò·µ»Ø0
-o ÎļþÊôÓÚÓû§±¾ÈË·µ»Ø1, ·ñÔò·µ»Ø0
-z Îļþ³¤¶ÈΪ0·µ»Ø1, ·ñÔò·µ»Ø0.
-f
ÎļþΪÆÕͨÎļþ·µ»Ø1, ·ñÔò·µ»Ø0
-d ÎļþΪĿ¼Îļþʱ·µ»Ø1, ·ñÔò·µ»Ø0
Áù¡¢²âÊÔ×Ö·û´®
×Ö·û´®1 = ×Ö·û´®2¡¡µ±Á½¸ö×Ö´®ÏàµÈʱΪÕæ
×Ö·û´®1 != ×Ö·û´®2
µ±Á½¸ö×Ö´®²»µÈʱΪÕæ
-n ×Ö·û´®¡¡ ¡¡ µ±×Ö·û´®µÄ³¤¶È´óÓÚ0ʱΪÕæ
-z ×Ö·û´®¡¡ µ±×Ö·û´®µÄ³¤¶ÈΪ0ʱΪÕæ
×Ö·û´® µ±´®×Ö·û´®Îª·Ç¿ÕʱΪÕæ
Æß¡¢²âÊÔÁ½¸öÕûÊý¹Øϵ
Êý×Ö1 -eq Êý×Ö2 Á½ÊýÏàµÈΪÕæ
Êý×Ö1 -ne Êý×Ö2 Á½Êý²»µÈΪÕæ
Êý×Ö1 -gt Êý×Ö2 Êý×Ö1´óÓÚÊý×Ö2ΪÕæ
Êý×Ö1 -ge Êý×Ö2 ¡¡
Êý×Ö1´óÓÚµÈÓÚÊý×Ö2ΪÕæ
Êý×Ö1 -lt Êý×Ö2 Êý×Ö1СÓÚÊý×Ö2ΪÕæ
Êý×Ö1 -le Êý×Ö2 Êý×Ö1СÓÚµÈÓÚÊý×Ö2ΪÕæ
°Ë¡¢Âß¼­²âÊÔ
-a ¡¡ ¡¡ Óë
-o¡¡ »ò
!·Ç
shellÖеÄÌØÊâ×Ö·ûÓÐ
1¡¢$ ÃÀÔª·û
2¡¢\ ·´Ð±¸Ü
3¡¢` ·´ÒýºÅ
4¡¢"¡¡Ë«ÒýºÅ
5¡¢,*,?,[,]
ÏÂÃæÎÒÒ»Ò»¾ÙÁÐ˵Ã÷
Ò»¡¢$·ûºÅ
1¡¢echo $? ÏÔʾµÄÊÇÉÏÒ»ÌõÖ¸ÁîÍ˳ö״̬
2¡¢echo "$?" Ч¹ûͬÉÏ
3¡¢echo '$?' ÏÔʾµÄÊÇ$?
4¡¢echo \$? ÏÔʾµÄÊÇ$?
5¡¢echo "\$?" ÏÔʾµÄÊÇ$?
´ó¼Ò¿ÉÄÜÒѾ­¿´³ö¡¡$·ûºÅÔÚË«ÒýºÅÖоßÓÐÌØÊâÒâÒå¡¡Ë«ÒýºÅ¶Ô$·ûºÅ²»Æð×÷Óöøµ¥ÒýºÅ¿ÉÒÔ½«ÌØÊâ×Ö·ûµÄµÄÌØÊâÒâÒåÆÁ±Îµô£¬Ê¹ÆäÄÜÏÔʾΪ×Ö·û±¾Éí£¬·´Ð±¸ÜÒ²¿ÉÒÔ½«ÌØÊâ×Ö·ûµÄÌØÊ⺬ÒåÆÁ±Îµô£¬Ê¹ÌØÊâ×Ö·ûʧȥÌØÊ⺬Òå¡£
¶þ¡¢\¡¡·´Ð±¸Ü
·´Ð±¸ÜµÄ×÷ÓÃÊǽ«ÌØÊâ·ûºÅ×Ö·ûµÄÌØÊ⺬ÒåÆÁ±Îµô£¬Ê¹Æ仹ÊÇÔ­×Ö·û
A£½1234
echo \$A ÏÔʾΪ$A Èç¹û²»¼Ó\½«ÏÔʾΪ1234
echo \` ÏÔʾΪ`
echo \" ÏÔʾΪ˫ÒýºÅ
echo \\ ÏÔʾΪ\
Èý¡¢` ·´ÒýºÅ
·´ÒýºÅµÄ¹¦ÄÜÊÇÃüÁîÌæ»»£¬½«·´ÒýºÅÖеÄ×Ö·û´®×öΪÃüÁîÀ´Ö´ÐУ¬ÎÒÃÇÔÚÓÃshell±à³Ìʱ¾­³£Óõĵ½¡¡½«ÏµÍ³ÃüÁîµÄÖ´Ðнá¹û¸³¸øÒ»¸ö±äÁ¿
A=`date`
echo $A ÏÔʾµÄ²»ÊÇdate¶øÊǵ±Ê±µÄʱ¼ä´®
±ÈÈçÓÐÒ»ÎļþAµÄÄÚÈÝÈçÏ¡¡
ABCDEFG
1234456
abcdefg
B=`cat A|grep 234`? #
¼ìË÷ÎļþAÖк¬ÓÐ×Ö·û´®234µÄÐÐ
echo $B ½«ÏÔʾΪ1234456
echo "$B" ½«ÏÔʾΪʲô£¿
echo "\$B" ½«ÏÔʾΪʲô£¿¶ÁÕß×Ô¼ºÊÔÊÔ
ËÄ¡¢"
Ë«ÒýºÅ
ÔÚϵͳÖÐÓÐЩÌØÊâ×Ö·û£¬Îª±ÜÃâÒýÓÃÕâЩÌØÊâ×Ö·û¡¡ÍùÍùÓÃË«ÒýºÅ»òµ¥ÒýºÅ½«ÕâЩÌØÊâ×Ö·ûÒýÆðÀ´£¬Ê¹Æä²»¾ßÓÐÌØÊ⺬Òå¡£
µ«ÓÐÒ»²¿·ÖÌØÊâ×Ö·ûÔÚÒýºÅÖл¹ÊǾßÓÐÌØÊ⺬Ò壬ÓÃË«ÒýºÅÒýÆðÀ´ÊDz»Æð×÷Óõġ£±¾ÎÄÖÐËùÁеÄÇ°ËĸöÌØÊâ×Ö·ûÔÚË«ÒýºÅÖл¹ÊÇÌØÊâ×Ö·û¡£ÎªÁËʹÆä²»¾ßÓÐÌØÊ⺬ÒåÒ»ÊÇÓõ¥ÒýºÅÒý½øÀ´¶þÊÇÓÃ\·´Ð±ÏßʹÆäʧȥ×÷Óá£
±ÈÈçÎÒÃÇÏëÔ­ÑùÊä³öÕâЩÌØÊâ×Ö·û
echo """
echo "$"
echo "\"
echo "`"
ÒÔÉϲ»ÊÇÄãËùÆÚÍûµÄ½á¹û£¬ÒòΪ˫ÒýºÅ¶ÔËüÃDz»Æð×÷Óã¬ÄãÖ»ÄÜÕâÑù²ÅÄÜÊä³öÕâЩÌØÊâ×Ö·ûµÄÔ­ÐÎ
echo '"'
echo '$'
echo '\'
echo '`'
»ò
echo "\""
echo "\$"
echo "\\"
echo "\`"
½«·Ö±ðÏÔʾΪ¡¡" $ \ `
Îå¡¢ÆäËüÌØÊâ×Ö·û
´ó¼Ò×¢Òâµ½£¬³ýÁËÇ°ËĸöÌØÊâ×Ö·ûÍ⣬ÎÒ½«ÆäËüµÄÌØÊâ×Ö·û¶¼·ÅÔÚÒ»¿é£¬ÕâÊÇÒòΪǰËĸöÌØÊâ×Ö·ûÔÚË«ÒýºÅÖл¹ÊǾßÓÐÌØÊ⺬Ò壬ËùÒÔµ¥¶ÀÄóöÀ´½²£¬³ý´ËÒÔÍâµÄÌØÊâ×Ö·ûÈç¹ûÄãÒªÊä³öÕâЩÌØÊâ×Ö·ûµÄÔ­ÐΣ¬Äã¾Í¿ÉÒÔÓÃË«ÒýºÅ»òµ¥ÒýºÅÒýÆðÀ´Ê¹ÆäʧȥÌØÊ⺬Òå¡£
,*,?,[,]¶ÔshellÓÐÌØÊ⺬Òå¡¡µ«Äã¿ÉÒÔÓÃË«ÒýºÅÒýÆðÀ´ÊäÈëÕâЩԭÐÎ
Ò»¡¢if
Ìõ¼þÓï¾ä¡¡
¸ñʽ£º
if Ìõ¼þ±í´ïʽ
then #µ±Ìõ¼þΪÕæʱִÐÐÒÔÏÂÓï¾ä
ÃüÁîÁбí
else #Ϊ¼ÙʱִÐÐÒÔÏÂÓï¾ä
ÃüÁîÁбí
fi
if¡¡Óï¾äÒ²¿ÉÒÔǶÌ×ʹÓÃ
if
Ìõ¼þ±í´ïʽ1
then
if Ìõ¼þ±í´ïʽ2
then
ÃüÁîÁбí
else
if Ìõ¼þ±í´ïʽ3
then
ÃüÁîÁбí
else
ÃüÁîÁбí
fi
fi
else
ÃüÁîÁбí
fi
Äã¿ÉÒÔ½øÐжà²ãǶÌס¡Ò»¸öifÓï¾äÒ»¶¨Òª¸úÒ»¸öfi¡¡±íʾ¸Ã²ãÌõ¼þ½áÊø·ñÔò»áÔì³ÉÓï·¨´íÎó½áºÏÇ°Ãæ½²µÄ¡¡¾ÙÀýÈçÏ£º
ÕâÀïÏȽ²Ò»¸öÌõ¼þÓï¾äÖÐÓõ½µÄÃüÁîtest¡¡±íʾ²âÊÔtestºóÃæµÄÌõ¼þÊÇ·ñΪÕæ
if test -f "$1"
then
lpr $1
else
if test -d "$1"
then
cd $1
lpr $1
else
echo "$1²»ÊÇÎļþ»òĿ¼"
fi
fi
ÒÔÉϵÄÀý×Ó»¹¿ÉÒԸijÉÈçÏÂËùʾ
if test -f "$1"
then
lpr $1
elif test -d "$1" #elif¡¡Í¬else if
then
(cd
$1;lpr $1)
else
echo "$1²»ÊÇÎļþ»òĿ¼"
fi??????
ÒÔÉϵÄÀý×Ó²»ÖªÄúÊÇ·ñ¿´¶®ÊÇʲôÒâ˼Âð£¿
¼ÙÈçÎÒÃÇÏÖÔÚ½«Õâ¸öÀý×Ó±£´æΪprfile
chmod +x prfile
Ö´ÐиղŵijÌÐò
./prfile aaa
Õâ¸öÀý×ÓÊǼì²éÄãµÄÊäÈëµÄ²ÎÊýÊÇ·ñÊÇÒ»¸öÎļþ¡¡Èç¹ûÊǾʹòÓ¡¡¡Èç¹ûÊÇÒ»¸öĿ¼¡¡ÏÈתĿ¼ÔÙ´òÓ¡¡¡Èç¹û¼´²»ÊÇÎļþÒ²²»ÊÇĿ¼¸ø³öÌáʾ
¶þ¡¢¶àÖØÌõ¼þ²âÊÔÓï¾äcase
¸ñʽ£º
case ×Ö´®¡¡in
ģʽ) ÃüÁîÁбí;;
ģʽ) ÃüÁîÁбí;;
....
esac
¶àÖØÌõ¼þÓï¾äÊÇÒÔcase
¿ªÊ¼ÒÔesac½áÊø¡¡Öмä¿ÉÒÔÓжà¸öÌõ¼þÁÐ±í¡¡¹¦ÄÜÊDzâÊÔ×Ö´®ºÍºÍÀïÃæµÄģʽÓÐûÓÐÆ¥ÅäµÄ,ÓоÍÖ´ÐÐÀïÃæµÄÃüÁîÁÐ±í¡¡Ä£Ê½Ò²¿ÉÒÔÊÇ*ºÅ¡¡±íʾÈÎÒâ×Ö´®£¬Ã¿¸öģʽÀïÃæµÄ×îºóÒªÐÄ;;Ë«ÒýºÅ½áÊø£¬·ñÔò»á·¢ÉúÓï·¨´íÎó¡£
ÏÖ¾ÙÀýÈçÏ£º
case $1 in
*.c)
cc $1
;;
*.txt)
lpr $1
;;
*)
echo "δ֪µÄÀàÐÍ"
esac
¼ÙÈ罫ÒÔÉÏÄÚÈݱ£´æÔÚÎļþabcÖÐ
chmod +x abc
Ö´ÐС¡./abc a.c
¡¡ ½«»á¶ÔÎļþa.c½øÐбàÒë
Ö´ÐÐ ./abc readme.txt¡¡½«»á°ÑÎļþͨ¹ý´òÓ¡»ú
¼ÙÈçÎÒ½«ÒÔÉÏÄÚÈݸÄһϣ¬ÄãÊÇ·ñ»áÖªµÀËüµÄÖ´Ðнá¹û£¿
case $1 in
*)
cc $1
;;
*.txt)
lpr $1
;;
*.c)
echo
"δ֪µÄÀàÐÍ"
esac
Ò». while Ñ­»·
while ÃüÁî¸ñʽ
while Ìõ¼þ±í
do
ÃüÁî±í
done
Ö´Ðйý³Ì
ShellÊ×ÏÈÖ´ÐÐÌõ¼þ±í,Èç¹ûÌõ¼þ±íµÄ×îºóÒ»ÌõÓï¾äµÄÍ˳ö״̬ΪÁã,ÔòÖ´Ðжܻ·ÌåÄÚµÄÃüÁî±í,Ö´ÐÐÍêºó,ÔÙ¼ì²éÌõ¼þ±í,Èç¹ûÍ˳ö״̬ΪÁ㽫¼ÌÐøÖ´ÐÐ,Èç´ËÑ­»·Íù¸´Ö±µ½Ìõ¼þ±íµÄ×îºóÒ»ÌõÓï¾äµÄÍ˳ö״̬·ÇÁã.
Í˳ö״̬ΪÁã¾ÍÊÇÌõ¼þΪÕæTrue.
¾ÙÀý˵Ã÷ ¼ÙÈçshellÎļþµÄÄÚÈÝÈçÏÂ:
Sum=0
i=0
while true #trueÊÇϵͳµÄ¹Ø¼ü´Ê ±íʾÕæ
do
i=`expr $i + 1`
Sum=`expr $Sum + $i`
if [ $i = "100" ]
then
break;
fi
done
echo $i $Sum
×îºóÕâ¸ö³ÌÐòÏÔʾµÄÊÇ
100 5050
Õâ¸ö³ÌÐòµÄÔËËã¾ÍÊǽ«1µ½100¼ÓÆðÀ´
ÏÂÃ潫Õâ¸ö³ÌÐòÔٸĶ¯Ò»ÏÂ
Sum=0
i=0
while [ $i != "100" ]
do
i=`expr $i + 1`
Sum=`expr $Sum + $i`
done
echo $i $Sum
¸Ä¶¯ºóµÄ³ÌÐòÔËËã½á¹ûºÍÉÏÃæÊÇÒ»Ñù µ«³ÌÐò±ÈÉÏÃæµÄÒª¼òÁ·
ÔÚÕâ¸öÑ­»·Öл¹¿ÉÒÔÒÔuntil×öΪ²âÊÔÌõ¼þ
ËüÕýºÃÓëwhile²âÊÔµÄÌõ¼þÏà·´,Ò²¾ÍÊǵ±Ìõ¼þΪ¼Ùʱ½«¼ÌÐøÖ´ÐÐÑ­»·ÌåÄÚµÄÓï¾ä,·ñÔò¾ÍÍ˳öÑ­»·Ìå,ÏÂÃ滹ÓÃÕâ¸öÀý×Ó.
Sum=0
i=0
until [ $i = "100" ]
do
i=`expr $i + 1`
Sum=`expr $Sum + $i`
done
echo $i $Sum
µ±i²»µÈÓÚ100ʱѭ»· ¾ÍÊǵ±Ìõ¼þΪ¼Ùʱѭ»·,·ñÔò¾ÍÍ˳ö,¶øµÚÒ»¸öÀý×ÓÊǵ±i²»µÈÓÚ100
ʱѭ»·,Ò²¾ÍÊDzâÊÔÌõ¼þΪÕæʱѭ»·.
¶þ.for Ñ­»·
ÃüÁî¸ñʽ:
for ±äÁ¿ in Ãû×ÖÁбí
do
ÃüÁîÁбí
done
ÕâÀïµÄÃû×ÖÁбíÊÇÒ»¸öÓÉ¿Õ¸ñ·Ö¸ôµÄ×Ö·û´®Áбí,shellÔÚÖ´ÐÐforÑ­»·Ê±Ã¿´ÎÒÀ´Î´ÓÃû×Ö±íÖÐÈ¡³öÒ»¸ö×Ö·û´®¸³¸øÑ­»·±äÁ¿×÷Ϊ±äÁ¿µÄÖµ.
ÔÚдforÓï¾äʱ,Ò²¿ÉÒÔÊ¡ÂÔin Ãû×ÖÁÐ±í²¿·Ö,Õâ±íʾÓõ±Ç°µÄλÖòÎÊýÀ´´úÌæÕâʱµÄÃû×ÖÁбí.
Ãæ¾Ù¸öÀý×Ó£¬±ÈÈçÔÚÄãµÄµçÄÔÖÐÓÐÁ½¸öĿ¼,Ò»¸öÊÇaa,Ò»¸öÊÇbbÔÚÕâÁ½¸öĿ¼ÖÐÓÐ5¸öÏàͬµÄÎļþ,µ«ÆäÖÐÒ»¸öĿ¼ÖеÄÒ»¸ö»ò¶à¸öÎļþ¸Õ¸ÕÐ޸Ĺý,ÏÖÔÚÎÒÍü¼Ç¸Õ²Å¸ÄµÄÊÇÄǼ¸¸öÎļþ
ÁË,ÄÇôÎÒ¿¿ÒÑÖªµÄÐòÂë²éÕÒ¡£
³ÌÐòÈçÏÂ:
for File in a1 a2 a3 a4 a5
do
diff aa/$File bb/$File
done
ÏÂÃæÔÙ¾ÙÒ»¸ö²»´øÃû×ÖÁбíµÄÀý×Ó
for
File
do
echo $Filw
done
ÎļþÄÚÈݱ£´æÔÚa.shÖÐ ²¢¿ÉÖ´ÐÐ
ÎÒÃÇÔÚÖ´ÐÐÕâ¸öshell³ÌÐòʱÃüÁîÐÐÈçÏÂ:
a.sh a1 a2 a3 a4 a5
Ö´Ðнá¹ûÈçÏÂ:
a1
a2
a3
a4
a5
´ó¼Ò´ÓÕâ¸öÀý×ÓÖпÉÒÔ¿´µ½ÃüÁîÐеIJÎÊý±»ÖðÒ»¶ÁÈëÒ»´Î
Èý.Ñ­»·¿ØÖÆÓï¾ä
break
ÃüÁî²»Ö´Ðе±Ç°Ñ­»·ÌåÄÚbreakÏÂÃæµÄÓï¾ä´Óµ±Ç°Ñ­»·Í˳ö.
continue
ÃüÁîÊdzÌÐòÔÚ±¾Ñ­ÌåÄÚºöÂÔÏÂÃæµÄÓï¾ä,´ÓÑ­»·Í·¿ªÊ¼Ö´ÐÐ.
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 3 ÌõÆÀÂÛ

  1. bzhao ÓÚ 2008-12-06 22:54:52·¢±í:

    ÎÒÔÚUbutuÏ£º
    echo --$TZ---
    -----
    ʲôԭÒò£¿

  2. fengyumoce ÓÚ 2008-10-24 20:12:19·¢±í:

    ¶ÔÓÚÐÂÊÖÀ´ËµºÜºÃ ÏÈϵ½µçÄÔÉÏ ÕÒ¸öʱ¼äÂýÂý×ÁÄ¥

  3. wufaqian ÓÚ 2008-10-16 20:55:17·¢±í:

    bu cuoa