您的位置:首页 > 运维架构 > Shell

Shell脚本编写

2015-07-02 16:33 666 查看
1.shell脚本就是一些命令的集合。把需要执行的一组命令记录到文档中,再去调用这个文档。
139邮箱,收到邮件同时受到短信通知。
shell脚步编写建议:自定义脚本放到/usr/local/sbin目录下
2.第一个shell脚本
vim firstshell.sh

#! /bin/bash

##this is my first shell script

##writen by jason 2015-07-02

date

echo "Hello world !"

#! /bin/bash,开头第一行,表示该文件使用bash语法

.sh后缀,习惯

#后面跟的注释,良好习惯,便于以后查看脚步
执行脚本:sh firstshell.sh 或者 ./firstshell.sh(有时缺少权限时,chmod修改)
3.脚本中变量的使用
vi variable.sh

#! /bin/bash

## In thie shell script we will use variables

## writen by jason 2015-07-02

d=`date +%H:%M:%S`

echo "This script begin at $d"

echo "Now we will sleep 2 seconds."

sleep 2

d1=`date +%H:%M:%S`

echo "This script end at $d1"

变量d和d1的定义时,使用反引号` `,变量内容使用到其他命令的结果(shell脚步基础知识博客中)。

脚本总引用变量,需加上$

3.1 数值运算,两个数的和 sum
vi sum.sh

#! /bin/bash

## Sum of tow numbers

## By jason 20150702

a=1

b=2

sum=$[$a+$b]

echo $sum

数学计算,要用[ ]括起来,并且最外面要加一个 $

3.2用户交互,用户输入
vi read.sh

#! /bin/bash

## Use "read" in shell script

## By jason 20150702

read -p "Please input a number:" x

read -p "Please input anoter number" y

sum=$[$x+$y]

echo "The sum of two number is : $sum"

read就是用在和和用户交互,把用户输入的字符串作为变量值

-x 查看shell执行的过程,sh -x read.sh

3.3 shell预设变量 $1,$2,$0(打印脚本本身的名字)

vi option.sh

#! /bin/bash

##

##

sum=$[$1+$2]

exho "sum=$sum"

sh option.sh 3 7

sum=10

$1,$2就是shell脚步预设的变量,默认设置好的,按顺序输入两个数字,默认赋值变量$1=3,$2=7.

同样$3,$4,$5依次后推也是预设变量。
4.脚本中逻辑判断if,if...else,if..elif..else
4.1 vi iftest1.sh

#! /bin/bash

##

##

read -p "Input your score: " a

if (($a<60)); then if 判断语句 ; then

echo "Didn't pass the exam." commond
fi fi
(($a<60)),shell脚步中独有的格式,必须使用两个()

语法格式:if 判断语句;then

匹配结果执行相应的命令
4.2 if ..else

vi iftest2.sh
#! /bin/bash

##

##

read -p "Iput your score: " a

if (($a<60));then if 判断语句 ;then

echo "Do not pass." commond
else else

echo "Good ! pass the exam." commond

fi fi

4.3 if..esif..else

vi iftest3.sh

#! /bin/bash

##

##

read -p "Input your score: " a

if (($a<60)); then

echo "do not pass."

elif(($a>=60))&&(($a<80)); then

echo "good,pass."

else echo "Very good,high."

两个判断语句通过&&或者||一起时,需要用两个(( ))隔开,单独判断

判断数值大小除(( ))形式外,还有[ ],但需用-lt(小于),-gt(大于),-le(小于等于),-ge(大于等于),-eq(等于),-ne(不等于)

if [ $a -lt 60 ]; then

if [ $a -gt 60 ] || [ $a -lt 80 ]; then ....

4.4 判断文档属性

-e 判断文件或目录是否存在;-d 判断是否为目录,并存在;-f 判断是否为普通文件,并存在

;-r 判断文档是否可读;-w 是否可行;-x 是否可执行,-z 判断指定的变量是否有值。
if [ -f /root/test.txt ]; then echo "It is file."; fi
[ ]前后一定要加空格,不然报错。

5.脚本逻辑判断 case

vi case.sh

#! /bin/bash

##

##
read -p "Input a number: " n

a=$[$n%2]

case $a in case 变量 in

1) value1)

echo "the number is odd." commond
;; ;;

0) value2)

echo "the number is even." commond

;; ;;

*)

echo "this is not a number"

;;

esac

不限判断value值的个数。

6.脚本中的循环 for

vi fortest.sh

#! /bin/bash

for i in `seq 1 5`; do for 变量名 in 循环条件 ;do

echo $i command

done done

循环条件seq 1 5表示从1-5的序列

6.1 循环条件写成一组字符串或者数字,也可以是一条命令执行的结果

for i in 1 2 3 4 a b; do echo $i; done
for file in `ls`; do echo $file; done
7.脚本中的循环 while

vi whiletest.sh

#! /bin/bash

a=5

while [$a -ge 1]; do while 循环条件;do

echo $a command

a=$[$a-1]

done done

8.shell脚本中函数

函数:就是把一段代码整理到一个小单元中,起个名字,调用这个单元的名字即可。

vi func.sh

#! /bin/bash

function sum() function 函数名()

{ {
sum=$1+$2 command

echo $sum

} }

sum $1 $2 调用函数

在shell脚本中,函数一定要写在最前面,不能出现在后面或中间,必须在最前面申明,才能为后面调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  world 短信 local 139邮箱