linux系统下的shell script 入门指南


第1节 scripts 的执行过程:
一个 script 被执行的时候, bash 会据以判断执行的步骤为:
1. 如果读取到一个 Enter 符号( CR ),就尝试开始执行该行命令;
2. 如同前面 bash command 提到的,指令间的多个空白会被忽略掉;
3. 而空白行也将被忽略掉!,并且 tab 也是不会被理会的!
4. 至于如果一行的内容太多,则可以使用 / 来延伸至下一行;
5. 此外,使用最多的 # 可做为批注!任何加在 # 后面的字,将全部被视为批注文字而被忽略!
然后,在撰写一个 scripts 的时候,最好养成良好的习惯:
1. 先宣告使用的 shell 为何?(特别留意这一点,在某些状况中,例如 /etc/crontab 情况下,如果没有宣告使用的 shell ,常常会出现错误讯息而导致 scripts 无法被执行呦!)
2. 注明该 script 的内容功能、版本信息、作者、文件日期等等
3. 每一个大步骤的主要功能
第2节 如何执行scripts 文件
• 一是将该文件改成可以执行的属性,如 chmod 755 scripts.file ,然后执行该文件;
• 另一种则是直接以 sh 这个执行档来执行 script 的内容,如 sh scripts.file!
第3节  建立简单的script:
例子:在屏幕上列出『 Hello ! How areyou ?』
[root @test test]# vi test01-hello.sh
#!/bin/bash <==在 # 之后加上 ! 与 shell 的名称,用来宣告使用的 shell
# 这个脚本的用途在于列出 Hello ! How are you 在屏幕上
# 建檔日期: 2002/05/20
# Made by VBird
hello=Hello/ /!/ How/ are/ you/ /? <==这就是变量啦!
echo $hello
注意:
• 所有在 scripts 里面的东西,基本规则 ( 如变量设定规则 ) 需要与 command line 时相同;
• scripts 的附档名最好为 .sh 提供他人的认识;
• 并非加上 .sh 就可以是执行档,还需要查看其属性中是否有 x 这个属性。
第4节 " 与 ' 的异同
[root @test test]# vi test02-2var.sh
#!/bin/bash
# 这个脚本用途在于引用两个变量,顺便比较一下 " 与 ' 的异同
# Date: 2002/06/27
# Made by VBird
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2
[root @test test]# sh test02-2var.sh
V.Bird
My name is V.Bird
My name is $name
输出的结果显示, " 与 ' 最大的不同就是在于能不能保有『变量内容』啦!再提醒一次,那个单引号『 ' 』里头的数据都将变成『单纯的字符』而不是有特殊的字体呦!
第5节 卷标与运算符号:declare
当我们在进行『计算』的时候,到底 bash 能不能了解我们所给予的是『数字』还是单纯的『字符』呢?这个很重要的,因为会造成系统的误判!比如,当我们需要输出 3 * 5 的结果时,需要如何做呢?用单纯的command line 一行一行输入的结果如下:
[root @test test]# a=3
[root @test test]# b=5
[root @test test]# c=$a*$b
[root @test test]# echo $c
3*5 <==糟糕!怎么变成了字符串了?!
发现了吗?上面输出的是不是我们所希望的 3*5 = 15 的结果?!这是因为我们没有定义该变量,则该变数预设是呈现『字符串』的型态!那么自然 $c 就成为自串型态了!所以我们需要来宣告一下变量,宣告变量使用的是 declare 这个指令。
 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
