一叶笑天
雄关漫道真如铁, 而今迈步从头越。 从头越, 苍山如海, 残阳如血。
posts - 73,comments - 7,trackbacks - 0

2.1 快速浏览Shell脚本:
C shell 和 TC shell 类似于C语言语法
Bourne shell 是基于Algol语言
Bash和Korn shells 混合了Bourne和C shells, 但是起源于Bourne shell.
2.3 C 和TC Shell 语法结构

shbang行

"shbang" 行告诉采用哪个shell来解析脚本. 首先是#, !, shell的路径 例如:#!/bin/csh or #!/bin/tcsh

注释

#符号。例如: # This is a comment

通配符

 *, ?, and [ ] 用于文件名的扩展.
! 是历史字符
 < , > , >> , <&, and | 用于标准IO的重定向和管道
 为了避免这些符号被shell解析器解释,shell中必须使用\或者"。例如:
rm *; ls ??; cat file[1-3]; !!
echo "How are you?"
echo Oh boy\!

输出显示

echo 输出显示 echo "Hello to you\!"

局部变量

局部变量存在于当前shell中。使用set设置。例如
set variable_name = value
set name = "Tom Jones"

全局变量

全局变量被称为环境变量。例如
setenv VARIABLE_NAME value
setenv PRINTER Shakespeare 

从变量中提取值

为了从变量中提取之,使用$在变量前。例如:
echo $variable_name
echo $name
echo $PRINTER

读取用户输入

使用 $< 读入一行到一个变量中,例如:
echo "What is your name?"
set name = $<
           


参数

可以从命令行中传入参数。两种方式接受这些参数值:一个是位置参数,另外一个是argv数组。例如:
% scriptname arg1 arg2 arg3 ...         

使用位置参数:

echo $1 $2 $3

arg1 指定为$1, arg2 to $2, etc.

echo $*

所有的参数

使用argv数组:

echo $argv[1] $argv[2] $argv[3]

 

echo $argv[*]

所有参数

echo $#argv

参数号

数组

数组是一列被空格分隔的字符。使用()把字符都包含进去。
用内建的shift命名可以移出左边的字从list中。
不同于C, index起始值是1而不是0.
例如:

set word_list = ( word1 word2 word3 )

 
set names = ( Tom Dick Harry Fred )
            shift names

从list中删除Tom

echo $word_list[1]
            echo $word_list[2]
            echo $word_list or $word_list[*]
            echo $names[1]
            echo $names[2]
            echo $names[3]
            echo $names or echo $names[*]
            

显示第一个
显示第二个
显示所有

命令替换

使用` `例如:

 
set variable_name=`command` echo $variable_name
 
 
set now = `date`
            echo $now
            echo "Today is `date`"

 

算术

使用@来表示计算结果的保存变量。例如
@ n = 5 + 5
echo $n

操作符

相等性:

==

!=

Relational:

>

greater than

>=

greater than or equal to

<

less than

<=

less than or equal to

逻辑性:

&&

and

||

or

!

nSot

条件语句

if  then. if 必须用endif结尾. 还可以使用if/else。例如:

The if construct is:

if (  expression  ) then
            block of statements
            endif
            

The if/else construct is:

if ( expression ) then
            block of statements
            else
            block of statements
            endif
            

The if/else/else if construct is:

if ( expression ) then
            block of statements
            else if ( expression ) then
            block of statements
            else if ( expression ) then
            block of statements
            else
            block of statements
            endif
            switch ( "$color" )
            case blue:
            echo $color is blue
            breaksw
            case green:
            echo $color is green
            breaksw
            case red:
            case orange:
            echo $color is red or orange
            breaksw
            default:
            echo "Not a valid color"
            endsw
            

switch 结构:

switch variable_name
            case constant1:
            statements
            case constant2:
            statements
            case constant3:
            statements
            default:
            statements
            endsw

循环

两种类型循环语句:whileforeach
循环控制中可以使用breakcontinue.例如:
while ( expression )
        block of statements
end
foreach variable ( word list )
     block of statements
end
 ------------------------------
 foreach color (red green blue)
     echo $color
  end

文件测试

例如:

–r

当前用户能读文件

–w

当前用户能写文件

–x

当前用户能执行文件

–e

文件存在

–o

当前用户拥有文件

–z

文件长度为0

–d

文件是目录

–f

文件是普通文件

2.3.1 C/TC Shell 脚本

Example 2.2.
1   #!/bin/csh –f
2   # The Party Program––Invitations to friends from the "guest" file
3   set guestfile = ~/shell/guests
4   if ( ! –e "$guestfile" ) then
echo "$guestfile:t non–existent"
exit 1
5   endif
6   setenv PLACE "Sarotini's"
7   @ Time = `date +%H` + 1
8   set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
9   foreach person ( `cat $guestfile` )
10      if ( $person =~ root ) continue
11      mail –v –s "Party" $person << FINIS   # Start of here document 
Hi $person!Please join me at $PLACE for a party!
         Meet me at $Time o'clock. I'll bring the ice cream. 
        Would you please bring $food[1] and anything else you would like to eat? 
        Let me know if you can make it. Hope to see you soon.
Your pal,
ellie@`hostname`       # or `uname -n`
12       FINIS
13       shift food
14       if ( $#food ==  0 ) then
set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
endif
15   end
echo "Bye..."
解释:
1.告诉kernel,这是C shell脚本. ”–f“表示快速启动.就是说不要执行.cshrc.
2.注释行
3.变量guestfile 被设置为调用guests的全路径
4.行读入,如果guests不存在,打印 "guests nonexistent" ,退出脚本。
5.endif
6.PLACE 环境变量
7.Time局部变量。@是内建算术。
8.food数组。
9.foreach循环。命令替换`cat $guestfile`.
10.条件测试
11.foreach循环
12.FINIS是用户定义的终结符
13.shift命令取得下一个person
14.如果food为空,将会重置。
15.循环结束标志
posted on 2008-06-19 13:52 一叶笑天 阅读(405) 评论(0)  编辑  收藏 所属分类: Shell技术

只有注册用户登录后才能发表评论。


网站导航: