shll脚本格式和规则


脚本文件必须已 .sh 结尾(yuan.sh)

脚本第一行必须是:#!/bin/bash

激活脚本的二种方式(sh yuan.sh)(给脚本X权限,以绝对路径执行脚本)

逻辑与&&前面执行成功后执行后面,如果前面执行不成功则取消 逻辑或|| 前面命令执行失败后,继续执行||后面的命令 ``优先执行反引号里的命令动作

逻辑语判断是文件还是目录

[root@Server yuan]# ls
aaa yunjisuan
[root@Server yuan]# [ -d yunjisuan ] #判断目录
[root@Server yuan]# echo $?
0
[root@Server yuan]# [ -d aaa ] #判断文件
[root@Server yuan]# echo $?
1
[root@Server yuan]# [ -f aaa ]
[root@Server yuan]# echo $?
0

逻辑语判断是大于还是小于

(-gt:大于) (-lt:小于) (-eq:等于)

(-le:小于等于)(-ne:不等于)(-ge:大于等于)

read交互模式

例如:

#!/bin/bash
read -p "请输入你的成绩:" b #设置一变量带入脚本
[ $b -lt 60 ] && echo "不及格"
[ $b -eq 60 ] && echo "恰好及格"
[ $b -ge 60 ] && [ $b -lt 70 ] && echo "良"
[ $b -ge 70 ] && [ $b -lt 85 ] && echo "良好"
[ $b -gt 85 ] && [ $b -le 100 ] && echo "优秀"
[ $b -gt 100 ] && echo "作弊高手" [root@Server ~]# sh yunjisuan.sh # 执行脚本
请输入你的成绩:60
bu'ji'ge

脚本传参格式

[root@Server ~]# vim yunjisuan.sh
#!/bin/bash
echo $#
echo $*
echo $0
echo $1
echo $2
echo $3
echo $4
[root@Server ~]# /root/yunjisuan.sh 1111 2222 #执行脚本
2
1111 2222
/root/yunjisuan.sh
1111
2222
  • $# 传参数的参数总个数

  • $ 横向罗列穿入的参数*

  • $0 文件的绝对路径

  • $1对应传入的不同的参数

  • $n对应传入的不同的参数

判断字符串个数和是否为空 echo ${#变量}

[root@Server ~]# xx=""
[root@Server ~]# echo ${#xx}
0
[root@Server ~]# xx="22"
[root@Server ~]# echo ${#xx}
2
[root@Server ~]# xx="22222"
[root@Server ~]# echo ${#xx}
5

文本交互式插入内容

[root@Server ~]# cat >yuan <<CFY
> 写入内容
> 写入内容
> 写入内容
>CFY
[root@Server ~]# 命令说明:
>yuan 文件名字
<<CFY 结束语

脚本if条件判断

条件单分支判断:如果条件1成功那么执行动作1,否则执行动作2

#!/bin/bash
read -p "请输入一个数字" x if [ $x == 60 ];then
echo "猜对了"
else
echo "猜错了"
fi
[root@Server ~]# sh yunjisuan.sh # 执行脚本
请输入一个数字40
猜错了
[root@Server ~]#

多分支判断:如果条件1成立;那么执行动作1,否则如果条件2成立;执行动作2,否则条件1和2都不成立一律执行动作3

多个条件时加 elif

#!/bin/bash
read -p "请输入一个数字" x if [ $x == 60 ];then
echo "猜对了"
elif [ $x -lt 60 ];then
echo "猜小了"
else
echo "猜大了"
fi
[root@Server ~]# sh yunjisuan.sh # 执行脚本
请输入一个数字:60
猜对了
[root@Server ~]#

脚本for循环

#!/bin/bash
for a in 1 2 3 4 5
do
echo $a全部都是数字
done
[root@Server ~]# sh yunjisuan.sh # 执行脚本
1全部都是数字
2全部都是数字
3全部都是数字
4全部都是数字
5全部都是数字

C语言的for循环

h=0
for b in {1..6}
do
echo $h
((h++)) # 或者 let h++
done
[root@Server ~]# sh yunjisuan.sh
0
1
2
3
4
5
6
[root@Server ~]# 命令说明:
let h++
不进行++的话,变量只会执行一次,++以后就会以此往上加

脚本while无限循环

read -p "随便输入一个数字:" h
while [ $h -gt 0 ]
do
echo $h
let h++
done [root@Server ~]# sh yunjisuan.sh # 执行脚本
随便输入一个数字:5
.....无限循环......
56443
53467
55667
.....无限循环......
[root@Server ~]#

case 语句格式

一般用于菜单选择,用于没有优先级的场合

#!/bin/bash
case $h in
start)
echo "服务启动"
;;
stop)
echo "服务停止"
;;
restart)
echo "服务准备停止"
echo "服务开始启动"
;;
*)
echo "输入错误"
;;
esac [root@Server ~]# /root/yunjisuan.sh start # 绝对路径执行
服务启动
[root@Server ~]# /root/yunjisuan.sh stop
服务停止
[root@Server ~]# /root/yunjisuan.sh restart
服务准备停止
服务开始启动

