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
相关文章推荐
- linux之基础shell脚本编程4 字符串操作,变量赋值,配置用户环境
- 如何编写简单的Shell脚本(Script)文件之Linux的基本操作
- linux——Shell 脚本基础篇(变量类型,变量操作,定义,运算与逻辑关系)
- Shell脚本运行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell推断hdfs文件文件夹是否存在
- Linux——Shell脚本的基本应用
- Linux学习之shell脚本——变量
- linux的shell脚本介绍、shell脚本结构和执行、date命令用法、shell脚本中的变量
- linux操作shell脚本定义数组赋值取值
- shell命令进阶【Linux-shell基本操作】
- shell命令, shell脚本linux操作
- linux 基础学习之五:shell脚本学习(2)——基本语法
- Linux的简单shell脚本中修改文件操作
- Linux系统shell脚本判断变量文件目录:权限、是否存在、空值、相等
- shell脚本的一些基本操作
- [Linux] shell bash 脚本特殊变量 [大三TJB_708]
- linux下MongoDB客户端shell基本操作
- linux脚本编写基础(环境变量、shell流程控制)
- Linux命令行与shell脚本(7)--创建基本脚本文件
- CentOS Linux自动备份MySQL数据库到远程FTP服务器并删除指定日期前的备份Shell脚本(一)
- Linux-Shell脚本编程-学习-3-Shell编程-shell脚本基本格式