shell 一
1.shell是什么
shell是一种脚本语言
可以使用逻辑判断、循环等语法
可以自定义函数
shell是系统命令的集合
shell脚本可以实现自动化运维,能大大增加我们的运维效率
2.shell脚本结构和执行
开头需要加#!/bin/bash
以#开头的行作为解释说明
脚本的名字以.sh结尾,用于区分这是一个shell脚本
[root@feature1 ~]# cat 1.sh
#!/bin/bash
touch /tmp/1.txt
chmod 600 /tmp/1.txt
mv /tmp/1.txt /tmp/2.txt
[root@feature1 ~]# bash 1.sh
[root@feature1 ~]# bash -x 1.sh
+ touch /tmp/1.txt
+ chmod 600 /tmp/1.txt
+ mv /tmp/1.txt /tmp/2.txt
[root@feature1 ~]# bash -n 1.sh
#-n 查看有没有语法错误
执行方法有两种
chmod +x 1.sh; ./1.sh
bash 1.sh
查看脚本执行过程 bash -x 1.sh
查看脚本是否语法错误 bash -n 1.sh
3.date命令
date +%Y-%m-%d, date +%y-%m-%d 年月日
date +%H:%M:%S = date +%T 时间
[root@feature1 ~]# date +%Y-%m-%d
2019-03-08
[root@feature1 ~]# date +%Y
2019
[root@feature1 ~]# date +%y
19
[root@feature1 ~]# date +%m
03
[root@feature1 ~]# date +%d
08
[root@feature1 ~]# date +%M
43
[root@feature1 ~]# date +%H
15
[root@feature1 ~]# date +%S
41
[root@feature1 ~]# date +%F
2019-03-08
[root@feature1 ~]# date +%T
15:43:51
[root@feature1 ~]# date +%Y@%m@%d
2019@03@08
date +%s 时间戳
[root@feature1 ~]# date +%s
1552031164
date -d @1504620492
[root@feature1 ~]# date -d @1
Thu Jan 1 08:00:01 CST 1970
[root@feature1 ~]# date -d @1552031164
Fri Mar 8 15:46:04 CST 2019
date -d "+1day" 一天后
date -d "-1 day" 一天前
date -d "-1 month" 一月前
date -d "-1 min" 一分钟前
[root@feature1 ~]# date -d "+1day"
Sat Mar 9 15:48:31 CST 2019
[root@feature1 ~]# date -d "-1day"
Thu Mar 7 15:48:40 CST 2019
[root@feature1 ~]# date -d "-1hour"
Fri Mar 8 14:48:47 CST 2019
[root@feature1 ~]# date -d "+hour"
Fri Mar 8 16:48:52 CST 2019
[root@feature1 ~]# date -d "+week"
Fri Mar 15 15:48:59 CST 2019
date +%w, date +%W 星期
[root@feature1 ~]# date +%w
5
[root@feature1 ~]# date +%W
09
[root@feature1 ~]# date -d "-70day" +%W
52
[root@feature1 ~]# date -d "-50day" +%W
02
4.shell脚本中的变量
当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi
引用某个命令的结果时,用变量替代 n=`wc -l 1.txt`
写和用户交互的脚本时,变量也是必不可少的
read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY
内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数
[root@feature1 ~]# a=`date +%w`
[root@feature1 ~]# echo $a
[root@feature1 ~]# a=$(date +%W)
[root@feature1 ~]# echo $a
09
[root@feature1 ~]# read -p "请输入一个数字:" n
请输入一个数字:8
[root@feature1 ~]# echo $n
8
[root@feature1 ~]# read -p "请输入一个数字:"
请输入一个数字:6
[root@feature1 ~]# echo $REPLY
6
[root@feature1 ~]# vim 2.sh
#!/bin/bash
echo "\$1=$1"
echo "第二个参数是$2"
echo "第二个参数是$3"
echo "本脚本一共有$#个参数"
[root@feature1 ~]# sh 2.sh 1 2 3 a
$1=1
第二个参数是2
第二个参数是3
本脚本一共有4个参数
[root@feature1 ~]# sh 2.sh aa bb cc dd
$1=aa
第二个参数是bb
第二个参数是cc
本脚本一共有4个参数
[root@feature1 ~]# sh 2.sh aa bb cc dd 55
$1=aa
第二个参数是bb
第二个参数是cc
本脚本一共有5个参数
[root@feature1 ~]# sh 2.sh aa bb
$1=aa
第二个参数是bb
第二个参数是
本脚本一共有2个参数
[root@feature1 ~]# cat 2.sh
#!/bin/bash
echo "\$1=$1"
echo "第二个参数是$2"
echo "第二个参数是$3"
echo "本脚本一共有$#个参数"
echo “\$0是$0”
[root@feature1 ~]# sh 2.sh 2 o
$1=2
第二个参数是o
第二个参数是
本脚本一共有2个参数
“$0是2.sh”
[root@feature1 ~]# chmod +x 2.sh
[root@feature1 ~]# ./2.sh
$1=
第二个参数是
第二个参数是
本脚本一共有0个参数
“$0是./2.sh”
数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
[root@feature1 ~]# a=1
[root@feature1 ~]# b=2
[root@feature1 ~]# c=$(($a+$b))
[root@feature1 ~]# echo $c
3
[root@feature1 ~]# c=$[$a+$b]
[root@feature1 ~]# echo $c
3
# $(($a+$b))=$[$a+$b]
5.shell中的逻辑判断
格式1:if 条件 ; then 语句; fi
格式2:if 条件; then 语句; else 语句; fi
格式3:if …; then … ;elif …; then …; else …; fi
逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
[root@feature1 ~]# vim if1.sh
#!/bin/bash
a=10
if [ $a -gt 5]
then
echo ok
fi
[root@feature1 ~]# sh -x if1.sh
+ a=10
+ '[' 10 -gt '5]'
if1.sh: line 3: [: missing `]'
#直接命令行也是可以的。换行用;
[root@feature1 ~]# a=10; if [ $a -gt 5 ]; then echo ok; fi
ok
[root@feature1 ~]# a=10
[root@feature1 ~]# if [ $a -gt 5 ]
> then
> echo ok
> fi
ok
[root@feature1 ~]# if [ $a -gt 5 ]; then echo ok; fi
[root@feature1 ~]# if [ $a -gt 5 ]; then echo ok; else echo "not ok"; fi
ok
[root@feature1 ~]# vim if1.sh
[root@feature1 ~]# cat if1.sh
#!/bin/bash
a=10
if [ $a -gt 5]
then
echo ok
else
echo "not ok"
fi
[root@feature1 ~]# vim if2.sh
[root@feature1 ~]# cat if2.sh
#!/bin/bash
a=2
if [ $a -gt 5 ]
then
echo ok
elif [ $a -gt 3 ]
then
echo "very ok"
else
echo "not ok"
fi
[root@feature1 ~]# sh -x if2.sh
+ a=2
+ '[' 2 -gt 5 ']'
+ '[' 2 -gt 3 ']'
+ echo 'not ok'
not ok
#逻辑判断的嵌套
[root@feature1 ~]# vim if3.sh
[root@feature1 ~]# cat if3.sh
#!/bin/bash
a=10
if [ $a -gt 5 ]
then
if [ $a -lt 20 ]
then
echo "ok"
else
echo "very ok"
fi
else
echo "not ok"
fi
[root@feature1 ~]# sh -x if3.sh
+ a=10
+ '[' 10 -gt 5 ']'
+ '[' 10 -lt 20 ']'
+ echo ok
ok
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then == if [ $a -get 5 -a $a -lt 10]
if [ $b -gt 5 ] || [ $b -lt 3 ]; then == if [$b -gt 5 -0 $ -lt 3]
6.if 判断文件、目录属性
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[root@feature1 ~]# if [ -f 1.sh www.xycheng178.com]; then echo 1; fi
1
[root@feature1 ~]# if [ -d 1.sh ]; then echo 1; fi
[root@feature1 ~]# ls -l /tmp/mysql.sock
srwxrwxrwx. 1 mysql mysql 0 Mar 5 09:38 /tmp/mysql.sock
[root@feature1 ~]# if [ -d /tmp/mysql.sock ]; then echo 1; fi
[root@feature1 ~]# if [ -f /tmp/mysql.sock ]; then echo 1; fi
[root@feature1 ~]# if [ -d /tmp/mysql.sock ]; then echo 1; fi
[root@feature1 ~]# if [ www.yongshi123.cn-e /tmp/mysql.sock ]; then echo 1; fi
1
[root@feature1 ~]# if [ www.dfgjpt.com-e /tmp/ ]; then echo 1; fi
1
[root@feature1 ~]# if [ www.68076.cn -e /tmp/12 ]; then echo 1; fi
#判断/root/ 下有没有ab.sh 没有的话 执行echo "1111">ab.sh
[root@feature1 ~]# if [www.yinmaoyule178.com -f /root/ab.sh ]; then echo 1; else echo "1111" >ab.sh; fi
[root@feature1 ~]# cat ab.sh
1111
root用户对文件的读写比较特殊,即使一个文件没有给root用户读或者写的权限,root用户照样可以读或者写
7.if判断的一些特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
[root@feature1 ~]# a=
[root@feature1 ~]# echo $a
[root@feature1 ~]# if [ -z "$a" ]; then echo "a为空"; fi
a为空
[root@feature1 ~]# a=1
[root@feature1 ~]# if [ -z "$a" ]; then echo "a为空"; fi
#加!是取反
[root@feature1 ~]# if [ ! -z "$a" ]; then echo "a不为空"; fi
a不为空
[root@feature1 ~]# if [ www.120xh.cn-n "$a"www.68079.cn ]; then echo "a不为空"; fi
a不为空
[root@feature1 ~]# ls 123
[root@feature1 ~]# echo $?
0
[root@feature1 ~]# ls 35
ls: cannot access 35: No such file or directory
[root@feature1 ~]# echo $?
2
[root@feature1 ~]# if ! ls 35 ; then echo "指令执行不成功"; fi
ls: cannot access 35: No such file or directory
指令执行不成功
[root@feature1 ~]# if !www.dfgjyl.cn ls 35 &>/dev/null; then echo "指令执行不成功"; fi
指令执行不成功
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样 if [ ! -e file ]; then 表示文件不存在时会怎么样
[root@feature1 ~]# if grep -q www.jiahuayulpt.com'123' 1.sh ; then echo "1.sh文件中包含有123关键字"; fi
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
[root@feature1 ~]# if (($a>2))www.thd178.com/ ; then echo "a大于2"; fi
[root@feature1 ~]# a=10
[root@feature1 ~]# if (($a>2)); then echo "a大于2"; fi
shell 一的更多相关文章
- Shell替换
如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...
- Shell特殊变量
$ 表示当前Shell进程的ID,即pid $echo $$ 运行结果 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数 ...
- shell变量
定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- shell简介
Shell作为命令语言,它交互式地解释和执行用户输入的命令:作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支. shell使用的熟练程度反映了用户对U ...
- Shell碎碎念
1. 字符串如何大小写转换 str="This is a Bash Shell script." 1> tr方式 newstr=`tr '[A-Z]' '[a-z]' < ...
- MongoDB学习笔记二—Shell操作
数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...
- 使用C#给Linux写Shell脚本
在这个逼格决定人格,鄙视链盛行的年头,尤其是咱们IT界,请问您今天鄙视与被鄙视的次数分别是多少?如果手中没有一点压箱的本事,那就只有看的份了.今天我们也要提升下自己的格调,学习些脑洞大开的东西,学完之 ...
- Linux环境下shell和vim中乱码原因及消除办法
shell和vim中乱码原因及消除办法 作者:Jack47 在Linux下开发,经常遇到乱码问题:shell或者vim中显示不了中文,或者能够显示,但不能输入中文.每次都是上网去搜,或者同事告诉我一些 ...
- 【说解】在shell中通过mkfifo创建命名管道来控制多个进程并发执行
背景: 工作中有两个异地机房需要传数据,数据全名很规范,在某个目录下命名为统一的前缀加上编号.如/path/from/file.{1..100}.而机房间的专线对单个scp进程的传输速度是有限制的,比 ...
随机推荐
- xshell替代工具finalShell
主要特性:1.多平台支持Windows,Mac OS X,Linux2.多标签,批量服务器管理.3.支持登录Ssh和Windows远程桌面.4.漂亮的平滑字体显示,内置100多个配色方案.5.终端,s ...
- vim-plug 插件安装与操作
安装 vim-plug curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/juneg ...
- .Net版本依赖之坑引发的搜查
前言 今天上午,一个客户反馈XX消息没有推送到第三方链接.于是我查看了推送日志列表,并没有今天的.接着登录服务器查询文件日志,看到了记录.我们的代码步骤是消息先推送到消息队列,消费消息队列时,记录文件 ...
- 线程池ThreadPoolExecutor整理
项目用到线程池,但是其实很多人对原理并不熟悉 ,这里只是整理一下 ThreadPoolExecutor java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心 ...
- Awesome Python,Python的框架集合
Awesome Python A curated list of awesome Python frameworks, libraries and software. Inspired by awes ...
- 浅谈meta viewport设置移动端自适应
1.viewport 移动设备上的viewport是设备屏幕上用来显示网页的那部分区域,再具体一点就是浏览器上用来显示网页的那部分区域,但viewport又不局限于浏览器可视区域的大小,它可能比浏览器 ...
- Vuex实现原理解析
我们在使用Vue.js开发复杂的应用时,经常会遇到多个组件共享同一个状态,亦或是多个组件会去更新同一个状态,在应用代码量较少的时候,我们可以组件间通信去维护修改数据,或者是通过事件总线来进行数据的传递 ...
- M2事后分析
计划 1. 你原计划的工作是否最后都做完了? 如果有没做完的,为什么? 修复了M1阶段的bug,整合前两组的数据.扩充功能,和学霸组达成功能上的一致,对数据库进行信息的完善. 2. 有没有发现你做了一 ...
- jisuanqi
1.jisuanqi 2.https://github.com/12wangmin/text/tree/master 3.计算截图 7+8 清除 4.总结 通过课程设计,主要要达到两个目的,一是检验和 ...
- 20150401 作业2 结对 四则运算(Doing)
import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class SE2_1 {/ ...