shell脚本中的dat,计算器,内置变量的用法
什么是shell脚本。首先它是一个脚本,并不能作为正式的编程语言。因为是跑在linux的shell中,所以叫shell脚本。说白了,shell脚本就是一些命令的集合。举个例子,我想实现这样的操作: 1)进入到/tmp/目录; 2)列出当前目录中所有的文件名; 3)把所有当前的文件拷贝到/root/目录下; 4)删除当前目录下所有的文件。 简单的4步在shell窗口中需要你敲4次命令,按4次回车。这样是不是很麻烦?所以不妨把所有的操作都记录到一个文档中,然后去调用文档中的命令,这样一步操作就可以完成。其实这个文档呢就是shell脚本了,只是这个shell脚本有它特殊的格式。 Shell脚本能帮助我们很方便的去管理服务器,因为我们可以指定一个任务计划定时去执行某一个shell脚本实现我们想要需求。比如利用邮件的便利,我们可以在我们的linux服务器上部署监控的shell脚本,比如网卡流量有异常了或者服务器web服务器停止了就可以发一封邮件给管理员,同时发送给管理员一个报警短信这样可以让我们及时的知道服务器出问题了。 凡是自定义的脚本建议放到/usr/local/sbin/目录下,这样做的目的是,一来可以更好的管理文档;二来以后接管你的管理员都知道自定义脚本放在哪里,方便维护。 shell脚本的基本结构以及如何执行第一个shell脚本 [root@localhost ~]# cd /usr/local/sbin/ [root@localhost sbin]# vim first.sh #! /bin/bash ## This is my first shell script. ## 2017.09.02. date echo "Hello world!" [root@localhost sbin]# sh first.sh 2017年 09月 2日 星期六 18:58:02 CST Hello world! 其实shell脚本还有一种执行方法就是: 其实shell脚本还有一种执行方法就是: [root@localhost sbin]# ./first.sh
-bash: ./first.sh: 权限不够
[root@localhost sbin]# chmod +x first.sh
[root@localhost sbin]# ./first.sh
2017年 09月 02日 星期六 18:58:51 CST
Hello world!
要想使用该种方法运行shell脚本,前提是脚本本身有执行权限,所以需要给脚本加一个 ‘x’ 权限。另外使用sh命令去执行一个shell脚本的时候是可以加-x选项来查看这个脚本执行过程的,这样有利于我们调试这个脚本哪里出了问题: [root@localhost sbin]# sh -x first.sh
+ date
2017年 09月 02日 星期六 20:00:11 CST
+ echo 'Hello world!'
Hello world!
[root@localhost sbin]# date +"%Y-%m-%d %H:%M:%S"
2017-09-2 19:41:01
date在脚本中最常用的几个用法:
有时在脚本中会用到一天前的日期: [root@localhost sbin]# date -d "-1 day" +%d
1
或者一小时前: [root@localhost sbin]# date -d "-1 hour" +%H
18
甚至1分钟前: [root@localhost sbin]# date -d "-1 min" +%M
50
shell脚本中的变量如果你写了一个长达1000行的shell脚本,并且脚本中出现了某一个命令或者路径几百次。突然你觉得路径不对想换一下,那岂不是要更改几百次?你固然可以使用批量替换的命令,但也是很麻烦,并且脚本显得臃肿了很多。变量的作用就是用来解决这个问题的。 [root@localhost sbin]# cat variable.sh
#! /bin/bash
## In this script we will use variables.
## Writen by Aming 2017-09-2.
d=`date +%H:%M:%S`
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’ 在脚本中作为变量出现,定义变量的格式为当在脚本中引用变量时需要加上 ‘$’ 符号,下面看看脚本执行结果吧:变量名=变量的值
[root@localhost sbin]# sh variable.sh
The script begin at 20:16:57.
Now we'll sleep 2 seconds.
The script end at 20:16:59.
下面我们用shell计算两个数的和: [root@localhost sbin]# cat sum.sh
#! /bin/bash
## For get the sum of tow numbers.
## 2017.09.02.
a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"
数学计算要用[ ]括起来并且外头要带一个 ‘$’ 脚本结果为: [root@localhost sbin]# sh sum.sh
1+2=3
Shell脚本还可以和用户交互: [root@localhost sbin]# cat read.sh
#! /bin/bash
## Using 'read' in shell script.
## 2017.09.02.
read -p "Please input a number: " x
read -p "Please input another number: " y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum"
read 命令就是用在这样的地方,用于和用户交互,把用户输入的字符串作为变量值。脚本执行过程如下: [root@localhost sbin]# sh read.sh
Please input a number: 2
Please input another number: 10
The sum of the two numbers is: 12
有时候我们会用到这样的命令前面的/etc/init.d/iptables文件其实就是一个shell脚本,为什么后面可以跟一个 “restart”? 这里就涉及到了shell脚本的预设变量。实际上,shell脚本在执行的时候后边是可以跟参数的,而且还可以跟多个。
[root@localhost sbin]# cat option.sh
#! /bin/bash
sum=$[$1+$2]
echo "sum=$sum"
执行结果为: [root@localhost sbin]# sh -x option.sh 1 2
+ sum=3
+ echo sum=3
sum=3
[root@localhost sbin]# cat option.sh
#! /bin/bash
echo "$1 $2 $0"
执行结果: [root@localhost sbin]# sh option.sh 1 2
1 2 option.sh
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |