Shell脚本介绍

什么是Shell

shell script 是利用 shell 的功能所写的一个“程序 (program)”,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式、管道命令与数据流重导向等功能,以达到我们所想要的处理目的。

简单的说,也就是可以使用一个普通的文本,写上多条 shell 命令,一起执行这些命令。
但是,在这个文件中可以添加一些逻辑判断什么的。

Shell脚本的规范

script 的功能;
script 的版本信息;
script 的作者与联络方式;
script 的版权宣告方式;
script 的 History (历史纪录);
script 内较特殊的指令,使用“绝对路径”的方式来下达;
script 运行时需要的环境变量预先宣告与设置。

脚本练习

[root@localhost ~]# vim start.sh

echo "显示磁盘分区"
lsblk
echo "*********************************"                  
echo "显示内存占用量"
free -h

sh start.sh     #sh 表示用shell运行

显示磁盘分区
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   30G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   29G  0 part 
  ├─centos-root 253:0    0   26G  0 lvm  /
  └─centos-swap 253:1    0    3G  0 lvm  [SWAP]
sr0              11:0    1  4.2G  0 rom  
显示内存占用量
              total        used        free      shared  buff/cache   available
Mem:           5.7G        170M        5.4G        8.6M        158M        5.3G
Swap:          3.0G          0B        3.0G
  如果内存占用过高 请运行ffm.sh
-----------------------------------------------------------
vim ffm.sh

echo 3 > /proc/sys/vm/drop_caches
echo "buffer/cache释放完"

[root@localhost ~]# sh ffm.sh 
buffer/cache释放完

执行脚本的方式

脚本(script)的默认变量

特殊变量

位置变量

位置变量是根据命令出现在命令行上的位置来确定的变量,在shell调用过程中,会按参数所在的位置进行调用。


判断表达式

$ touch a.txt
$ test -e a.txt;echo $?
0                             # 测试成功,命令返回值为 0

$ test -e s.txt;echo $?
1                             # 测试失败,命令返回值为 非 0

$ test -f a.txt;echo $?
0

$ test -d a.txt;echo $?
1
其他同理




$? 可以查看上一条命令是否执行成功

echo $?为0则没执行结果或未成功
echo $?为1则为成功

Shell的逻辑结构

一、if / fi判断

if [ 条件判断式 ] ;then
  当条件判断式成立时,执行的命令
fi #条件语句结束
----------------------------------
if [ 条件判断式 ] 
then
  当条件判断式成立时,执行的命令
fi #一样的
示例:
vim if-fi.sh

#!/bin/bash
read -p "input a number:" A
if [  "$A" = 10   ]
then
        echo "congratulations!”
fi
[root 03:37 AM t2sh ~]#sh if-fi.sh
input a number:10
congratulations!
-------------------------------------------
位置变量在if-elif中

二、if / else / fi判断

vim if-fi.sh

#!/bin/bash
read -p "input a number:" A
if [ "$A" = 10 ]
then
    echo "congratulations!"
else
    echo "wrong!"
fi

[root 03:38 AM t2sh ~]#sh if-fi.sh
input a number:1
wrong!
[root 03:38 AM t2sh ~]#sh if-fi.sh
input a number:10
congratulations!
---------------------------

三、if / elif / fi判断

vim if-fi.sh

#!/bin/bash
read -p "input a number:" A
if [ "$A" = 10 ]
then
    echo "congratulations!"
elif [ "$A" -gt 10   ]   #判断$A大于10
then    
    echo "too big!"
elif [ "$A" -lt 10   ]    #判断$A小于10
then
    echo "too little"
fi
 
[root 03:42 AM t2sh ~]#sh if-fi.sh
input a number:1
too little
[root 03:42 AM t2sh ~]#sh if-fi.sh
input a number:11
too big!
[root 03:43 AM t2sh ~]#sh if-fi.sh
input a number:10
congratulations!
---------------------------------------
echo "Press y to continue" 
read yn
if [ "$yn" = "y"  ]
then
        echo "process is continue·····"
else
        echo "process is STOP·····"
fi
------------------------------
进阶版 
ps aux |grep -v "grep"|grep "$1"|grep -v "sh.sh" > /dev/null   #一定要-v 文件名,因为位置变量会产生一个子进程
A=`echo $?`
if [ "$A" -eq 0 ]
then
 echo "process is running"
else
 echo "process is not exist"
fi


四、While do / done

while [  条件判断  ]
do
    条件满足时你要执行的语句
done
-----------------------
n=0    #赋值n=0
while [ "$n" -lt 5  ]  #判断n是否小于5,如果小于5执行do下面的语句
do
    let n++    #每次循环都会让n+1
    echo "$n"
done

[root 03:48 AM t2sh ~]#sh while.sh 
1
2
3
4
5

-----------------------------
if语句在while语句中循环

n=0
while [ "$n" -lt 5  ]
do
    let n++
echo "guess a number what i want"
read -p "input a number:" A
if [[ "$A" =~ [0-9] ]]
then
    if [ "$A" -gt 18  ]
    then
        echo "that's too big"

    elif [ "$A" -lt 18 ]
    then
        echo "that's too little"

    elif [ "$A" == 18 ]
    then
        echo "congratulations!"
        break
    fi
fi
done
echo "u have no choice!"

--------------------------------------
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容