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 强行跳出函数体系从哪开始到那继续

最新文章

  1. .NET Core采用的全新配置系统[4]: “Options模式”下各种类型的Options对象是如何绑定的?
  2. javascript数组去重的两个方法
  3. Python 入门简介(一)
  4. 清除SQLServer日志的两种方法
  5. Android 在内部存储读写文件
  6. asp.net mvc4使用百度ueditor编辑器
  7. 用一天的时间学习Java EE中的SSH框架
  8. poj 2229 Sumsets DP
  9. 6. Java 加解密技术系列之 3DES
  10. STL源码剖析之序列式容器
  11. java安装及设置环境变量
  12. linux入门--Linux系统的优缺点
  13. 23 , CSS 构造列表与导航
  14. mobile_音悦台
  15. vue 项目实战 (生命周期钩子)
  16. RHEL7安装配置VNC
  17. sql语句之表间字段值复制遇到的一些问题--基于mysql
  18. codevs 1576 最长严格上升子序列
  19. noip 邮票面值设计 - 搜索 - 动态规划
  20. Numpy 数组的切片操作

热门文章

  1. 《Mathematical Analysis of Algorithms》中有关“就地排列”(In Situ Permutation)的算法分析
  2. +load 和 +initialize
  3. SpringBoot使用RedisTemplate操作Redis时,key值出现 \xac\xed\x00\x05t\x00\tb
  4. JSP学习笔记(二)
  5. MATLAB——时间,日期及显示格式
  6. Codeforces 631 (Div. 2) E. Drazil Likes Heap 贪心
  7. Jenkins集成时报错 hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx:32034
  8. C++STL(二)——vector容器
  9. Gang Of Four的23中设计模式
  10. 对Web语义化的思考。