📄 1042.html
字号:
esac <br>
<br>
<br>
Linux程式设计-11.Shell Script(bash)--(7)流程控制select <br>
http://www.openchess.org/noitatsko/programming/ (2001-05-25 14:08:00) <br>
select name [ in word; ] do list ; done <br>
select顾名思义就是在word中选择一项。与for相同,如果[in word;]省略,将会使用Script後面所加的参数。 <br>
例 <br>
#!/bin/sh <br>
WORD="a b c" <br>
<br>
select i in $WORD ; do <br>
case $i in <br>
a) <br>
echo "I am A" <br>
;; <br>
b) <br>
echo "I am B" <br>
;; <br>
c) <br>
echo "I am C" <br>
;; <br>
*) <br>
break; <br>
;; <br>
esac <br>
done <br>
<br>
执行结果 <br>
[foxman@foxman bash]# ./select_demo <br>
1) a <br>
2) b <br>
3) c <br>
#? 1 <br>
I am A <br>
1) a <br>
2) b <br>
3) c <br>
#? 2 <br>
I am B <br>
1) a <br>
2) b <br>
3) c <br>
#? 3 <br>
I am C <br>
1) a <br>
2) b <br>
3) c <br>
#? 4 <br>
<br>
<br>
Linux程式设计-11.Shell Script(bash)--(8)返回状态Exit <br>
http://www.openchess.org/noitatsko/programming/ (2001-05-25 15:00:00) <br>
在继续下去之前,我们必须要切入另一个话题,即返回状态值 - Exit Status。因为if/while/until都迁涉到了使用Exit Status来控制程式流程的问题。 <br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
许多人都知道,在许多语言中(C/C++/Perl....),都有一个exit的函数,甚至连Bash自己都有个exit的内建命令。而exit後面所带的数字,便是返回状态值 - Exit Status。 <br>
返回状态值可以使得程式与程式之间,利用Shell script来结合的可能性大增,利用小程式,透过Shell script,来完成很杂的工作。 <br>
<br>
在shell中,返回值为零表示成功(True),非零值为失败(False)。 <br>
<br>
<br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
举例来说,以下这个两个小程式yes/no分别会返回0/1(成功/失败): <br>
/* yes.c */ <br>
void main(void) { exit(0); } <br>
/* no.c */ <br>
void main(void) { exit(1); } <br>
那麽以下这个"YES"的shell script便会显示"YES"。 <br>
#!/bin/sh <br>
# YES <br>
if yes ; then <br>
echo "YES" <br>
fi <br>
而"NO"不会显示任何东西。 <br>
#!/bin/sh <br>
# NO <br>
if no ; then <br>
echo "YES" <br>
fi <br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
test express <br>
[ express ] <br>
在Shell script中,test express/[ express ]这个语法被大量地使用,它是个非常实用的指令。由於它的返回值即Exit Status,经常被运用在if/while/until的场合中。而在後面,我们也会大量运用到,在进入介绍if/while/until之前,有必要先解一下。 <br>
<br>
其返回值为0(True)或1(False),要看表述(express)的结果为何。 <br>
<br>
express格式 <br>
<br>
-b file : 当档案存在并且属性是Block special(通常是/dev/xxx)时,返回True。 <br>
-c file : 当档案存在并且属性是character special(通常是/dev/xxx)时,返回True。 <br>
-d file : 当档案存在并且属性是目录时,返回True。 <br>
-e file : 当档案存在时,返回True。 <br>
-f file : 当档案存在并且是正常档案时,返回True。 <br>
-g file : 当档案存在并且是set-group-id时,返回True。 <br>
-k file : 当档案存在并且有"sticky" bit被设定时,返回True。 <br>
-L file : 当档案存在并且是symbolic link时,返回True。 <br>
-p file : 当档案存在并且是name pipe时,返回True。 <br>
-r file : 当档案存在并且可读取时,返回True。 <br>
-s file : 当档案存在并且档案大小大於零时,返回True。 <br>
-S file : 当档案存在并且是socket时,返回True。 <br>
-t fd : 当fd被开启为terminal时,返回True。 <br>
-u file : 当档案存在并且set-user-id bit被设定时,返回True。 <br>
-w file : 当档案存在并且可写入时,返回True。 <br>
-x file : 当档案存在并且可执行时,返回True。 <br>
-O file : 当档案存在并且是被执行的user id所拥有时,返回True。 <br>
-G file : 当档案存在并且是被执行的group id所拥有时,返回True。 <br>
file1 -nt file2 : 当file1比file2新时(根据修改时间),返回True。 <br>
file1 -ot file2 : 当file1比file2旧时(根据修改时间),返回True。 <br>
file1 -ef file2 : 当file1与file2有相同的device及inode number时,返回True。 <br>
-z string : 当string的长度为零时,返回True。 <br>
-n string : 当string的长度不为零时,返回True。 <br>
string1 = string2 : string1与string2相等时,返回True。 <br>
string1 != string2 : string1与string2不相等时,返回True。 <br>
! express : express为False时,返回True。 <br>
expr1 -a expr2 : expr1及expr2为True。 <br>
expr1 -o expr2 : expr1或expr2其中之一为True。 <br>
arg1 OP arg2 : OP是-eq[equal]、-ne[not-equal]、-lt[less-than]、-le[less-than-or-equal]、-gt[greater-than]、-ge[greater-than-or-equal]的其中之一。 <br>
<br>
<br>
<br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
在Bash中,当错误发生在致命信号时,bash会返回128+signal number做为返回值。如果找不到命令,将会返回127。如果命令找到了,但该命令是不可执行的,将返回126。除此以外,Bash本身会返回最後一个指令的返回值。若是执行中发生错误,将会返回一个非零的值。 <br>
Fatal Signal : 128 + signo <br>
Can't not find command : 127 <br>
Can't not execute : 126 <br>
Shell script successfully executed : return the last command exit status <br>
Fatal during execution : return non-zero <br>
<br>
<br>
Linux程式设计-11.Shell Script(bash)--(9)流程控制if <br>
http://www.openchess.org/noitatsko/programming/ (2001-05-25 16:10:00) <br>
<br>
if list then list [ elif list then list ] ... [ else list ] fi <br>
几种可能的写法 <br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
第一种 <br>
if list then <br>
do something here <br>
fi <br>
当list表述返回值为True(0)时,将会执行"do something here"。 <br>
<br>
例一 : 当我们要执行一个命令或程式之前,有时候需要检查该命令是否存在,然後才执行。 <br>
if [ -x /sbin/quotaon ] ; then <br>
echo "Turning on Quota for root filesystem" <br>
/sbin/quotaon / <br>
fi <br>
<br>
例二 : 当我们将某个档案做为设定档时,可先检查是否存在,然後将该档案设定值载入。 <br>
# Filename : /etc/ppp/settings <br>
PHONE=1-800-COLLECT <br>
<br>
#!/bin/sh <br>
# Filename : phonebill <br>
if [ -f /etc/ppp/settings ] ; then <br>
source /etc/ppp/settings <br>
echo $PHONE <br>
fi <br>
执行 <br>
[foxman@foxman ppp]# ./phonebill <br>
1-800-COLLECT <br>
<br>
<br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
第二种 <br>
if list then <br>
do something here <br>
else <br>
do something else here <br>
fi <br>
例三 : Hostname <br>
#!/bin/sh <br>
if [ -f /etc/HOSTNAME ] ; then <br>
HOSTNAME=`cat /etc/HOSTNAME` <br>
else <br>
HOSTNAME=localhost <br>
fi <br>
<br>
<br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
第三种 <br>
if list then <br>
do something here <br>
elif list then <br>
do another thing here <br>
fi <br>
例四 : 如果某个设定档允许有好几个位置的话,例如crontab,可利用if then elif fi来找寻。 <br>
#!/bin/sh <br>
<br>
if [ -f /etc/crontab ] ; then <br>
CRONTAB="/etc/crontab" <br>
elif [ -f /var/spool/cron/crontabs/root ] ; then <br>
CRONTAB="/var/spool/cron/crontabs/root" <br>
elif [ -f /var/cron/tabs/root ] ; then <br>
CRONTAB="/var/cron/tabs/root" <br>
fi <br>
export CRONTAB <br>
<br>
<br>
<br>
-------------------------------------------------------------------------------- <br>
<br>
第四种 <br>
if list then <br>
do something here <br>
elif list then <br>
do another thing here <br>
else <br>
do something else here <br>
fi <br>
例五 : 我们可利用uname来判断目前系统,并分别做各系统状况不同的事。 <br>
#!/bin/sh <br>
<br>
SYSTEM=`uname -s` <br>
<br>
if [ $SYSTEM = "Linux" ] ; then <br>
echo "Linux" <br>
elif [ $SYSTEM = "FreeBSD" ] ; then <br>
echo "FreeBSD" <br>
elif [ $SYSTEM = "Solaris" ] ; then <br>
echo "Solaris" <br>
else <br>
echo "What?" <br>
fi <br>
<br>
<br>
</FONT><br>
</TD>
</TR>
<TR>
<TD colSpan=2><FONT
class=middlefont></FONT><BR>
<FONT
class=normalfont>全文结束</FONT> </TD>
</TR>
<TR>
<TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2
height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="20%"
background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2>
<DIV align=center>
<table class=tableoutline cellspacing=1 cellpadding=4
width="100%" align=center border=0>
<tr class=firstalt>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
<font class=normalfont><b>所有分类</b></font></td>
</tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td> </tr> </table></td></tr> </table>
</DIV></TD></TR>
<TR vAlign=top>
<TD width="80%">
<DIV align=center><BR>
</DIV>
</TD></TR></TBODY></TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee
border=0><TBODY>
<TR>
<TD width="50%">
<P><FONT class=middlefont>版权所有 © 2004 <A
href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
违者必究. </FONT></P>
</TD>
<TD width="50%">
<DIV align=right><FONT class=middlefont>Powered by: <A
href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
</TD></TR></TBODY></TABLE>
<CENTER></CENTER></TD></TR>
</TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -