Shell编程(一)

[toc]shell

shell编程

1、shell 脚本介绍

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。
shell是一种脚本语言;
可使用逻辑判断、循环等语法;
可自定义函数;
shell是系统命令的集合;
shell脚本能够实现自动化运维,能大大增长咱们的运维效率;编程

说明了shell 脚本在工做的重要性,shell脚本就是一些命令的集合,是自动化运维的重要部分bash

2、 Shell脚本结构和执行

下面跟着步骤来一步步学习如何写shell脚本运维

2.1 建立shell脚本实验环境

[root@localhost ~]# mkdir shell
[root@localhost ~]# cd shell/
[root@localhost shell]# ls
[root@localhost shell]# vi 01.sh

shell脚本一般都是.sh为后缀名,但不是说不加.sh的脚本就不能执行,只是你们的一个习惯而已,因此若是发新了以.sh为后缀的文件,那么它可能就是一个shell脚本.ide

mark

本例中,脚本文件01.sh,进入编辑状态后,第一行要以#!/bin/bash开头,表示该文件使用的是bash语法.而/bin/bash是Bash的解释器命令路径.函数

2.2 执行该脚本 #sh.01.sh

[root@localhost shell]# sh 01.sh
123
 22:43:14 up 25 min,  1 user,  load average: 0.00, 0.01, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     22:26    2.00s  0.08s  0.00s w
01.sh

把#!/bin/bash这一行删除,运行命令后也能执行但这样不符合规范.学习

mark

2.3 另外一种方法执行该脚本 #./01.sh

使用该方法运行shell脚本的前提是脚本自己有执行权限,须要给脚本加x权限测试

[root@localhost shell]# chmod +x 01.sh
[root@localhost shell]# ./01.sh
123
 23:18:34 up  1:01,  1 user,  load average: 0.00, 0.01, 0.03
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     22:26    2.00s  0.11s  0.01s w
01.sh

2.4 查看解释路径/bin/bash和/bin/sh

mark

[root@localhost shell]#  bash 01.sh
123
 23:37:15 up  1:19,  1 user,  load average: 0.00, 0.01, 0.03
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     22:26    3.00s  0.20s  0.01s w
01.sh

这里是否能够理解有三种方法均可以执行shell脚本,可是本机验证成功,不表明其余机器虚拟机是否成功,后续测试设计

2.5 举例脚本的编辑方式,注释等

[root@localhost shell]# vi /etc/init.d/network

#! /bin/bash
#
# network       Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.
#

mark

2.6 sh -x 查看shell脚本执行过程,方便后续调试

[root@localhost shell]# sh -x 01.sh
+ echo 123
123
+ w
 23:40:21 up  1:22,  1 user,  load average: 0.00, 0.01, 0.03
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     22:26    5.00s  0.22s  0.01s w
+ ls
01.sh
[root@localhost shell]# bash -x 01.sh
+ echo 123
123
+ w
 23:40:29 up  1:23,  1 user,  load average: 0.00, 0.01, 0.03
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     22:26    5.00s  0.21s  0.00s w
+ ls
01.sh

2.7 sh -n 检测shell脚本是否有语法错误

mark

3、date命令用法

date命令用于显示或设置系统时间与日期。
命令选项:调试

[root@xavi ~]# date  //当前日期
2018年 04月 17日 星期二 22:06:22 CST
[root@xavi ~]# cal  //当前日历
      四月 2018     
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

[root@xavi ~]# date +%Y  //年
2018
[root@xavi ~]# date +%y  //年缩写
18

[root@xavi ~]# date +%m  //月
04

[root@xavi ~]# date +%Y%m%d //年月日
20180417

[root@xavi ~]# date +%F
2018-04-17

[root@xavi ~]# date +%H  //小时
22
[root@xavi ~]# date +%M   //分钟
23
[root@xavi ~]# date +%S   //秒
10

[root@xavi ~]# date +%s  //时间戳,时间戳是从1970年1月1日(UTC/GMT的午夜)开始所通过的秒数,不考虑闰秒
1523975099

[root@xavi ~]# date +%T //时间的完整显示
22:27:04

[root@xavi ~]# date "+%Y-%m-%d %H:%M:%S %w" // 年月日,时分秒,星期  
2018-04-17 22:20:50 2

[root@xavi ~]# date -d "-1 day"
2018年 04月 16日 星期一 22:31:20 CST
[root@xavi ~]# date -d "-1 day" +%F
2018-04-16

[root@xavi ~]# date -d "-1 month" +%F
2018-03-17
[root@xavi ~]# date -d "-1 year" +%F
2017-04-17
[root@xavi ~]# date -d "-1 hour" +%T
21:44:30

[root@xavi ~]# date +%s -d "2018-04-17 22:46"  //  日期转化为时间戳
1523976360

4、shell脚本中的变量

当脚本中使用某个字符串较频繁而且字符串长度很长时就应该使用变量代替
 使用条件语句时,常使用变量    if [ $a -gt 1 ]; then ... ; fi
 引用某个命令的结果时,用变量替代   n=`wc -l 1.txt`
 写和用户交互的脚本时,变量也是必不可少的  read -p "Input a number: " n; echo $n   若是没写这个n,能够直接使用$REPLY
 内置变量 $0, $1, $2…    $0表示脚本自己,$1 第一个参数,$2 第二个 ....       $#表示参数个数
 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
相关文章
相关标签/搜索