shell脚本

shell脚本

shell脚本介绍

shell脚本就是一些命令的集合,能帮助我们更方便管理服务器

shell脚本的创建和执行

自定义脚本都放在sbin目录下

# cd /usr/local/sbin

编写第一个脚本

# vi first.sh

脚本内容

#! /bin/bash     表示该文件使用的是bash语

## This is my first shell script.     #表示注释
## Writen by kei 2020-03-31
date
echo "Hello world!"
 
执行脚本内容
# sh first.sh
# ./first.sh   
使用这个命令是要先给脚本x权限
 

查看脚本执行过程

# sh -x first.sh

命令date 

date的常用用法

date +%Y    表示以四位数字格式打印年份

date +%y    表示以两位数字格式打印年份

date +%m   表示月份

date +%d    表示日期

date +%H   表示小时

date +%M   表示分钟

date +%S   表示秒

date +%w   表示星期(结果显示为0表示周日)   

shell脚本中的变量 

定义变量的格式为“变量名=变量的值”,在脚本中引用变量时需要加上符号$

编写一个与变量相关的脚本

# vi variable.sh

脚本内容

#! /bin/bash
## In this script we will use variables.
## Writen by kei 2020-03-31
d=`date +%H:%M:%S`       反引号的作用是将引号中的字符串当成shell命令执行
echo "The script begin at $d."
echo "Now we'll sleep 2 seconds."
sleep 2
d1=`date +%H:%M:%S`
echo "The script end at $d1."    d和d1在脚本中作为变量
 
执行脚本
# sh variable.sh

数学运算

# vi sum.sh

脚本内容

#! /bin/bash
## For get the sum of two numbers.
## Writen by kei 2020-03-31
a=1
b=2
sum=$[$a+$b]        数学计算要用[ ]括起来,而且前面要加符号$
echo "$a+$b=$sum"

 

执行脚本

# sh sum.sh

和用户交互 

# vi read.sh

脚本内容

#! /bin/bash
## Using 'read' in shell script.
## Kei 2020-03-31
read -p "Please input a number: " x       read命令把用户输入的字符串作为变量值
read -p "Please input another number: " y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum"
 
执行脚本
# sh read.sh

查看执行过程

# sh -x read.sh

shell脚本预设变量

shell脚本在执行时,后面可以跟一个或者多个参数

# vi option.sh

脚本内容

#! /bin/bash
sum=$[$1+$2]      $1和$2为脚本的预设变量
echo "sum=$sum"
 
执行脚本
# sh -x option.sh 1 2

修改脚本,$0代表脚本本身的名字

# vi option.sh

脚本内容

#! /bin/bash
echo "$1 $2 $0"
 
执行脚本
# sh option.sh 1 2

shell脚本中的逻辑判断

不带else

格式

if  判断语句; then

    command

fi

编写脚本

# vi if1.sh

脚本内容

#! /bin/bash
read -p "Please input your score: " a
if ((a<60)); then       
        echo "You didn't pass the exam."
fi
 
执行脚本
# sh if1.sh

带有else

格式

if  判断语句; then

    command

else

   command

fi

编写脚本

# vi if2.sh

脚本内容

#! /bin/bash
read -p "Please input your score: " a
if ((a<60)); then
        echo "You didn't pass the exam."
else
        echo "Good! You passed the exam."
fi
 
 
执行脚本

# sh if2.sh

带有elif 

格式

if  判断语句1; then

    command

elif  判断语句2;then

    command

else

    command

fi

编写脚本

# vi if3.sh

脚本内容

#! /bin/bash
read -p "Please input your score: " a
if ((a<60)); then       判断数值大小除了可以用(())的形式外,还可以用[ ]
        echo "You didn't pass the exam."
elif ((a>=60)) && ((a<85)); then             &&表示并且。||表示或者
        echo "Good! You passed the exam."
else
        echo "Very good! Your score is very high!"
fi

 

执行脚本

# sh if3.sh

 

常用符号

-lt  小于

-gt  大于

-le  小于或等于

-ge  大于或等于

-eq  等于

-ne  不等于

和文档有关的判断

if还常用于判断文档的属性

-e   判断文件或目录是否存在

-d   判断是不是目录以及是否存在

-f   判断是不是普通文件以及是否存在

-r   判断是否有读权限

-w   判断是否有写权限

-x   判断是否可执行

格式

if [ -e filename ]; then

    command

fi

case逻辑判断

格式

case  变量  in

value1)       不限制value的个数

  command

  ;;

value2)

  command

  ;;

*)        *代表其他值

  command

  ;;

esac

编写脚本

# vi case.sh

脚本内容

#! /bin/bash
read -p "Input a number: " n
a=$[$n%2]
case $a in                   $a的值为1或0
 1)
        echo "The number is odd."
        ;;
 0)
        echo "The number is even."
        ;;
 *)
        echo "It's not a number!"
        ;;
esac

 

执行脚本

# sh case.sh

shell脚本中的循环 

常用的循环有for循环和while循环

for循环

格式

for  变量名  in  循环的条件;do

  command

done          

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

# vi for.sh

脚本内容

#! /bin/bash
for i in `seq 1 5`; do
        echo $i
done

 

执行脚本

# sh for.sh

循环的条件还可以引用系统命令的执行结果,但需要用反引号括起来 

#  for file in `ls`; do echo $file; done

while循环 

用冒号:代替循环条件时可以做到死循环

格式

while  条件; do

  command

done

编写脚本

# vi while.sh

脚本内容

#! /bin/bash
a=5
while [ $a -ge 1 ]; do
        echo $a
        a=$[$a-1]
done

 

执行脚本 

# sh while.sh

shell脚本中的函数

shell脚本中的函数就是先把一段代码整理到一个小单元中,并给这个小单元命名,当用到这段代码时直接调用这个小单元的名字即可

格式

function  函数名()

{

  command1

  command2

}

编写脚本

# vi func.sh

脚本内容

#! /bin/bash
a=5
while [ $a -ge 1 ]; do
        echo $a
        a=$[$a-1]
done
[root@localhost sbin]# cat func.sh
#! /bin/bash
function sum()
{
        sum=$[$1+$2]
        echo $sum
}
sum $1 $2
 
执行脚本
# sh func.sh 1 2

shell脚本中的中断和继续

break

break用在循环中,表示退出该层循环
编写脚本
# vi break.sh
脚本内容
#! /bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i == 3 ]             当i等于3时会跳出循环
        then
          break
        fi
        echo $i
done
echo aaaaaa
 
执行脚本
# sh break.sh

contiune

在循环中,contiue表示退出本次循环
编写脚本
# vi contiune.sh
脚本内容
#! /bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i == 3 ]       当i等于3时,结束本次循环
        then
          continue
        fi
        echo $i
done
echo $i
 
执行脚本
# sh continue.sh
 

exit

exit表示直接退出脚本

编写脚本

# vi exit.sh

脚本内容

#! /bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i == 3 ]
        then
          exit
        fi
        echo $i
done
echo aaaaaaa

 

执行脚本

# sh exit.sh

 

 

 

 

posted @ 2020-04-03 13:51  腿腿腿长一米八  阅读(362)  评论(0编辑  收藏  举报