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

来源:互联网 发布:java一年多的工作经验 编辑:程序博客网 时间:2024/06/06 01:09

Linux shell解释型脚本语言

直接调用linux命令

Shell 脚本执行的两种方式

  1. sh 脚本文件
  2. ./脚本文件 ,需要脚本文件有执行权限
[root@localhost test]# ./test.sh-bash: ./test.sh: Permission denied赋执行权限:[root@localhost test]# chmod u+x ./test.sh [root@localhost test]# lltotal 4-rwxr--r--. 1 root root 21 Dec 13 06:22 test.sh可以执行[root@localhost test]# ./test.sh 

Shell脚本 — 变量

环境变量

  1. 全局环境变量,所有用户有效vi /etc/profile
  2. 用户环境变量,当前用户有效~/.bash_profile

位置变量

echo 回显变量

Demo:[root@localhost test]# cat test.sh#!/bin/bashecho $0echo $2echo $1执行这个脚本[root@localhost test]# ./test.sh  arg0 arg1./test.sharg1arg0可以看到$0 表示脚本名称$1 表示第一个参数$2 表示第二个参数

预定义变量

前面的$0表示脚本名称,还有一些其他的预定义变量$0 脚本名称$! 进程的PID$? 程序执行的状态 0表示成功 非0不成功$* 所有参数 整体$@ 所有参数 逐个$# 参数的总个数$$ 当前进程的id号
demo[root@localhost test]# cat test.sh#!/bin/bashecho $0echo $*echo $@echo $$echo $!echo $?echo $#执行该脚本[root@localhost test]# ./test.sh arg0 arg1 arg2 arg3./test.sharg0 arg1 arg2 arg3arg0 arg1 arg2 arg3356004

自定义变量

name=[value]
等号两边不能有空格
对大小写敏感
${}符号取值

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

Shell 脚本常用表达式

逻辑连接符

  1. &&逻辑与 前面成功后面才执行,前面失败后面不执行。
  2. || 逻辑或 前面失败执行后面,前面成功后面不执行
Demo:逻辑与[root@localhost test]# cat test.sh && echo 3#!/bin/bashecho $0echo $*echo $@echo $$echo $!echo $?echo $#3[root@localhost test]# cat test.shtse && echo 3cat: test.shtse: No such file or directory逻辑或[root@localhost test]# cat test.sh || echo 3#!/bin/bashecho $0echo $*echo $@echo $$echo $!echo $?  这个很重要,下面会用到echo $#[root@localhost test]# cat test.shtse || echo 3cat: test.shtse: No such file or directory3

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

[root@localhost test]# echo $a ; echo $b23

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 $argtest[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+$b2+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
阅读全文
0 0
原创粉丝点击