Shell编程

20.1 Shell脚本介绍

shell是什么?
shell是一种脚本语言 aming_linux blog.lishiming.net
可以使用逻辑判断、循环等语法
可以自定义函数
shell是系统命令的集合
shell脚本可以实现自动化运维,能大大增加我们的运维效率


20.2 Shell脚本结构和执行

shell脚本结构和执行方法:
开头需要加#!/bin/bash
以#开头的行作为解释说明
脚本的名字以.sh结尾,用于区分这是一个shell脚本
执行方法有两种 (1、bash; 2、绝对路径或者相对路径) chmod +x 1.sh; ./1.sh
bash 1.sh
查看脚本执行过程 bash -x 1.sh
查看脚本是否语法错误 bash -n 1.sh


[root@DasonCheng ~]# cd /usr/local/sbin/
[root@DasonCheng sbin]# vim first.sh
#! /bin/bash    //说明用什么脚本解析器;
#The shell script is my first scirpt!
#Written by chengzhenge 2017年 09月 12日 星期二 14:59:26 CST
date
echo 'hello world'
……

执行法1:sh +脚本

[root@DasonCheng sbin]# sh first.sh     //sh直接加脚本;
2017年 09月 12日 星期二 15:02:05 CST
hello world
##其实sh就是bash、详情如下:
[root@DasonCheng sbin]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 5月  31 08:35 /bin/sh -> bash
[root@DasonCheng sbin]# ll /bin/bash
-rwxr-xr-x. 1 root root 960392 8月   3 2016 /bin/bash
[root@DasonCheng sbin]# bash first.sh     //sh first.sh也就是bash first.sh
2017年 09月 12日 星期二 15:03:42 CST
hello world
[root@DasonCheng sbin]# sh -x first.sh   //参数-x 查看执行过程;
+ date
2017年 09月 12日 星期二 15:02:08 CST
+ echo 'hello world'
hello world
[root@DasonCheng sbin]# sh -n first.sh    //参数-n查看脚本是否有逻辑循环问题;没有则没有显示;

执行法2:./first.sh或者/usr/local/sbin/first.sh

[root@DasonCheng sbin]# ./first.sh     //这样执行需要有执行权限哦!
-bash: ./first.sh: 权限不够
[root@DasonCheng sbin]# chmod +x first.sh 
[root@DasonCheng sbin]# ./first.sh     //相对路径执行;
2017年 09月 12日 星期二 15:09:15 CST
hello world
[root@DasonCheng sbin]# /usr/local/sbin/first.sh     //绝对路径执行;
2017年 09月 12日 星期二 15:09:21 CST
hello world

20.3 date命令用法

date  +%Y-%m-%d, date +%y-%m-%d 年月日
date  +%H:%M:%S = date +%T 时间
date +%s  时间戳
date -d @1504620492
date -d "+1day" 一天后
date -d "-1 day" 一天前
date -d "-1 month" 一月前
date -d "-1 min" 一分钟前
date +%w, date +%W 星期


[root@DasonCheng ~]# date +%Y%m%d    //日期Y:year,m:month,d:day
20170912
[root@DasonCheng ~]# date +%y%m%d
170912
[root@DasonCheng ~]# date +%Y-%m-%d
2017-09-12
[root@DasonCheng ~]# date +%F
2017-09-12
[root@DasonCheng ~]# date +%H%M%S    //时间H:hour,M:min,S:second
151534
[root@DasonCheng ~]# date +%H:%M:%S
15:15:53
[root@DasonCheng ~]# date +%T
15:16:34
[root@DasonCheng ~]# date +%w    //星期w:week,W:今年的第几周
2
[root@DasonCheng ~]# date +%W
37
[root@DasonCheng ~]# date +%s    //时间戳:现在距离1970年Linux出来的时候距离多少秒
1505201532
[root@DasonCheng ~]# date +%F
2017-09-12
[root@DasonCheng ~]# date +"%F %T"
2017-09-12 15:32:38
[root@DasonCheng ~]# date -d @1505201532    //时间戳转日期;
2017年 09月 12日 星期二 15:32:12 CST
[root@DasonCheng ~]# date +%s -d "2017-09-12 15:32:38"    //日期转时间戳;
1505201558

20.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]


1、变量:vim variable.sh

_变量的格式:变量名=变量的值
变量:相当于引用或者替代,下面来用脚本来说明: vim variable.sh _

[root@DasonCheng sbin]# cat variable.sh 
#! /bin/bash
#In this script we will use variables;
#Written by chengzhenge 2017-09-12
d=`date +%T`
echo "The script begin at $d"
echo "We will sleep 2 seconds"
sleep 2
d1=`date +%T`
echo "The script end at $d1"
[root@DasonCheng sbin]# sh variable.sh 
The script begin at 15:41:22
We will sleep 2 seconds
The script end at 15:41:24
2、数学运算:vim sum.sh

实现数学运算的方法1:sum=$[$a+$b]
实现数学运算的方法2:sum=$(($a+$b))

[root@DasonCheng sbin]# cat sum.sh 
#! /bin/bash
#For get the sum of two numbers
#Written by chengzhenge 2017-09-12
a=1
b=2
sum=$[$a+$b]    //实现数学运算的方法1:sum=$[$a+$b] 
echo "$a+$b=$sum"

echo "Wait for a moment,i will achieve it too"
sleep 3
#The other way to achieve the sum
a1=1
b1=2
sum1=$(($a1+$b1))    //实现数学运算的方法2:sum=$(($a+$b))
echo "$a1+$b1=$sum1"
[root@DasonCheng sbin]# sh sum.sh 
1+2=3
Wait for a moment,i will achieve it too
1+2=3
3、和用户交互:vim read.sh

:read命令用于和用户交互,它把用户输入的字符串作为变量值;如果没写这个n,可以直接使用$REPLY

[root@DasonCheng sbin]# cat read.sh 
#! /bin/bash
#Using 'read' in shell script
#Written by chengzhenge at 2017-09-12
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"
[root@DasonCheng sbin]# sh read.sh 
please input a number:1
please input another number:2
The sum of the two numbers is: 3
4、内置变量 $0, $1, $2…$#

:内置变量$1就是运行脚本第一个参数,$2是运行脚本第二个参数,$0是代表脚本本身名字,$#是代表运行脚本的参数个数!

[root@DasonCheng sbin]# cat option.sh 
#! /bin/bash
echo "$1,$2,$0,$#"    
//内置$1就是运行脚本第一个参数,$2是运行脚本第二个参数,$0是代表脚本本身名字,$#是代表运行脚本的参数个数!
[root@DasonCheng sbin]# sh option.sh 1 2
1,2,option.sh,2

转载于:https://my.oschina.net/u/3651233/blog/1535866

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值