| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > SHELL

csh shell编程入门

时间:2006-10-20 14:04:17  来源:Linux联盟收集  作者:
d9JLinux联盟
输入项,-v选项显示每一行的输入项入口,-c选项将清除目录列表,-S选项可以把目录d9JLinux联盟
列表作为一系列的cd和pushed命令保存在文件中,-L选项可以从指定的文件中将cdd9JLinux联盟
和pushed命令读入,该文件包含-S选项所存储的cd和pushed命令.如果没有指定的文件d9JLinux联盟
名,将使用赋值到dirsfile shell变量中的文件名.如果没有设置dirsfile,将使用d9JLinux联盟
~/.cshdirs,在启动时注册shell将对dirs -L求值,如果设置了savedirs,退出前使用d9JLinux联盟
dirs -S,由于在~/.cshdirs之前,仅仅~/.tcshrc是正常来源,dirsfile应该以~/.tcshrcd9JLinux联盟
设置而不是以~/.login设置.d9JLinux联盟
d9JLinux联盟
11,echod9JLinux联盟
格式:echo [-n] word/stringd9JLinux联盟
此命令把每个单词或字符串写入shell的标准输出.可设置echostyle shell变量来仿真d9JLinux联盟
选项以及BSD的换码序列或者echo的System V 版本.d9JLinux联盟
d9JLinux联盟
12,evald9JLinux联盟
格式:eval argument...d9JLinux联盟
此命令把参数作为shell的输入,执行当前shell的上下文中的结果命令,由于分析发声d9JLinux联盟
在替换前,所以该命令通常用于执行命令或变量替代所产生的命令.d9JLinux联盟
d9JLinux联盟
13,execd9JLinux联盟
格式:exec commandd9JLinux联盟
此命令代替当前的shell执行指定的命令,并退出终端.d9JLinux联盟
d9JLinux联盟
14,exitd9JLinux联盟
格式: exit [expression]d9JLinux联盟
shell可以带指定的表达式的值退出,如果没有包含表达式,也可带状态变量值退出.d9JLinux联盟
d9JLinux联盟
15,fgd9JLinux联盟
格式:fg [%job...]d9JLinux联盟
把指定的任务带到前台,如果任务在终止状态,在带到前台的同时使他运行.d9JLinux联盟
job参数是使用任务号或者下列字符串之一的任务引用:' % + -d9JLinux联盟
d9JLinux联盟
16,foreachd9JLinux联盟
格式:foreach variable(list of values)d9JLinux联盟
commandsd9JLinux联盟
endd9JLinux联盟
foreach结构被设置为顺序引用数值列表,它类似BASH shell的for in 结构.foreachd9JLinux联盟
结构产生两个操作数:一个变量和一组包含在()中的数值列表.将列表中的值赋值到d9JLinux联盟
结构中的变量中.循环体的结尾由语句end构成.下例中脚本list输出由项目和foreachd9JLinux联盟
循环读取每一项当前的日期构成的一行信息,列表中的每一项被连续赋值到变量objectd9JLinux联盟
listd9JLinux联盟
#d9JLinux联盟
set tdate=`date +%D`d9JLinux联盟
foreach object(milk cookies apples cheese)d9JLinux联盟
echo $object $tdated9JLinux联盟
endd9JLinux联盟
%tcsh listd9JLinux联盟
milk 04/26/03d9JLinux联盟
cookies 04/26/03d9JLinux联盟
apples 04/26/03d9JLinux联盟
cheese 04/26/03d9JLinux联盟
使用foreach循环有助于管理文件,可以模式中的shell特定字符来产生用作数值列表d9JLinux联盟
的文件名列表,然后所产生的文件名列表就成为foreach结构引用的列表.如果在列表d9JLinux联盟
中不带任何变量,可以使用命令行参数实现循环.d9JLinux联盟
listd9JLinux联盟
#set tdate=`date +D%`d9JLinux联盟
foreach object($argv d9JLinux联盟
)d9JLinux联盟
echo "$object $tdate"d9JLinux联盟
endd9JLinux联盟
%tcsh list a b cd9JLinux联盟
a 04/26/03d9JLinux联盟
b 04/26/03d9JLinux联盟
c 04/26/03d9JLinux联盟
使用argv d9JLinux联盟
特殊参数变量,可以直接引用命令行参数.下例中,当调用shell脚本d9JLinux联盟
cbackuparg时,在命令行下输入C程序文件的列表,在foreach循环中,argv d9JLinux联盟
引用d9JLinux联盟
命令行中的所有参数,将按顺序把各个参数赋值给变量backfile,变量argnum用于d9JLinux联盟
引用每个参数.显示参数和backfile的值来说明它们两个是相同的.d9JLinux联盟
d9JLinux联盟
d9JLinux联盟
源码:--------------------------------------------------------------------------------cbackupargd9JLinux联盟
#d9JLinux联盟
@ argnum=1d9JLinux联盟
foreach backfile ($argv d9JLinux联盟
)d9JLinux联盟
   cp $backfile sourcebak/$backfiled9JLinux联盟
   echo "$backfile $argv[$argnum]"d9JLinux联盟
   @ argnum=$argnum+1d9JLinux联盟