例子:如果您的计算结果当中,需要输入为 2*3+5*13-32+25 时,并且在最后输出『 Your result is ==> 』该怎样写这一支简单的 script 呢?
[root @test test]# vi test03-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"
第6节  对谈式 scripts
1 read指令
read 的功能就是『依据您在键盘输入的结果 input 到变量内容中』,例如:
[root @test test]# read name
VBird <==这是键盘输入的结果
[root @test test]# echo $name
VBird
2 怎样定义一个 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 的名称时,因为有时候使用者会自行更
改文件名称,则这个功能变量就相当的重要了。
3 scripts 逻辑判断式与表达式:
• 3.1几个重要的逻辑卷标:
1. 关于文件与目录的侦测逻辑卷标
-f    常用!侦测『文件』是否存在
-d    常用!侦测『目录』是否存在
-b    侦测是否为一个『 block 文件』
-c    侦测是否为一个『 character 文件』
-e    侦测『某个东西』是否存在!
-S    侦测是否为一个『 socket 标签文件』
-L   侦测是否为一个『 symbolic link 的文件』
2. 关于程序的逻辑卷标!
-G    侦测是否由 GID 所执行的程序所拥有
-O    侦测是否由 UID 所执行的程序所拥有
-p    侦测是否为程序间传送信息的 name pipe 或是 FIFO
3.关于文件的属性侦测!
-r    侦测是否为可读的属性
-w    侦测是否为可以写入的属性
-x    侦测是否为可执行的属性
-s    侦测是否为『非空白文件』
-u    侦测是否具有『 SUID 』的属性
-g    侦测是否具有『 SGID 』的属性
-k    侦测是否具有『 sticky bit 』的属性
4. 两个文件之间的判断与比较 ;例如『 test file1 -nt file2 』
-nt    第一个文件比第二个文件新
-ot    第一个文件比第二个文件旧
-ef    第一个文件与第二个文件为同一个文件( link 之类的文件)
5. 逻辑的『和(and)』『或(or)』
&&   逻辑的 AND 的意思
||    逻辑的 OR 的意思
3.2运算符号
• 运算符号简介:
= 等于 != 不等于 < 小于 > 大于
-eq 等于 -ne 不等于 -lt 小于 -gt 大于
-le 小于或等于
-ge 大于或等于
-a 双方都成立(and)
-o 单方成立(or)
-z 空字符串
-n 非空字符串
3.3 条件式判断:if...then...fi, case.....esac
• 条件判断一:if then fi 的方式
这个条件判断的语法为:
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then <== if 是起始的意思,后面可以接若干个判断式,使用 && 或 ||执行内容程序elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then <==第二段的判断,如果第一段没有符合就来此搜寻条件执行第二段内容程序
else <==当前两段都不符合时,就以这段内容来执行!
执行第三段内容程序
fi <==结束 if then 的条件判断!

