20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量

前端之家收集整理的这篇文章主要介绍了20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
-20.1shell脚本介绍
-20.2shell脚本结构和执行
-20.3date命令用法
-20.4shell脚本中的变量
#20.1Shell脚本介绍
-shell是一种脚本语言关注aming_linuxblog.lishiming.net
-可以使用逻辑判断、循环等语法
-可以自定义函数
-shell是系统命令的集合
-shell脚本可以实现自动化运维,能大大增加我们的运维效率








#20.2Shell脚本结构和执行
-开头需要加#!/bin/bahs//告诉系统,这个脚本是通过哪一个解释器来进行操作的
-以#开头的行作为解释说明
-脚本的名字以.sh结尾,用于区分这是一一个shell脚本
-先创建一个shell目录
```
[root@aming-01~]#clear
[root@aming-01~]#mkdirshell
[root@aming-01~]#cdshell/
[root@aming-01shell]#ls
[root@aming-01shell]#vi01.sh

#!/bin/bash
echo"123"
w
ls
~

~
:wq
[root@aming-01shell]#vi01.sh
```

-执行方法有两种:
chmod+x1.sh;./1.sh
bash1.sh,sh1.sh
```
[root@aming-01shell]#sh01.sh
123
21:07:19up5min,2users,loadaverage:0.04,0.33,0.20
USERTTYFROMLOGIN@IDLEJcpuPcpuWHAT
roottty121:024:390.02s0.02s-bash
rootpts/0192.168.202.121:037.00s0.03s0.01sw
01.sh
[root@aming-01shell]#
```
-还有一种,文件开头的意思就是意味着接下来的命令是由这个文件来解析的。
```
[root@aming-01shell]#chmoda+x01.sh
[root@aming-01shell]#./01.sh
123
21:09:48up7min,loadaverage:0.14,0.23,0.18
USERTTYFROMLOGIN@IDLEJcpuPcpuWHAT
roottty121:027:080.02s0.02s-bash
rootpts/0192.168.202.121:034.00s0.03s0.00s/bin/bash./01.sh
01.sh
[root@aming-01shell]#
[root@aming-01shell]#cat01.sh
#!/bin/bash
echo"123"
w
ls
[root@aming-01shell]#

```
-bin/bash其实就是bin/sh
```
[root@aming-01shell]#ls-l/bin/bash
-rwxr-xr-x.1rootroot9603928月32016/bin/bash
[root@aming-01shell]#ls-l/bin/sh
lrwxrwxrwx.1rootroot410月522:18/bin/sh->bash
[root@aming-01shell]#

```

-查看脚本执行过程,每个+表示一个步骤
```
[root@aming-01shell]#sh-x01.sh
+echo123
123
+w
21:17:01up14min,loadaverage:0.02,0.07,0.12
USERTTYFROMLOGIN@IDLEJcpuPcpuWHAT
roottty121:0214:210.02s0.02s-bash
rootpts/0192.168.202.121:035.00s0.05s0.00ssh-x01.sh
+ls
01.sh
[root@aming-01shell]#
```
-也可以用sh-n查看脚本的语法是否正确
-没有输出,表示没有错误

```
[root@aming-01shell]#sh-n01.sh
[root@aming-01shell]#
```
-修改一下文件测试一下错误
```
[root@aming-01shell]#sh-n01.sh
[root@aming-01shell]#vi01.sh

#!/bin/bash
echo"123"
w
ls
foriin`seq110`
do
echo$i
~

~
~
:wq

```
-再执行下sh-n看下
```
[root@aming-01shell]#sh-n01.sh
01.sh:行8:语法错误:未预期的文件结尾
[root@aming-01shell]#
```
-改回来
```
[root@aming-01shell]#vi01.sh

#!/bin/bash
echo"123"
w
ls
~
~
~

:wq
```
-再监测就是没用问题
```
[root@aming-01shell]#sh-n01.sh
[root@aming-01shell]#
```







