Chinaunix首页 | 论坛 | 博客
  • 博客访问: 184663
  • 博文数量: 56
  • 博客积分: 132
  • 博客等级: 民兵
  • 技术积分: 313
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-14 17:03
文章分类

全部博文(56)

文章存档

2013年(2)

2012年(54)

分类:

2012-11-12 14:44:52

原文地址:shell编程 作者:ly44770

读鸟哥的私房菜,做的摘录。

1、在编写shell时,第一行一定要指明系统需要那种shell解释你的shell程序,如:
#! /bin/bash,
#! /bin/csh,/bin/tcsh,
还是#! /bin/pdksh .

2、shell中引号的使用方法
shell使用引号(单引号/双引号)和反斜线("\")用于向shell解释器屏蔽一些特殊字符.
反引号(`)对shell则有特殊意义.
如:
abc="how are you" (bash/pdksh)
set abc = "how are you" (tcsh)
这个命令行把三个单词组成的字符串how are you作为一个整体赋值给变量abc.
abc1='@LOGNAME,how are you!' (bash/pdksh)
set abc1='$LOGNAME,how are you!' (tcsh)
abc2="$LOGNAME,how are you!" (bash/pdksh)
set abc2="$LOGNAME,how are you!" (tcsh)
LOGNAME变量是保存当前用户名的shell变量,假设他的当前值是:wang.执行完两条命令后,
abc1的内容是:$LOGNAME, how are you!.而abc2的内容是;wang, how are you!.
象单引号一样,反斜线也能屏蔽所有特殊字符.但是他一次只能屏蔽一个字符.而不能屏蔽
一组字符.
反引号的功能不同于以上的三种符号.他不具有屏蔽特殊字符的功能.但是可以通过他将
一个命令的运行结果传递给另外一个命令.
如:
contents=`ls` (bash/pdksh)
set contents = `ls` (tcsh)


3、read:

变量值是使用者键盘输入的内容
范例:
[test @test test]# read name
testing <==这个时候屏幕会等待使用者由键盘输入!
[test @test test]# echo $name
testing <==刚刚输入的数据变成了变量的内容啦!



4、grep
 
语法:
[root @test /root ]# grep [-acinv] '搜寻字符串' filename
参数说明:
-a :将 binary 档案以 text 档案的方式搜寻数据
-c :计算找到 '搜寻字符串' 的次数
-i :忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
范例:
[root @test /root]# grep 'root' /var/log/secure
将 /var/log/secure 这个档案中有 root 的那一行秀出来
[root @test /root]# grep -v 'root' /var/log/secure
若该行没有 root 才将数据秀出来到屏幕上!
[root @test /root]# last | grep root
若该行有 root 才将数据秀出来到屏幕上!
[root @test /root]# grep [A-Z]ANPATH /etc/man.config
将 /etc/man.config 这个档案当中,所有有:
[任何一个大写字符]后面接 ANPATH 的那一行就显示出来!
例如 AANPATH, BANPATH.... ZANPATH 等等!


5、declare

宣告变量内容
语法:
[test @test test]# declare [-afirx]
参数说明:
-a :定义为数组 array
-f :定义为函数 function
-i :定义为整数 integer
-r :定义为『只读』
-x :定义为透过环境输出变量
范例:
[test @test test]# declare -i a=3
[test @test test]# declare -i b=5
[test @test test]# declare -i c=$a*$b
[test @test test]# echo $c
15 <==变成数字啰!
如果您的计算结果当中,需要输入为 2*3+5*13-32+25 时,
并且在最后输出『 Your result is ==> 』该怎样写这一支简单的 script 呢?可以这样试试看:
[root @test test]# vi test-declare.sh
#!/bin/bash
# This program is used to "declare" variables
# VBird 2002/06/27
number1=2*3+5*13-32+25
declare -i number2=2*3+5*13-32+25
echo "Your result is ==> $number1"
echo "Your result is ==> $number2"
[root @test test]# sh test-declare.sh
Your result is ==> 2*3+5*13-32+25
Your result is ==> 64

6、对谈式 scripts :
什么是对谈式的 scripts 呢?很简单啦!例如你在执行 Windows 的安装程序时,系统不是常常会跳出
一个窗口,问你『下一步』、『上一步』或『取消』吗?那就是对谈啦!程序会依据您输入的数据来
进行判断,OK!那么最简单的对谈式指令是什么呢?呵呵!就是 read 这个指令啦! read 的功能就是
『依据您在键盘输入的结果 input 到变量内容中』,例如:
[root @test test]# read name
VBird <==这是键盘输入的结果
[root @test test]# echo $name
VBird
好了!那么我们来设定一下,当您的 script 在执行的时候,将您由键盘输入的数据列出来!如何做呢?
[root @test test]# vi test04-read.sh
#!/bin/bash
# This program is used to "read" variables
# VBird 2002/06/27
echo "Please keyin your name, and press Enter to start."
read name
echo "This is your keyin data ==> $name"
[root @test test]# sh test04-read.sh
Please keyin your name, and press Enter to start.
VBird Tsai
This is your keyin data ==> VBird Tsai

下一步我们再来说一说怎样定义一个 script 的参数的代号!?以底下我们的说明为例:
[root @test test]# myscript opt1 opt2 opt3 opt4
          $0    $1  $2  $3  $4
这是什么意思呢?就是说,在这个 script ( myscript )里面,只要变量名称为 $0 就表示为
myscript 这个咚咚,也就是说:
$0 : myscript 亦即是 script 的檔名
$1 : opt1 亦即是第一个附加的参数 (parameter)
$2 : opt2
$3 : opt3
这样说或许不是很清楚,我们来看一个例子。
[root @test test]# vi test05-0123
#!/bin/bash
# This program will define what is the parameters
# VBird 2002/06/27
echo "This script's name => $0"
echo "parameters $1 $2 $3"
[root @test test]# sh test05-0123 pa1 pa2 pa3
This script's name => test05-0123
parameters pa1 pa2 pa3
这个东西在运用上也是相当的重要的,例如当您要取得 script 的名称时(因为有时候使用者会自行更
改文件名称),则这个功能变量就相当的重要了!




7、条件式判断:if...then...fi, case.....esac

if [ 条件判断一 ] && (||) [ 条件判断二 ]; then <== if 是起始的意思,后面可以接若干个判断式,使用 && 或 ||
执行内容程序
elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then <==第二段的判断,如果第一段没有符合就来此搜寻条件
执行第二段内容程序
else <==当前两段都不符合时,就以这段内容来执行!
执行第三段内容程序
fi <==结束 if then 的条件判断!
上面的意思是这这样的:在中刮号『[]』里面的是条件式,如果是复合式的条件判断(如若A及B则
C之类的逻辑判断),那么就需要在两个中刮号之间加上『 && (and)』或者是『 || (or)』这样的逻
辑表达式才行!如果是多重选择的话,那么就需要以 elif (optional, 选择性的,若有需要才加上!)来
新增另一个条件;如果所有的条件都不适用,则使用 else (optional)来进行最后的执行内容啰!

case 种类方式(string) in <==开始阶段,那个种类方式可分成两种类型,通常使用 $1 这一种直接下达类型!
种类方式一)
程序执行段
;; <==种类方式一的结束符号!
种类方式二)
程序执行段
;;
*)
echo "Usage: {种类方式一|种类方式二}" <==列出可以利用的参数值!
exit 1
esac <==这个 case 的设定结束处!
范例:
[test @test test]# vi test09-case.sh
#!/bin/bash
# program: Using case mode
# Made by: VBird
# date: 2002/05/20
# content: I will use this program to study the case mode!
# 1. print this program
echo "This program will print your selection!"
case $1 in <==使用直接下达指令型态!
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}" <==列出可以使用的参数(如果使用者下达错误的参数时)
exit 1
esac
[test @test test]# sh test09-case.sh <==执行结果!显示没有相对的参数!所以列出可以参数!
This program will print your selection!
Usage {one|two|three}
[test @test test]# sh test09-case.sh three
This program will print your selection!
your choice is three

那么对谈式的 case 又如何呢?
[root @test test]# vi test10-case.sh
#!/bin/bash
# program: Using case mode
# Made by: VBird
# date: 2002/06/27
# content: I will use this program to study the case mode!
# 1. print this program
echo "Press your select one, two, three"
read number
case $number in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
exit 1
esac

[root @test test]# sh test10-case.sh
Press your select one, two, three
two <=这一行是您输入的呦!
your choice is two


8、循环:for....do....done, while...do...done, until...do...done,


for (( 条件一; 条件二; 条件三 ))
for variable in variable1 variable2 .....
while [ condition1 ] && { || } [ condition2 ] ...
until [ condition1 ] && { || } [ condition2 ] ...


9、script 如何 debug :
scripts 在执行之前,最怕的就是出现问题了!那么我们如何 debug 呢?有没有办法不需要透过直接执
行该 scripts 就可以来判断是否有问题呢!?呵呵!当然是有的!我们就直接以 sh 来进行判断吧!
[test @test test]# sh [-nvx] scripts
-n :不要执行 scripts ,查询 scripts 内的语法,若有错误则予以列出!
-v :在执行 scripts 之前,先将 scripts 的内容显示在屏幕上;
-x :将有使用到的 scripts 内容显示在屏幕上,与 -v 稍微不同!
[test @test test]# sh -n test01-hello.sh
[test @test test]# sh -v test01-hello.sh
#!/bin/bash
# This program will print the "Hello! How are you" in your monitor
# Date: 2002/06/27
# User: VBird
hello="Hello! How are you"
echo $hello
Hello! How are you
[test @test test]# sh -x test01-hello.sh
+ hello=Hello! How are you
+ echo 'Hello!' How are you
Hello! How are you

阅读(791) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~