在脚本中引用一个函数库

#!/bin/bash

. /etc/init.d/functions

case $h in
start)
action "服务启动" /bin/true
;;
stop)
action "服务停止" /bin/false
;;
restart)
action "服务准备停止" /bin/true
action "服务开始启动" /bin/false
;;
*)
action "输入错误"
;;
esac [root@Server ~]# /root/yunjisuan.sh start # 绝对路径执行
服务启动 [ OK ]
[root@Server ~]# /root/yunjisuan.sh stop
服务停止 [FAILED]
[root@Server ~]# /root/yunjisuan.sh restart
服务准备停止 [ OK ]
服务开始启动 [FAILED]
[root@Server ~]#

把脚本加入到chkconfig里进行管理

[root@Server ~]# cp /root/yunjisuan.sh /etc/init.d/
[root@Server ~]# vim /etc/init.d/yunjisuan.sh
#!/bin/bash
#chkconfig: 35 90 10
......一下忽略..... [root@Server ~]# chkconfig --add yunjisuan.sh
[root@Server ~]# chkconfig --list yunjisuan.sh
.....省略.....

函数的格式用法

#!/bin/bash

function CFY(){
echo "吃饭"
echo "上班"
echo "睡觉"
echo "111111111"
} CFY
CFY [root@Server ~]# sh yunjisuan.sh # 执行脚本
吃饭
上班
睡觉
111111111
吃放
上班
睡觉
111111111

脚本4种循环控制语句

  • exit 强行终止脚本
#!/bin/bash
h=0
while [ $h -lt 20 ]
do
if [ $h -eq 5 ];then
exit
fi
echo $h
let ++
done echo "脚本继续执行" [root@Server ~]# sh yunjisuan.sh # 执行脚本
0
1
2
3
4
5
  • break 退出当前最近的循环
#!/bin/bash
h=0
while [ $h -lt 20 ]
do
if [ $h -eq 5 ];then
break
fi
echo $h
let ++
done echo "脚本继续执行" [root@Server ~]# sh yunjisuan.sh # 执行脚本
0
1
2
3
4
5
脚本继续执行
  • continue 终止当前本次循环进入下次循环
#!/bin/bash
h=0
while [ $h -lt 20 ]
do
let $h++
if [ $h -eq 5 ];then
continue
fi
echo $h
done echo "脚本继续执行" [root@Server ~]# sh yunjisuan.sh # 执行脚本
0
1
...忽略...
19
20
脚本继续执行
  • return 强行跳出函数体系从哪开始到那继续