#20.3date命令用法
-date在shell中用处非常大;对文件后缀增加一个时间,以便后期管理
-date+%Y-%m-%d,date+%y-%m-d年月日
```
[root@aming-01~]#date
2017年11月20日星期一22:01:01CST
[root@aming-01~]#
```
-m月,M分钟
```
[root@aming-01~]#date
2017年11月20日星期一22:01:01CST
[root@aming-01~]#date+%Y
2017
[root@aming-01~]#date+%y
17
[root@aming-01~]#date+%m
11
[root@aming-01~]#date+%M
27
[root@aming-01~]#
```
-日期d大D
```
[root@aming-01~]#date+%d
20
[root@aming-01~]#date+%D
11/20/17
[root@aming-01~]#
```
-日期,年月日date+%Y%m%d
```
[root@aming-01~]#date+%Y%m%d
20171120
[root@aming-01~]#
```
-日期date+%F
```
[root@aming-01~]#date+%F
2017-11-20
[root@aming-01~]#
```
-小时,分,秒
-s表示一个时间戳表示距离1970年1月1日0点0分到现在的总共多少秒
```
[root@aming-01~]#date+%H
22
[root@aming-01~]#date+%s
1511188397
[root@aming-01~]#date+%S
22
[root@aming-01~]#date+%S
32
[root@aming-01~]#
```
-关于时间
-date+%T
-date+%H%M%S
```
[root@aming-01~]#date+%T
22:35:38
[root@aming-01~]#date+%H%M%S
223608
[root@aming-01~]#
```
-h显示月份,
```
[root@aming-01~]#date+%h
11月
[root@aming-01~]#
```
-先把语言改成英文
```
[root@aming-01~]#LANG=en
[root@aming-01~]#date+%h
Nov
[root@aming-01~]#
```
-加上冒号date+%H:%M:%S等同于date+%T
```
[root@aming-01~]#date+%H:%M:%S
22:42:38
[root@aming-01~]#date+%T
22:42:42
[root@aming-01~]#
```
-周
-w周几
-W今年的第几周
```
[root@aming-01~]#date+%w
1
[root@aming-01~]#date+%W
47
[root@aming-01~]#
```
-显示日历cal
```
[root@aming-01~]#cal
November2017
SuMoTuWeThFrSa
1234
567891011
12131415161718
19202122232425
2627282930

[root@aming-01~]#
```
-date-d"+1day"+%F一天后
-date-d"-1day"+%F一天前
-date-d"-1month"+%F一个月前
-date-d"-1min"+%F一分钟前

-date-d"-1year"+%F一年前

-标记昨天的日期date-d"-1day"
```
[root@aming-01~]#date
MonNov2023:07:38CST2017
[root@aming-01~]#date-d"-1day"
SunNov1923:07:46CST2017
[root@aming-01~]#
[root@aming-01~]#date-d"-1day"+%F
2017-11-19
[root@aming-01~]#

```
-上个月date-d"-1month"
```
[root@aming-01~]#date-d"-1month"+%F
2017-10-20
[root@aming-01~]#
```
-上一年date-d"-1year"
```
[root@aming-01~]#date-d"-1year"+%F
2016-11-20
[root@aming-01~]#date-d"-1year"
SunNov2023:10:43CST2016
[root@aming-01~]#
```
-上一个小时date-d"-1hour"
```
[root@aming-01~]#date-d"-1hour"+%T
22:11:43
[root@aming-01~]#
```
-时间戳转换成具体的日期时间,也可以现在的时间转换成时间戳
```
[root@aming-01~]#date+%s
1511190759
[root@aming-01~]#

[root@aming-01~]#date-d@1511190759
MonNov2023:12:39CST2017
[root@aming-01~]#
[root@aming-01~]#date+%s-d"2017-11-2023:12:39"
1511190759
[root@aming-01~]#
```








#20.4Shell脚本中的变量
-当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
-使用条件语句时,常使用变量if[$a-gt1];then...;fi
-引用某个命令的结果时,用变量替代n=`wc-l1.txt`
-写和用户交互的脚本时,变量也是必不可少的read-p"Inputanumber:"n;echo$n如果没写这个n,可以直接使用$REPLY
-内置变量$0,$1,$2…$0表示脚本本身,$1第一个参数,$2第二个....$#表示参数个数
-数学运算a=1;b=2;c=$(($a+$b))或者$[$a+$b]
原文链接:https://www.f2er.com/bash/389599.html

猜你在找的Bash相关文章