Linux 版 (精华区)

发信人: xian (我想用心来点亮希望), 信区: Linux
标  题: Linux程式设计入门 - bash, Shell Scripts 
发信站: 紫 丁 香 (Sun May  2 16:35:00 1999), 转信


网络工作室--知识库:编程技术:Unix编程:Linux程式设计入门:
Linux程式设计入门-bash,ShellScripts
Linux程式设计入门-bash,ShellScripts



众所皆知地,UNIX上以小工具着名,利用许多简单的小工具,来完成原本需要

大量软体开发的工作,这一点特色,使得UNIX成为许多人心目中理想的系统平

台。


在众多的小工具中,ShellScript算得上是最基本、最?nbsp;systemisFreeBSD"

echo"DoFreeBSDstuffhere..."

;;

*)

echo"Unknownsystem:$SYSTEM"

echo"Idon'twhattodo..."

;;

esac



selectname[inword;]dolist;done


select顾名思义就是在word中选择一项


范例


#!/bin/sh


WORD="abc"


selectiin$WORD;do

case$iin

a)

echo"IamA"

;;

b)

echo"IamB"

;;

c)

echo"IamC"

;;

*)

break;

;;

esac

done


执行结果


[foxman@foxmanbash]#./select_demo

1)a

2)b

3)c

#?1

IamA

1)a

2)b

3)c

#?2

IamB

1)a

2)b

3)c

#?3

IamC

1)a

2)b

3)c

#?4

[foxman@foxmanbash]#




iflistthenlist[eliflistthenlist]...[elselist]fi


几种可能的写法


iflistthen

dosomethinghere

fi


iflistthen

dosomethinghere

else

dosomethingelsehere

fi


iflistthen

dosomethinghere

eliflistthen

doanotherthinghere

fi


iflistthen

dosomethinghere

eliflistthen

doanotherthinghere

else

dosomethingelsehere

fi










这里要迁扯到ExitStatus的问题,等我将ExitStatus的问题说明完再回来继

续。



whilelistdolistdone/untillistdolistdone


范例一:无限圈写法


#!/bin/sh


while:;do

echo"dosomethinghere"

sleep5

done


范例二














这里要迁扯到ExitStatus的问题,等我将ExitStatus的问题说明完再回来继

续。



[function]name(){list;}


范例


functionfunc(arg1,arg2){

echo$arg1

echo$arg2

return1

}


类同於Pascal中的function。


bash内建指令集


.filename[arguments]

sourcefilename[arguments]


由filename中读取命令,并执行。

您会在/etc/rc.d/*中发现很多

./xxxx

的指令,而xxxx的permission都不是可执行的。事实上,在tcsh中,需要用

source/xxxx

来做同样的指令。

注意到"."的後面是有空格的。filename是内含指令的纯文字档即可,无须

chmod755filename。


范例


filename:my_source


DEV=lo

IP=127.0.0.1

NETMASK=255.0.0.0

BROADCAST=127.255.255.255


ifconfig$IPnetmask$NETMASKbroadcast$BROADCASTdev$DEV


接下来

./path/my_source



sourcemy_source


便可执行该script,而不需要"chmod755my_source"


alias[name[=value]...]




例如您如果来自DOS的世界,对UNIX的指令不习惯,可用alias来修改,以符合

您的习惯。


范例


aliasls="ls--color"

aliasdir="ls"

aliascd..="cd.."

aliascopy="cp-f"#dangerous,recommend,cp-i

aliasdel="rm-f"#dangerous,recommend,rm-i

aliasmove="mv-f"#dangerous,recommend,mv-i

aliasmd="mkdir"

aliasrd="rmdir"


unalias[-a][name...]


unalias取消alias的设定。unalias-a将全部alias取消。


范例


unaliascopy


bg[jobspec]

fg[jobspec]


jobs[-lnp][jobspec...]

jobs-xcommand[args...]






kill[-ssigspec|-sigspec][pid|jobspec]...

kill-l[signum]




wait[n]


bind[-mkeymap][-lvd][-qname]

bind[-mkeymap]-ffilename

bind[-mkeymap]keyseq:function-name


break[n]


控制圈中使用


范例




continue[n]


控制圈中使用


范例


exit[n]


离开程式。n是ExitStatus。


return[n]


在function中使用。n为返回值,其作用与ExitStatus一样。


builtinshell-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][-ooption][arg...]


unset[-fv][name...]


fc[-eename][-nlr][first][last]

fc-s[pat=rep][cmd]


getoptsoptstringname[args]


hash[-r][name]


help[pattern]


history[n]

history-rwan[filename]


letarg[arg...]


local[name[=value]...]


logout


popd[+/-n]


pushd[dir]

pushd+/-n


pwd


read[-r][name...]


readonly[-f][name...]

readonly-p


shift[n]


suspend[-f]


testexpr

[expr]


times


trap[-l][arg][sigspec]


type[-all][-type|-path]name[name...]


ulimit[-SHacdfmstpnuv[limit]]


umask[-S][mode]



bash内建叁数


bash的内建叁数很多,你可以自行"manbash"查一查。这里我只说明一些常用

及重要的。


PPID:该bash的呼叫者processID.


PWD:目前的工作目录。


OLDPWD:上一个工作目录。


HOSTTYPE:机器种类。


OSTYPE:作业系统名称。


PATH:命令搜寻路径。



PATH="/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:."


HOME:目前使用者的homedirectory;


PS1:Thevalueofthisparameterisexpanded(seePROMPTING

below)andusedastheprimarypromptstring.The

default

valueis``bash\$''.


PS2:Thevalueofthisparameterisexpandedandusedasthe

secondarypromptstring.Thedefaultis``>''.


PS3:Thevalueofthisparameterisusedasthepromptforthe

selectcommand(seeSHELLGRAMMARabove).


PS4:Thevalueofthisparameterisexpandedandthevalueis

printedbeforeeachcommandbashdisplaysduringan

execu-

tiontrace.ThefirstcharacterofPS4isreplicated

mul-

tipletimes,asnecessary,toindicatemultiplelevels

of

indirection.Thedefaultis``+''.



OKSTATION,Webmaster,BrianLin

admin@studio.openunix.org
[AppendtoThisAnswer]

--
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 202.118.239.115]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.374毫秒