您的位置:首页 > 运维架构 > Shell

linux基本操作---shell脚本-变量、表达式、日期

2017-12-12 23:58 811 查看

Linux shell解释型脚本语言

直接调用linux命令

Shell 脚本执行的两种方式

sh 脚本文件

./脚本文件 ,需要脚本文件有执行权限

[root@localhost test]# ./test.sh
-bash: ./test.sh: Permission denied

赋执行权限:
[root@localhost test]# chmod u+x ./test.sh
[root@localhost test]# ll
total 4
-rwxr--r--. 1 root root 21 Dec 13 06:22 test.sh

可以执行
[root@localhost test]# ./test.sh


Shell脚本 — 变量

环境变量

全局环境变量,所有用户有效vi /etc/profile

用户环境变量,当前用户有效~/.bash_profile

位置变量

echo 回显变量

Demo:
[root@localhost test]# cat test.sh
#!/bin/bash

echo $0
echo $2
echo $1

执行这个脚本

[root@localhost test]# ./test.sh  arg0 arg1
./test.sh
arg1
arg0

可以看到$0 表示脚本名称
$1 表示第一个参数
$2 表示第二个参数


预定义变量

前面的$0表示脚本名称,还有一些其他的预定义变量

$0 脚本名称
$! 进程的PID号
$? 程序执行的状态 0表示成功 非0不成功
$* 所有参数 整体
$@ 所有参数 逐个
$# 参数的总个数
$$ 当前进程的id号


demo

[root@localhost test]# cat test.sh
#!/bin/bash

echo $0
echo $*
echo $@
echo $$
echo $!
echo $?
echo $#
执行该脚本
[root@localhost test]# ./test.sh arg0 arg1 arg2 arg3
./test.sh
arg0 arg1 arg2 arg3
arg0 arg1 arg2 arg3
3560

0
4


自定义变量

name=[value]

等号两边不能有空格

对大小写敏感

${}符号取值

[root@localhost test]# a=3

[root@localhost test]# echo ${a}
3


Shell 脚本常用表达式

逻辑连接符

&&逻辑与 前面成功后面才执行,前面失败后面不执行。

|| 逻辑或 前面失败执行后面,前面成功后面不执行

Demo:
逻辑与
[root@localhost test]# cat test.sh && echo 3
#!/bin/bash
echo $0
echo $*
echo $@
echo $$
echo $!
echo $?
echo $#
3

[root@localhost test]# cat test.shtse && echo 3
cat: test.shtse: No such file or directory

逻辑或
[root@localhost test]# cat test.sh || echo 3
#!/bin/bash

echo $0
echo $*
echo $@
echo $$
echo $!
echo $?  这个很重要,下面会用到
echo $#

[root@localhost test]# cat test.shtse || echo 3
cat: test.shtse: No such file or directory
3


3.无逻辑符号 ; 他们先后执行

[root@localhost test]# echo $a ; echo $b
2
3


4.连接符号 -a(and) -o(or)

[root@localhost test]# [ -e /etc/passwd -a -e test.sh ];echo $?
0

返回0表示执行才成功 ,-e表示 exists 存在

[root@localhost test]# [ -e /etc/passwdss -a -e test.sh ];echo $?
1

返回1表示执行失败,

[root@localhost test]# [ -e /etc/passwdss -o -e test.sh ];echo $?
0

返回0表示执行成功,这次使用的是-o or的意思


内置判断

文本判断

[root@localhost test]# [ -e /etc/passwdss -o -e test.sh ];echo $?
0

其中-e表示exists 文件是否存在

除了-e之外,还有其他的判断文件的符号

例如:
-e   (exits) 是否存在
-d   (directory) 是否是目录
-f   (file) 是否是文件
-r   (read) 是否可读
-w  (write) 是否可写
-x   (excute) 是否可执行


字符串判断

-z表示 zero string的长度为0
-n 表示 not zero string的长度不为0
[root@localhost test]# arg="test"
[root@localhost test]# echo $arg
test
[root@localhost test]# [ -n "$arg" ] ; echo $?
0
[root@localhost test]# [ -z "$arg" ] ; echo $?
1


数字判断

-eq 等于

-ne 不等于

-lt 小于

-gt 大于

-ge 大于等于

-le 小于等于

Demo:
[root@localhost test]# a=2
[root@localhost test]# b=3
[root@localhost test]# echo $((a+b))
5
[root@localhost test]# a=2
[root@localhost test]# b=3
[root@localhost test]# echo $[a+b]
5
[root@localhost test]# expr $a+$b
2+3


时间命令

查看日期

按照指定的格式
[root@localhost test]# date '+%Y-%m-%d %H:%M:%S'
2017-12-13 07:30:11
按照指定的格式查看前一天日期
[root@localhost test]# date -d '-1 day' '+%Y-%m-%d %H:%M:%S'
2017-12-12 07:30:11
其他的 你也可以搞 -1 month -1 week
按照指定的格式查看前一天日期
[root@localhost test]# date -d '1 day ago' '+%Y-%m-%d %H:%M:%S'
2017-12-12 07:30:11
其他的你也可以搞a week age  a month age 等等


修改日期:

[root@localhost test]# date -s "2017-09-09 12:00"
Sat Sep  9 12:00:00 CST 2017
格式化修改后的日期
[root@localhost test]# date -s "2017-09-09 12:00"  '+%Y-%m-%d %H:%M:%S'
2017-09-09 12:00:00
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息