Linux基础知识整理[7]——Shell script

Shell script

bash据以判断执行脚本的步骤为:

         ①如果读取到一个Enter符号(CR),就尝试开始执行该行命令;

         ②如同前面bashcommand提到的,指令间的多个空白会被忽略;

         ③空白行也将被忽略,并且Tab也不会被理会;

         ④如果一行内容太多,则可以使用\延伸至下一行;

         ⑤此外,使用最多的#可做为注释。任何加在#后的字,将全部被视为注释文字而被忽略。

执行的方法:

     ①一个是将该文件改成可以执行的属性,如chmod 755 scripts.file,然后执行该文件;

     ②另一种则是直接以sh这个执行文件来执行脚本内容,如sh scripts.file。

 

[root @test /root]# mkdir test; cd test

建立一个新的目录,所有的脚本都暂存在此!

[root @test test]# vi test01-hello.sh

#!/bin/bash <==在 # 之后加上 ! 与 shell 的名称,用来宣告使用的 shell

# 这个脚本的用途在于在屏幕上显示Hello ! How are you

# 创建日期: 2002/05/20

hello=Hello\ \!\ How\ are\ you\ \? <==这就是变量

echo $hello

[root @test test]# sh test01-hello.sh

Hello ! How are you ? <==输出结果显示在屏幕上!

这里注意:

         ①所有在脚本里的东西,基本规则(如变量设定规则)需要与在命令行下相同;

         ②脚本的后缀名最好为.sh,以方便他人识别;

         ③并非加上.sh就是可执行文件,还需要查看其属性中是否有x属性。

 

声明变量使用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

15 <==变成数字了。

如果没有定义某个变量,则该变量默认呈现字符串的形式:

[root @test test]# vi test03-declare.sh

#!/bin/bash

# This program is used to "declare"variables

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 test03-declare.sh

Your result is ==> 2*3+5*13-32+25

Your result is ==> 64

 

交互式脚本

[root @test test]# read name

VBird <==这是您直接在键盘上输入的内容

[root @test test]# echo $name

VBird

应用到脚本中:

[root @test test]# vi test04-read.sh

#!/bin/bash

# This program is used to "read"variables

echo "Please keyin your name, andpress 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 tostart.

VBird Tsai

This is your keyin data ==> VBird Tsai

 

脚本的参数代号

[root @test test]# myscript    opt1    opt2    opt3   opt4

                        $0     $1      $2     $3      $4

上面的意思是说,在这个脚本(myscript)中,只要变量名称为$0,就表示为myscript,

也就是说:

· $0: myscript,亦即脚本的文件名

· $1: opt1,亦即第一个附加的参数

· $2: opt2

· $3: opt3

 [root @test test]# vi test05-0123

#!/bin/bash

# This program will define what is theparameters

echo "This script's name =>$0"

echo "parameters $1 $2 $3"

 

[root @test test]# sh test05-0123 pa1 pa2pa3

This script's name => test05-0123

parameters pa1 pa2 pa3

 

脚本逻辑判断式与表达式

逻辑卷标含义

1. 关于文件与目录的逻辑卷标

         -f 常用!检测文件是否存在        -d 常用!检测目录是否存在        -b 检测是否为一个block文件

         -c 检测是否为一个character文件     -S 检测是否为一个socket标签文件   -L 检测是否为一个符号链接文件

         -e 检测某个东西是否存在!可以是任何东西

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 第一个文件与第二个文件为同一个文件(诸如链接文件)

5. 逻辑与(and)和或(or)

         && 逻辑与

         || 逻辑或

 

运算符

= 等于   != 不等于     < 小于   >大于   -eq等于  -ne不等于

-lt 小于   -gt 大于       -le 小于或等于      -ge 大于或等于

-a 双方都成立(and)    -o单方成立(or)  -z空字符串

-n 非空字符串

 

条件判断

if [ 条件判断一 ] && (||) [ 条件判断二 ]; then <==if是起始,后面可以接若干个判断式,使用 && 或 ||执行判断

elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then<==第二段判断,如果第一段不符合要求就转到此搜寻条件,执行第二段程序

else <==当前两段都不符合时,就执行这段内容

fi <==结束 if then 的条件判断

注:①在 [ ]中,只能有一个判断式;

         ②在 [ ]与 [ ] 间,可以使用&&或| |结合判断式;

         ③每一个独立的组件之间需要用空格键隔开。

 

case...esac

case 种类方式(string) in <==开始阶段,种类方式可分成两种类型,

通常使用 $1 这种直接输入类型

种类方式一)

程序执行段

;; <==种类方式一的结束符号

种类方式二)

程序执行段

;;

*)

echo "Usage: {种类方式一|种类方式二}"<==列出可以利用的参数值

exit 1

esac <== case结束处

种类方式(string)的格式主要有两种:

[root @test test]# vi test10-case.sh

#!/bin/bash

# program: Using case mode

# content: I will use this program to studythe 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

 

循环

①for ((条件1; 条件2; 条件3))

②for variable invariable1 variable2 .....

③while [ condition1 ]&& { | | } [ condition2 ] ...

④until [ condition1 ]&& { | | } [ condition2 ] ...

计算1 +2+ 3 +... + 100:

[test @test test]# vi test11-loop.sh

#!/bin/bash

# Using for and loop

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

----------------------------------------------------------

[test @test test]# vi test14-for.sh

#!/bin/bash

# using for...do ....done

LIST="Tomy Jony Mary Geoge"

for i in $LIST

do

echo $i

done

[test @test test]# sh test5.sh

Tomy

Jony

Mary

Geoge

--------------------------------------------------------------

[test @test test]# vi test12-loop.sh

#!/bin/bash

# Using while and loop

declare -i i

declare -i s

while [ "$i" != "101" ]

do

s=s+i

i=i+1

done

echo "The count is ==> $s"

 

如何调试脚本

[test @test test]# sh [-nvx] scripts

-n :不执行脚本,查询脚本内的语法,若有错误则列出

-v :在执行脚本之前,先将脚本的内容显示在屏幕上;

-x :将用到的脚本内容显示在屏幕上,与-v稍微不同

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值