分享
 
 
 

Linux程式设计入门 - bash, Shell Scripts

王朝system·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

众所皆知地,UNIX上以小工具着名,利用许多简单的小工具,来完成原本需要大量软体开发的工作,这一点特色,使得UNIX成为许多人心目中理想的系统平台。

在众多的小工具中,Shell Script算得上是最基本、最?nbsp;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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有