endd9JLinux联盟
--------------------------------------------------------------------------------d9JLinux联盟
d9JLinux联盟
%cbackuparg main.c lib.c io.cd9JLinux联盟
main.c main.cd9JLinux联盟
lib.c lib.cd9JLinux联盟
io.c io.cd9JLinux联盟
d9JLinux联盟
17,historyd9JLinux联盟
格式:d9JLinux联盟
history [-hr][n]d9JLinux联盟
history -S|-L|M [filename]d9JLinux联盟
history -cd9JLinux联盟
如果history没有任何参数,将显示开行号的历史命令列表.用作参数的号码将列出d9JLinux联盟
行的最后号码.如果没有任何选项参数,她将用作历史文件名;否则,将使用hisfiled9JLinux联盟
变量值.d9JLinux联盟
用-h选项,将以注释的形式显示不带行号的历史文件列表d9JLinux联盟
用-r选项,将反向显示,以最近的列表开始d9JLinux联盟
用-c选项,则清除历史列表d9JLinux联盟
如果用-S选项,可以把历史列表存入文件,如果savehist shell变量的第一个单词d9JLinux联盟
被设置为数值,历史文件将被保存为最大的行数,如果第二单词为'merge',历史列表d9JLinux联盟
将被合并到当前的历史文件中,而不替代历史列表,时间图章排序历史列表.d9JLinux联盟
如果用-L选项,shell将从存储的历史列表文件中读取历史列表并把她追加到当前d9JLinux联盟
历史列表中.d9JLinux联盟
如果用-M选项,将从历史文件读取历史列表信息,但用当前历史列表信息合并,排序d9JLinux联盟
历史列表.d9JLinux联盟
如果没有用这些选项给出文件名,那么,就使用赋值到histfile shell变量的文件d9JLinux联盟
名,如果没有设置histfile,就使用~/.history.d9JLinux联盟
d9JLinux联盟
18,hupd9JLinux联盟
格式:d9JLinux联盟
hup [command]d9JLinux联盟
如果带有括起来的command参数,hup运行command,根据停机信号退出.当shell退出时,d9JLinux联盟
hup安排shell发出停机信号.注意:命令也许设置自己的停机响应,重写hup,如果没有d9JLinux联盟
参数(只有在shell脚本中才允许),hup使shell根据脚本的余项的停机信号退出.d9JLinux联盟
d9JLinux联盟
19,if-thend9JLinux联盟
格式:d9JLinux联盟
if (expression) thend9JLinux联盟
commandd9JLinux联盟
endifd9JLinux联盟
if-then结构把条件放入几个linux命令,该条件是expression,如果expression得出d9JLinux联盟
非零的数值,那么expression为真,执行if结构内的命令,如果expression得出零值d9JLinux联盟
那么expression为假,就不执行if结构内的命令.d9JLinux联盟
if-then结构以if关键词开始,expression表达式用()括起来,关键词then后可以跟d9JLinux联盟
任意数量的linux命令,以关键词endif结束if命令.注意:在TCSH中,if (expressiong)d9JLinux联盟
和then必须在同一行!d9JLinux联盟
举例:d9JLinux联盟
源码:--------------------------------------------------------------------------------d9JLinux联盟
iflsd9JLinux联盟
#d9JLinux联盟
echo -n "Please enter option:"d9JLinux联盟
set option=$<d9JLinux联盟
if ($option=="s") thend9JLinux联盟
   echo List files by sized9JLinux联盟
   ls -sd9JLinux联盟
