常用的linux技巧,Linux Shell常用技巧(十二)-第二部分
下面的示例使用了let命令操作符,如:
/> cat
> test3.shif (( $# != 2
))#等同于
[ $# -ne 2
]
thenecho "Usage: $0 arg1 arg2"
1>&2exit 1
#exit退出值为0-255之间,只有0表示成功。
fiif (( $1 < 0 || $1
> 30 ))#等同于
[ $1 -lt 0 -o $1 -gt 30
]
thenecho "arg1 is out of range."exit 2fiif (( $2 <= 20
))#等同于
[ $2 -le 20
]
thenecho "arg2 is out of range."fiCTRL+D/> sh
./test3.shUsage:
./test3.sh arg1 arg2
/> echo
$?#Shell脚本的退出值为exit的参数值。1
/> sh ./test3.sh 40
30arg1 is out
of range.
/> echo
$?2下面的示例为如何在if的条件表达式中检验空变量:
/> cat
> test4.shif [ "$name" = ""
]#双引号就表示空字符串。
thenecho "name is null."fiCTRL+D/> .
./test4.shname is
null.if/elif/else语句的使用方式和if语句极为相似,相信有编程经验的人都不会陌生,这里就不在赘述了,其格式如下:if command
then
command
elif command
then
command
else
command
fi见如下示例脚本:
/> cat
> test5.shecho -e "How old are you?
\c"read ageif [ $age -lt 0 -o $age -gt 120
]#等同于
(( age < 0 || age
> 120 ))
thenecho "You are so old."elif [ $age -ge 0 -a $age -le 12
]#等同于
(( age >= 0
&& age <= 12
))
thenecho "You are child."elif [ $age -ge 13 -a $age -le 19
]#等同于
(( age >= 13
&& age <= 19
))
thenecho "You are 13--19 years old."elif [ $age -ge 20 -a $age -le 29
] #等同于
(( age >= 20
&& age <= 29
))
thenecho "You are 20--29 years old."elif [ $age -ge 30 -a $age -le 39
] #等同于
(( age >= 30
&& age <= 39
))
thenecho "You are 30--39 years old."elseecho "You are above 40."fiCTRL+D/> .
./test5.shHow old are
you? 50You are
above 40.case语句格式如下:case variable in
value1)
command
;;
#相同于C语言中case语句内的break。
value2)command
;;
*)
#相同于C语言中switch语句内的default
command
;;
esac见如下示例脚本:/> cat >
test6.sh#!/bin/shecho -n "Choose a color: "read colorcase "$color" in[Bb]l??)echo "you select blue color.";;[Gg]ree*)echo "you select green color.";;red|orange)echo "you select red or orange.";;*)echo "you select other color.";;esacecho "Out of case
command."/> .
./test6.shChoose a
color: greenyou select
green color.
Out of case
command.4. 循环语句:Bash
Shell中主要提供了三种循环方式:for、while和until。for循环声明格式:for variable in word_list
do
command
done见如下示例脚本:
/> cat
> test7.shfor score in math english physics
chemist #for将循环读取in后面的单词列表,类似于Java的for-each。doecho "score = $score"doneecho "out of for loop"CTRL+D
/> .
./test7.shscore =
math
score =
english
score =
physics
score =
chemist
out of for
loop
/> cat
> mylist#构造数据文件
tompattyannjakeCTRL+D
/> cat
> test8.sh#!/bin/shfor person in $(cat
mylist)#for将循环读取cat mylist命令的执行结果。
doecho "person = $person"doneecho "out of for loop."CTRL+D/> .
./test8.shperson =
tom
person =
patty
person =
ann
person =
jake
out of for
loop.
/> cat
> test9.shfor file in
test[1-8].sh#for将读取test1-test8,后缀为.sh的文件
doif [ -f $file ]#判断文件在当前目录是否存在。
thenecho "$file
exists."fidoneCTRL+D/> .
./test9.shtest2.sh
exists.
test3.sh
exists.
test4.sh
exists.
test5.sh
exists.
test6.sh
exists.
test7.sh
exists.
test8.sh
exists.
/> cat
> test10.shfor name in
$*#读取脚本的命令行参数数组,还可以写成for name的简化形式。
doecho "Hi, $name"doneCTRL+D/> . ./test10.sh stephen
annHi,
stephen
Hi,
annwhile循环声明格式:
while command#如果command命令的执行结果为0,或条件判断为真时,执行循环体内的命令。do
command
done见如下示例脚本:
/> cat
>
test1.shnum=0while (( num < 10
))#等同于
[ $num -lt 10
]
doecho -n "$num "let num+=1doneecho -e "\nHere's out of
loop."CTRL+D/> .
./test1.sh0 1 2 3 4 5
6 7 8 9
Here's out
of loop.
/> cat
> test2.shgo=startecho Type q to quit.while [[ -n $go
]]#等同于[ -n "$go" ],如使用该风格,$go需要被双引号括起。
doecho -n How are you.read wordif [[ $word == [Qq] ]]#等同于[ "$word" = Q -o "$word" = q
]
thenecho Bye.go=
#将go变量的值置空。fidoneCTRL+D/> .
./test2.shHow are
you. HiHow are
you. qBye.until循环声明格式:
until command #其判断条件和while正好相反,即command返回非0,或条件为假时执行循环体内的命令。
docommand
done见如下示例脚本:
/> cat
> test3.shuntil who | grep
stephen#循环体内的命令将被执行,直到stephen登录,即grep命令的返回值为0时才退出循环。
dosleep 1echo "Stephen still doesn't login."doneCTRL+Dshift命令声明格式:shift [n]shift命令用来把脚本的位置参数列表向左移动指定的位数(n),如果shift没有参数,则将参数列表向左移动一位。一旦移位发生,被移出列表的参数就被永远删除了。通常在while循环中,shift用来读取列表中的参数变量。见如下示例脚本:
/> set stephen ann
sheryl mark #设置4个参数变量。
/>
shift#向左移动参数列表一次,将stephen移出参数列表。
/> echo
$*ann sheryl
mark
/> shift
2#继续向左移动两位,将sheryl和ann移出参数列表
/> echo
$*mark
/> shift
2#继续向左移动两位,由于参数列表中只有mark了,因此本次移动失败。
/> echo
$*mark
/> cat
> test4.shwhile (( $# > 0
)) #等同于
[ $# -gt 0
]
doecho $*shiftdoneCTRL+D/> . ./test4.sh a b c d
ea b c d
e
b c d
e
c d e
d e
ebreak命令声明格式:break [n]和C语言不同的是,Shell中break命令携带一个参数,即可以指定退出循环的层数。如果没有指定,其行为和C语言一样,即退出最内层循环。如果指定循环的层数,则退出指定层数的循环体。如果有3层嵌套循环,其中最外层的为1,中间的为2,最里面的是3。见如下示例脚本:
/> cat
> test5.shwhile truedoecho -n "Are you ready to move on?"read answerif [[ $answer == [Yy] ]]thenbreakelseecho "Come on."fidoneecho "Here we are."CTRL+D/> .
./test5.shAre you
ready to move on? yHere we
are
常用的linux技巧,Linux Shell常用技巧(十二)-第二部分相关推荐
- 【Linux开发】linux设备驱动归纳总结(十二):简单的数码相框
linux设备驱动归纳总结(十二):简单的数码相框 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- Linux内核Thermal框架详解十二、Thermal Governor(2)
本文部分内容参考 万字长文 | Thermal框架源码剖析, Linux Thermal机制源码分析之框架概述_不捡风筝的玖伍贰柒的博客-CSDN博客, "热散由心静,凉生为室空" ...
- 【Linux上分之路】第十二篇:Linux三剑客grep、sed、awk
文章目录 Linux三剑客 三剑客特点及应用场景 grep grep命令的基本格式 grep范例 grep命令选项 -A.-B -c.-v -n.-w sed sed命令基本格式 sed命令功能 se ...
- linux线程并不真正并行,Linux系统编程学习札记(十二)线程1
Linux系统编程学习笔记(十二)线程1 线程1: 线程和进程类似,但是线程之间能够共享更多的信息.一个进程中的所有线程可以共享进程文件描述符和内存. 有了多线程控制,我们可以把我们的程序设计成为在一 ...
- linux设备驱动归纳总结(十二):简单的数码相框
http://blog.chinaunix.net/uid-25014876-id-116926.html 其实代码很简单,实现lcd驱动,使lcd能够显示图片,当按下按键后切换图片. 先要说明一下几 ...
- linux系统及shell常用命令
主机系统: linux 乱码后解码: echo -e '\xf' 查看占用端口: netstat -anp |grep 9200 查看主机硬件时间: hwclock -r 查看cpu核数: nproc ...
- Linux之部分shell脚本练习(二)
while CONDITION;do statement done 进入循环:条件满足 退出循环:条件不满足 until CONDITION;do statment ... done ...
- tableau linux无网络安装_四十二、Linux网络管理,软件安装,进程管理总结
「@Author: Runsen」 1.⽹络管理 1.1 网络状态查看 在Linux中经常使用ifconfig,route和netstat查看网络状态,它们就是. net-tools工具,下面我来使用 ...
- linux内核网络协议栈--packet_type(十二)
进入函数netif_receive_skb()后,skb正式开始协议栈之旅. 先上图,协议栈大致过程如下所示: 跟OSI七层模型不同,linux根据包结构对网络进行分层. 比如,arp头和ip头都是紧 ...
- linux进程--进程与线程(十二)
1.介绍 进程(Process) 是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 在当代面向线程设计的计算机结构中,进程是线程的容器.程序是 ...
最新文章
- 【论文速读】基于投影方法的激光雷达点云处理比较
- [python] 从GPS坐标获取国家名
- 探索JAVA并发 - 如何减少锁的竞争
- 吴恩达机器学习笔记 —— 3 线性回归回顾
- C# Message 消息处理
- Python的代码结构
- 台式计算机l小时耗电,电脑一天的耗电量是多少?不算不知道 一算吓一跳!
- 单例模式的几种实现方式及优缺点
- SpringBoot @Cacheable缓存注解的使用
- C语言学习之猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。
- 10年嵌入式工程师经验之谈:对于研发工作的感悟
- 核心频率个加速频率_流处理器、核心频率、 位宽……这些显卡参数你知道吗?—— 电脑硬件科普篇(八)...
- [蓝桥杯2019初赛]年号字串-数论+模拟
- php session 效率,大量php session临时文件带来的服务器效率问题
- 【BZOJ1857】【SCOI2010】传送带 [三分]
- 什么是社会性网络?什么是六度分隔理论?
- 安装oracle失败,停止在76%
- 开源的代理服务器HAProxy 易遭严重的 HTTP 请求走私攻击
- 无刷直流电机构成及工作原理详解
- [c++] 使用 raylib + ODE(open dynamics engine) 制作一个简易牛顿摆