11 break跳出循环 continue结束本次循环 exit退出整个脚本
1.shell脚本介绍
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。
Linux提供了像MicrosoftWindows那样的可视的命令输入界面--X Window的图形用户界面(GUI)。它提供了很多桌面环境系统,其操作就像Windows一样,有窗口、图标和菜单,所有的管理都是通过鼠标控制。GNOME。
每个Linux系统的用户可以拥有他自己的用户界面或Shell,用以满足他们自己专门的Shell需要。
同Linux本身一样,Shell也有多种不同的版本。主要有下列版本的Shell:
- Bourne Shell:是贝尔实验室开发的。
- BASH:是GNU的Bourne Again Shell,是GNU操作系统上默认的shell。
- Korn Shell:是对Bourne SHell的发展,在大部分内容上与Bourne Shell兼容。
- C Shell:是SUN公司Shell的BSD版本。
- Z Shell:The last shell you’ll ever need! Z是最后一个字母,也就是终极Shell。它集成了bash、ksh的重要特性,同时又增加了自己独有的特性。
2.shell脚本结构和执行
- 开头需要加#!/bin/bash
- 以#开头的行作为解释说明
- 脚本的名字以.sh结尾,用于区分这是一个shell脚本
- 执行方法有两种
- chmod +x 1.sh; ./1.sh
- bash 1.sh
- 查看脚本执行过程 bash -x 1.sh
- 查看脚本是否语法错误 bash -n 1.sh
3.date命令用法
#date 获取当前时间
#date -d "-1week" +%Y%m%d 获取上周日期(day,month,year,hour)
#date--date="-24 hour" +%Y%m%d 同上
date_now=`date+%s` shell脚本里面赋给变量值
%% 输出%符号
%a 当前域的星期缩写 (Sun..Sat)
%A 当前域的星期全写 (Sunday..Saturday)
%b 当前域的月份缩写(Jan..Dec)
%B 当前域的月份全称 (January..December)
%c 当前域的默认时间格式 (Sat Nov 04 12:02:33 EST 1989)
%C n百年 [00-99]
%d 两位的天 (01..31)
%D 短时间格式 (mm/dd/yy)
%e 短格式天 ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%h same as %b
%H 24小时制的小时 (00..23)
%I 12小时制的小时 (01..12)
%j 一年中的第几天 (001..366)
%k 短格式24小时制的小时 ( 0..23)
%l 短格式12小时制的小时 ( 1..12)
%m 双位月份 (01..12)
%M 双位分钟 (00..59)
%n 换行
%N 十亿分之一秒(000000000..999999999)
%p 大写的当前域的上下午指示 (blank in many locales)
%P 小写的当前域的上下午指示 (blank in many locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数(a GNU extension)
%S 双位秒 second (00..60);
%t 横向制表位(tab)
%T 24小时制时间表示(hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)
%U 一年中的第几周 星期天为开始 (00..53)
%V 一年中的第几周 星期一为开始 (01..53)
%w 一周中的第几天 星期天为开始 (0..6)
%W 一年中的第几周 星期一为开始 (00..53)
%x 本地日期格式 (mm/dd/yy)
%X 本地时间格式 (%H:%M:%S)
%y 两位的年(00..99)
%Y 年 (1970…)
实例:
//年月日
[root@knightlai01 ~]# date +%Y-%m-%d
2018-10-21
//时间戳
[root@knightlai01 ~]# date +%s
1540101608
[root@knightlai01 ~]# date -d @1540101308
Sun Oct 21 01:55:08 EDT 2018
//一天后
[root@knightlai01 ~]# date -d "+1day"
Mon Oct 22 02:01:02 EDT 2018
//一月前
[root@knightlai01 ~]# date -d "-1 month"
Fri Sep 21 02:01:45 EDT 2018
//一星期中的第几日(0-6),0 代表周一
[root@knightlai01 ~]# date +%w
0
//一年中的第几周,以周一为每星期第一天(00-53)
[root@knightlai01 ~]# date +%W
42
//显示日历
[root@knightlai01 ~]# cal
October 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
4.shell脚本中的变量
[root@knightlai01 ~]# a=1
[root@knightlai01 ~]# echo $a
1
- 注意: 1,变量名和等号之间不能有空格;
- 2,首个字符必须为字母(a-z,A-Z)。
- 3, 中间不能有空格,可以使用下划线(_)。
- 4, 不能使用标点符号。
- 5, 不能使用bash里的关键字(可用help命令查看保留关键字)。
变量类型
运行shell时,会同时存在三种变量:
1) 局部变量
局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。
2) 环境变量
所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。
3) shell变量
shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行
特殊变量
$* 和 $@ 的区别为: $* 和 $@ 都表示传递给函数或脚本的所有参数,不被双引号(" ")包含时,都以"$1" "$2" … "$n" 的形式输出所有参数。但是当它们被双引号(" ")包含时,"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数;"$@" 会将各个参数分开,以"$1" "$2" … "$n" 的形式输出所有参数。
$? 可以获取上一个命令的退出状态。所谓退出状态,就是上一个命令执行后的返回结果。退出状态是一个数字,一般情况下,大部分命令执行成功会返回 0,失败返回 1。
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(!=) 注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
实例:
[root@knightlai01 shell]# cat if.sh
#! /bin/bash
a=3
if
[ $a -lt 5 ]
then
echo "ok"
fi
[root@knightlai01 shell]# sh if.sh
ok
-lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)
6.文件目录属性判断
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
实例:
//判断是否是文件,其它参数使用也差不多
[root@knightlai01 shell]# cat if2.sh
#!/bin/bash
if
[ -f ./shell ]
then
echo "file is exits."
else
echo "file is not exits"
fi
[root@knightlai01 shell]# sh if2.sh
file is not exits
7.if特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
实例:
//判断1.txt中是否有12有就打印出ok
[root@knightlai01 shell]# cat 1.txt
32423414123123
43543654654
[root@knightlai01 shell]# cat if3.sh
#!/bin/bash
f=1.txt
if
[ -z "$f" ]
then
echo "file is not exits"
else if
grep -q "12" 1.txt
then
echo "ok"
fi
fi
[root@knightlai01 shell]# sh if3.sh
ok
8.case判断
shell脚本中case选择语句可以结合read指令实现比较好的交互应答操作,case接收到read指令传入的一个或多个参数,然后case根据参数做选择操作。
case 变量 in
value1)
command
;; //每一个选择都以双 ;; 结束
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
上面的结构中,不限制value的个数, * 则代表除了上面的value外的其他值。
case实例:
[root@knightlai01 shell]# cat case.sh
#!/bin/bash
read -p "Input your sex: " m
case $m in
"m")
echo "You are male!" ;;
"f")
echo "You are female" ;;
*)
echo "please input "m" or "f" " ;;
esac
[root@knightlai01 shell]# sh case.sh
Input your sex: r
please input m or f
[root@knightlai01 shell]# sh case.sh
Input your sex: m
You are male!
[root@knightlai01 shell]# sh case.sh
Input your sex: f
You are female
9. for循环
for命令格式:
– list参数:迭代中要用的一系列值
– 每个迭代中,变量var会包含列表中的当前值
– do和done语句之间输入的命令可以是一条或多条标准的bash shell命令
例:
for var in list
do
commands
done
for实例:
[root@knightlai01 shell]# cat for.sh
#!/bin/bash
sum=0
for i in `seq 1 10`
do
echo $i
done
[root@knightlai01 shell]# sh for.sh
1
2
3
4
5
6
7
8
9
10
10.while循环
格式如下:
while命令中定义的test命令和if-then中定义是一样的格式
while命令的关键:指定的test命令的退出状态码必须随着循环中运行的命令改变,否则while会不停循环下去
最常用的test命令用法是,用方括号来查看命令中用到的shell变量的值
1、while循环: 以行读取文件,默认分隔符是空格或者Tab;
while 实例:
[root@knightlai01 shell]# cat while.sh
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
[root@knightlai01 shell]# sh while.sh
Please input a number: 189
189
11 break跳出循环 continue结束本次循环 exit退出整个脚本
break
结束并退出循环
continue
在循环中不执行continue下面的代码,转而进入下一轮循环
exit
退出脚本,
常带一个整数给系统,如 exit 0
break continue exit实例
//判断输入的是不是数字,如果不是就一直输入直到输入数字才会结束循化
这就是continue与break
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
//beak直接退出循环
[root@knightlai01 shell]# cat xun.sh
#!/bin/bash
for(( i=1; i<5; i++ ))
do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
[root@knightlai01 shell]# sh xun.sh
1
1
2
2
3
//continue 退出本次循环,继续循环
[root@knightlai01 shell]# sh xun.sh
1
1
2
2
3
4
4
[root@knightlai01 shell]# cat xun.sh
#!/bin/bash
for(( i=1; i<5; i++ ))
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
//exit直接退出本次脚本
[root@knightlai01 shell]# cat xun.sh
#!/bin/bash
for(( i=1; i<5; i++ ))
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
[root@knightlai01 shell]# sh xun.sh
1
1
2
2
3
break是立马跳出循环;continue是跳出当前条件循环,继续下一轮条件循环;exit是直接退出整个脚本