endifd9JLinux联盟
--------------------------------------------------------------------------------d9JLinux联盟
d9JLinux联盟
%tcsh iflsd9JLinux联盟
Please enter option: Sd9JLinux联盟
List files by sized9JLinux联盟
total 2d9JLinux联盟
1 monday 2 todayd9JLinux联盟
%d9JLinux联盟
d9JLinux联盟
20,if-then-elsed9JLinux联盟
格式:d9JLinux联盟
if (expressiong) thend9JLinux联盟
commandd9JLinux联盟
elsed9JLinux联盟
commandd9JLinux联盟
endifd9JLinux联盟
用户需要经常需要根据expression是真还是假来进行选择.关键词else允许if结构d9JLinux联盟
在两者之间选择,如果expression为真,那么执行第一个command,否则,执行第二个d9JLinux联盟
就是else后面的command.d9JLinux联盟
举例:d9JLinux联盟
源码:--------------------------------------------------------------------------------d9JLinux联盟
elselsd9JLinux联盟
#d9JLinux联盟
echo Enter s to list file sizesd9JLinux联盟
echo otherwise all file information is listedd9JLinux联盟
echo -n "please enter option:"d9JLinux联盟
set option=$<d9JLinux联盟
if ($option=="s") thend9JLinux联盟
   ls -sd9JLinux联盟
       elsed9JLinux联盟
   ls -ld9JLinux联盟
