linux基本操作---shell脚本-变量、表达式、日期
来源:互联网 发布:java一年多的工作经验 编辑:程序博客网 时间:2024/06/06 01:09
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]# lltotal 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/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 脚本常用表达式
逻辑连接符
- &&逻辑与 前面成功后面才执行,前面失败后面不执行。
- || 逻辑或 前面失败执行后面,前面成功后面不执行
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
- linux基本操作---shell脚本-变量、表达式、日期
- shell 脚本日期操作
- linux shell scrip(脚本)编程简介------变量、特殊符号和表达式
- linux shell基本操作
- 【shell脚本学习】2.linux shell变量
- linux shell脚本 变量给变量赋值
- shell脚本专家指南--日期时间操作
- 通过脚本学shell之变量操作
- linux shell脚本操作mysql
- shell脚本:shell的基本元素-2 变量
- shell命令, shell脚本linux操作
- shell脚本的一些基本操作
- linux学习之shell脚本 ------- 变量
- linux shell脚本的基础与变量
- linux shell脚本 && 和 ||及变量说明
- linux 下shell脚本编程之变量
- linux之基础shell脚本编程4 字符串操作,变量赋值,配置用户环境
- shell命令进阶【Linux-shell基本操作】
- ListView滑动置顶,listview吸顶效果,安卓时间栏状态栏透明,有图有源码
- Golang从入门到精通(八):Golang数组
- 八周二次课(12月12日) 14.6 多进程Manager 14.7 进程池
- 雕刻机蓝牙android上位机控制器设计
- 算法系列—— 输出所有的笛卡尔积组合
- linux基本操作---shell脚本-变量、表达式、日期
- 一看就懂的,java深拷贝浅拷贝
- 用ajax实现图片上传 帮你简单快速学会使用
- Python基础-安装第三方模块 pip 和 Anaconda
- 史上最全的Android 图片滤镜 colorMatrix自定义任意图片滤镜
- Golang从入门到精通(九):Golang字符串
- spring加载bean原理
- Lua数据接收问题
- EditText Int