如果是多重选择的话,那么就需要以 elif (optional, 选择性的,若有需要才加上!)来新增另一个条件;
不过,这里有几个新手常犯的错误,我们需要来加强说明一下:
1. 在 [ ] 当中,只能有一个判别式;
2. 在 [ ] 与 [ ] 当中,可以使用 && 或 || 来组织判别式;
3. 每一个独立的组件之间『都需要有空格键来隔开』!
例子:[root @test test]# vi test06-ifthen.sh
#!/bin/bash
# This program is used to study if then
# VBird 2002/06/27
echo "Press y to continue"
read yn
if [ "$yn" = "y" ]; then
echo "script is running..."
else
echo "STOP!"
fi
改进:
[root @test test]# cp test06-ifthen.sh test07-ifthen.sh
[root @test test]# vi test07-ifthen.sh
#!/bin/bash
# This program is used to study if then
# VBird 2002/06/27
echo "Press y to continue"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
echo "script is running..."
else
echo "STOP!"
fi
parameter 的选用呢?
[root @test test]# vi test08-ifthen.sh
#!/bin/bash
# set parameters in the if then
# 需要加上 hello 这个参数才会显示正确的!
# VBird 2002/06/27
if [ "$1" = "hello" ]; then
echo "Hello! How are you ?"
elif [ "$1" = "" ]; then
echo "You MUST input parameters"
else
echo "The only accept parameter is hello"
fi
范例:
[test @test test]# vi port.sh <==编辑一个文件为 test1.sh 的 script
#!/bin/bash <==宣告使用的 shell 类型
# program: Using to study the [if ... then ... fi] program
# Made by: VBird
# date: 2002/05/20
# content: I will using this program to show your services
# 1. print the program's work in your screen
echo "Now, the services of your Linux system will be detect!"
echo "The www, ftp, ssh, and sendmail + pop3 will be detect!"
echo " "
# 2. www
www=`netstat -an|grep LISTEN|grep :80` <==这个就是变量啦!并使用了管线命令!
if [ "$www" != "" ]; then <==开始条件的判断啰!
echo "WWW is running" <==若条件成立,那么就打印这一行的内容!
else
echo "WWW is NOT running"
fi
# 3. ftp
ftp=`netstat -an|grep LISTEN|grep :21`
if [ "$ftp" != "" ]; then
echo "FTP is running"
else
echo "FTP is NOT running"
fi
# 4. ssh
ssh=`netstat -an|grep LISTEN|grep :22`
if [ "$ssh" != "" ]; then
echo "SSH is running"
else
echo "SSH is NOT running"
fi
# 5. sendmail + pop3
smtp=`netstat -an|grep LISTEN|grep :25`
pop3=`netstat -an|grep LISTEN|grep :110`
if [ "$smtp" != "" ] && [ "$pop3" != "" ]; then <==有两个以上的条件式,就使用 && 或 || 来分隔!
echo "sendmail is OK!"
elif [ "$smtp" != "" ] && [ "$pop3" = "" ]; then
echo "sendmail have some problem of your pop3"
elif [ "$smtp" = "" ] && [ "$pop3" != "" ]; then
echo "sendmail have some problem of your smtp"
else
echo "sendmail is NOT running"
fi
• 条件判断二:使用 case ...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
那么对谈式的 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
4循环:for....do....done, while...do...done, until...do...done
最简单的判断式可以是底下几种:
• for (( 条件一; 条件二; 条件三 ))
• for variable in variable1 variable2 .....
• while [ condition1 ] && { || } [ condition2 ] ...
• until [ condition1 ] && { || } [ condition2 ] ...
for 是已经知道有多少个 run 了,即是已经知道要跑几次了,至于 until 与 while 则分别是:
• 『until:直到条件相同的时候才离开程序』;
• 『while:当条件相同的时候,就继续做!』
示例:假设我们计算 1 + 2 + 3+.... + 100 ,以 script 要如何写呢?
[test @test test]# vi test11-loop.sh
#!/bin/bash
# Using for and loop
# VBird 2002/06/27
declare -i s # <==变量宣告
for (( i=1; i<=100; i=i+1 ))
do
s=s+i
done
echo "The count is ==> $s"
[test @test test]# sh test11-loop.sh
The count is ==> 5050
请注意! for (( 条件一; 条件二; 条件三)) 这是必须要的!
• 条件一:这可以看成是『初始值』,如上面的例子中,初始值是 i=1 啦!
• 条件二:这可以看成是『符合值』,如上面的例子中,当 i<=100 的时候都是符合条件的!
• 条件三:这可以看成是『步阶』!也就是说, i 每次都加一!
上面的例子是说:由 i=1 开始到 i<= 100 ,每次 i 都加一来执行底下的程序段(就是s=s+i ),当 i >100 (也就是 i=101 )
例子: 使用 while :
[test @test test]# vi test12-loop.sh
#!/bin/bash
# Using while and loop
# VBird 2002/06/27
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
例子:使用 until
[test @test test]# vi test13-loop.sh
#!/bin/bash
# Using until and loop
# VBird 2002/06/27
declare -i i
declare -i s
until [ "$i" = "101" ]
do
s=s+i
i=i+1
done
echo "The count is ==> $s"
例子:这是另外一种循环的方式,可以用来判断非数字的类型呦!
[test @test test]# vi test14-for.sh
#!/bin/bash
# using for...do ....done
# VBird 2002/06/27
LIST="Tomy Jony Mary Geoge"
for i in $LIST
do
echo $i
done
这一种格式是以空格键当作 i 这个变量的选择项目!也就是说,上面的 $LIST 这个变量当中,以空格
键来分隔的时候,共可以分离出来四个!所以啰!当以 do ..... done ... 就可以分别写出四个咚咚啦!
例子:将你的 Linux 主机上的账号 ( account ) 印出来?
[test @test test]# vi test15-for.sh
#!/bin/bash
# Using for and loop to read the account of this linux server!
# VBird 2002/06/27
account=`cut -d ":" -f1 /etc/passwd|sort`
echo "The following is your linux server's account"
for i in $account
do
echo $i
done
对谈式的循环作用吧!当我们输入 y 或 Y 时,程序就予以结束!该怎样做?!
[test @test test]# vi test16-loop.sh
#!/bin/bash
# Using until
# VBird 2002/06/27
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
read yn
done
echo "Stop here"
5『逻辑判断式』的使用方式
例子:
1. 先查看一下 /root/test/logical 这个名称是否存在;
2. 若不存在,则建立一个文件,使用 touch 来建立,建立完成后离开;
3. 如果存在的话,判断该名称是否为文件,若为文件则将之删除后建立一个文件,档名为 logical,之后离开;
4. 如果存在的话,而且该名称为目录,则移除此目录!
[test @test test]# vi test17-ifthen.sh
#!/bin/bash
# using if and then to select file or directory
# VBird 2002/06/27
if [ ! -e logical ]; then
touch logical
echo "Just make a file logical"
exit 1
elif [ -e logical ] && [ -f logical ]; then
rm logical
mkdir logical
echo "remove file ==> logical"
echo "and make directory logical"
exit 1
elif [ -e logical ] && [ -d logical ]; then
rm -rf logical
echo "remove directory ==> logical"
exit 1
else
echo "Does here have anything?"
fi
6  script 如何 debug :
通常情况下,我们就直接以 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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值