endifd9JLinux联盟
echo Good-beyd9JLinux联盟
--------------------------------------------------------------------------------d9JLinux联盟
(结果略)d9JLinux联盟
d9JLinux联盟
21,jobsd9JLinux联盟
格式:d9JLinux联盟
jobs [-l]d9JLinux联盟
此命令列出所有活动的任务,如果带-l选项,将列出进程号,以及正常的信息.d9JLinux联盟
d9JLinux联盟
22,killd9JLinux联盟
格式:d9JLinux联盟
kill [-signal] %job\pid...d9JLinux联盟
kill -ld9JLinux联盟
此命令用于终止进程或者任务,如果带-signal选项,应该指定要发送的信号,默认发送d9JLinux联盟
的信号是SIGTERM,如果带有-l选项,则列出信号名,对于带-l选项提供的信号数,将d9JLinux联盟
列出与其相关的信号名.d9JLinux联盟
d9JLinux联盟
23,logoutd9JLinux联盟
此命令用于终止注册shell,如果设置了ignoreeof,它将非常有用.d9JLinux联盟
d9JLinux联盟
24,niced9JLinux联盟
格式:d9JLinux联盟
nice [+ number][command]d9JLinux联盟
此命令设置shell调整优先为number,如果没有设置number,调整优先设置为4,带有d9JLinux联盟
指定的command,nice适当的优先运行command,number值越大,进程获得的CPU的时间d9JLinux联盟
就越少.d9JLinux联盟
d9JLinux联盟
25,nohupd9JLinux联盟
格式:d9JLinux联盟
nohup [command]d9JLinux联盟
如果不带command参数,nohup指示shell忽略任何停机信号,如果带command参数,将d9JLinux联盟
执行此命令并忽略执行中的任何停机信号.d9JLinux联盟
d9JLinux联盟
26,notifyd9JLinux联盟
格式:notify [%job...]d9JLinux联盟
Linux执行命令后,如果有后台任务在执行,系统将通知到目前为止已经完成的后台d9JLinux联盟
任务,该系统不会中断如编辑这样的操作来通知用户关于完成的任务,当某任务完成d9JLinux联盟
时,如果想马上知道,无论系统在作什么,可以使用notify命令指令系统通知用户,它的d9JLinux联盟
参数作为任务号,当任务完成时,系统将中断当前命令并通知用户任务已经完成,如:d9JLinux联盟
%notify %2d9JLinux联盟
当2号任务完成时,告诉系统通知用户.d9JLinux联盟
d9JLinux联盟
27,onintrd9JLinux联盟
格式 onintr [-|label]d9JLinux联盟
此命令控制shell在中断时的动作,不带任何参数,将回复shell中断默认的动作,将d9JLinux联盟
终止shell脚本或返回输入级的终止符,如果带-参数,则忽略全部的中断,当接受中断d9JLinux联盟
或当子进程终止时,带label的命令将使shell执行goto标号.d9JLinux联盟
d9JLinux联盟
28.popdd9JLinux联盟
格式 popd [-p][-l][-n|-v][+n]d9JLinux联盟
此命令从目录列表中删除一个目录,不带参数的命令从列表中删除顶层目录,+n删除d9JLinux联盟
从左起的第n层的目录.然后popd显示最终的目录列表.pushdsilent shell变量可以d9JLinux联盟
设置为支持此特性,-p选项可以重写pushdsilent.d9JLinux联盟
d9JLinux联盟
29.printenvd9JLinux联盟
格式:d9JLinux联盟
printenv [name]d9JLinux联盟
此命令显示环境变量的名和值,如果带name参数,仅仅显示环境变量name的值.d9JLinux联盟
d9JLinux联盟
30.pushdd9JLinux联盟
格式:d9JLinux联盟
pushd [-p][-l][-n|-v][name|+n]d9JLinux联盟
此命令把目录添加到存储的目录列表中,然后显示目录列表,如不带参数,除非目录d9JLinux联盟
为空,pushd交换顶层的两个目录,并返回0.d9JLinux联盟
+n旋转列表以便使第n个目录(从左起)列与顶端,但是,如果设置了dextract,pushd +nd9JLinux联盟
将展开第n个目录,把它压入堆栈顶层.d9JLinux联盟
-n旋转堆栈以便第n个目录(从右起)列于顶端,dir把dir添加到顶端的目录列表,是她d9JLinux联盟
成为新的当前目录.如果设置了pushtohome,不带参数的pushd命令执行pushd ~的d9JLinux联盟
功能,像cd的功能一样,如果dunique被设置,pushd在压入堆栈之前从堆栈中删除任何d9JLinux联盟
的name历程,可以设置pushdsilent shell变量来取消目录列表的显示,然后可以使用d9JLinux联盟
-p选项重写pushdsilent.d9JLinux联盟
d9JLinux联盟
31,repeatd9JLinux联盟
格式:d9JLinux联盟
repeat count commandd9JLinux联盟
此命令重复执行command指定的次数count.d9JLinux联盟
d9JLinux联盟
32,setd9JLinux联盟
格式:d9JLinux联盟
setd9JLinux联盟
set name ...d9JLinux联盟
set name = value ...d9JLinux联盟
set name = (wordlist)...d9JLinux联盟
set name[index] = word ...d9JLinux联盟
set -rd9JLinux联盟
set -r name ...d9JLinux联盟
set -r name = value ...d9JLinux联盟
set -r name = (wordlist) ...d9JLinux联盟
不带任何参数的set命令将显示全部的shell变量值,包含许多单词的变量作为放在d9JLinux联盟
括号中的词列出,带name参数的set命令定义一个变量并为它赋值null串,带有named9JLinux联盟
和=符号分隔的值,set定义变量并给它赋值,要把wordlist作为值赋值给name变量,d9JLinux联盟
把列表中的词作为要赋的值放在括号中,为了将数值赋值给数组元素,,使用方括号d9JLinux联盟
[]指定元素的index,但是该元素必须已经存在.d9JLinux联盟
-r选项常用于引用只读变量,仅仅带-r选项的set命令将列出只读变量,与变量名一起d9JLinux联盟
使用,set将使此变量设置为只读,与赋值的变量一起使用,将初始化该变量,并使该d9JLinux联盟
变量成为不能被修改的只读变量.d9JLinux联盟
在TCSH中,用户必须在使用变量之前首先声明它,使用加变量名的set命令声明变量.d9JLinux联盟
变量名可以是任何字母字符包括下划线_,也可以含有数字,但是变量名不能以数字为d9JLinux联盟
首字符!d9JLinux联盟
d9JLinux联盟
33,setenvd9JLinux联盟
格式:d9JLinux联盟
setenv [name[value]]d9JLinux联盟
setenv常用于定义有特定值的环境变量.如没有带value选项,setenv设置name变量d9JLinux联盟
为null串,如果没有带任何参数,将显示全部环境变量的名称和值.d9JLinux联盟
TCSH有两种类型的变量:局部变量和环境变量.局域变量是在shell内部声明的;环境d9JLinux联盟
变量是全局域的变量.使用setenv可以定义环境变量,使用setenv命令,变量名,以及d9JLinux联盟
被赋值的值,就可给环境变量赋值.其中,没有赋值运算符,如:d9JLinux联盟
%setenv greeting hellod9JLinux联盟
greeting环境变量被赋值为hellod9JLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门