shll脚本常用格式和规则使用的更多相关文章

  1. C#常用的命名规则汇总

    C#常用的命名规则汇总 来源 https://www.cnblogs.com/pengyouqiang88/p/5021128.html 本文转载自脚本之家 本文详细汇总了C#常用的命名规则.分享给大 ...

  2. 常用的acl规则

    一.常用的acl规则        haproxy的ACL用于实现基于请求报文的首部.响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性.其配置法则通常分为两步,首先去定义ACL ...

  3. Struts2 验证框架 validation.xml 常用的验证规则

    validation.xml 的命名规则和放置路径: 文件名:<ActionClassName>-validation.xml <ActionClassName>就是要验证的A ...

  4. SCI/EI期刊投稿 Reply Letter 常用格式总结

    SCI/EI期刊投稿Reply Letter常用格式总结          整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...

  5. C#常用格式输出

    ylbtech- .NET-Basic:C#常用格式输出 C#常用格式输出 1.A,相关概念返回顶部 using System; namespace Test { class Formating { ...

  6. Swift - 正则表达式的使用(附用户名、邮箱、URL等常用格式验证)

    Swift虽然是一个新出的语言,但却不提供专门的处理正则的语法和类.所以我们只能使用古老的NSRegularExpression类进行正则匹配. 即先接受一个正则表达式的字符串,由此生成NSRegul ...

  7. markdown 常用格式API

    摘要 记录常用格式 参考:https://www.zybuluo.com/mdeditor 1. 标题 写法: 文字前加 #, 几个# 表示几级标题 标题下方增加 = 或 - 效果 标题1 标题2 标 ...

  8. js常用身份校验规则

    js常用身份校验规则 var Validator = { extractBirth: function(id) { // 身份证提取出生年月 var re = null, split, year, m ...

  9. MarkDown常用格式

    常用格式 ** :加粗 <br> : 换行 > :可以用来引用文章,很漂亮. 可以展开的文件夹格式 <details> <summary>框架</sum ...

随机推荐

  1. 火焰图--记一次cpu降温过程

    引子 正值周末,娃儿6:30又如闹铃般准时来叫醒了我们.年前离开美菜,又回到了杭州.原本是想有更多时间陪伴娃儿,然而新的工作节奏与工作地点,让我们每天都是早上见面:这不,为了周末可以多玩一会儿,早早就 ...

  2. .NET Core项目部署到Linux(Centos7)(八)为.NET Core项目创建Supervisor进程守护监控

    目录 1.前言 2.环境和软件的准备 3.创建.NET Core API项目 4.VMware Workstation虚拟机及Centos 7安装 5.Centos 7安装.NET Core环境 6. ...

  3. ln 软连接与硬连接

                                                                                                        ...

  4. Vulnhub JIS-CTF-VulnUpload靶机渗透

    配置问题解决 参考我的这篇文章https://www.cnblogs.com/A1oe/p/12571032.html更改网卡配置文件进行解决. 信息搜集 找到靶机 nmap -sP 192.168. ...

  5. Hadoop安装教程_伪分布式

    文章更新于:2020-04-09 注1:hadoop 的安装及单机配置参见:Hadoop安装教程_单机(含Java.ssh安装配置) 注2:hadoop 的完全分布式配置参见:Hadoop安装教程_分 ...

  6. Linux服务器架设篇,Windows中的虚拟机linux上不了外网怎么办?

    1.将电脑的网线口直连路由器内网接口(确保该路由器可以直接正常上网,切记不可以使用宽带连接和无线网连接). 2.在实体机电脑可以上网的前提下,在命令框窗口输入 ipconfig 3.记录下电脑以太网的 ...

  7. matplotlib BboxBase类

    2020-04-07 17:24:12  --Edit by yangray BboxBase 是 TransformNode 的子类, 同时它是所有 bounding box(平行四边形限位框) 的 ...

  8. NumPy学习2:基本运算

    数组相减: a = array([20, 30, 40, 50])print ab = arange(4)print bc = a-bprint c 结果: [20 30 40 50][0 1 2 3 ...

  9. shell脚本编程(ubantu)

    项目 内容 这个作业属于那个课程 这里是链接 作业要求在哪里 这里是链接 学号-姓名 17041506-张政 作业学习目标 了解shell脚本的概念及使用:掌握shell脚本语言的基本语法:学习简单的 ...

  10. std::string::insert函数

    string& insert (size_t pos, const string& str); string& insert (size_t pos, const string ...