(三)Linux Shell编程——Shell常用命令(输出、判断、循环、函数、包含)
3. 常用命令
3.1 输出
3.1.1 echo命令
echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式:
echo arg
name="coding"
echo '$name\"'+" ${name}" #原样输出 $name\"+ coding
echo `date` #当前日期
3.1.2 printf命令
printf 命令用于格式化输出, 是echo命令的增强版。它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同。
printf format-string [arguments...] #format-string 为格式控制字符串,arguments 为参数列表
printf "Hello, Shell\n" #printf 不像 echo 那样会自动换行,必须显式添加换行符(\n)
printf "%d %s\n" "abc" #输出 abc
3.2 if else语句
if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句:
- if ... fi 语句;
- if ... else ... fi 语句;
- if ... elif ... else ... fi 语句。
3.2.1 if ... fi语句
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。
3.2.2 if ... else ... fi 语句
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
a=
b=
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
if ... else 语句也可以写成一行,以命令的方式来运行,像这样:
if test $[*] -eq $[+]; then echo 'The two numbers are equal!'; fi;
if ... else 语句也经常与 test 命令结合使用,test 命令用于检查某个条件是否成立,与方括号([ ])类似。
a=
b=
if [ ${a} == ${b} ]
#if test $[a] -eq $[b] #数值类型比较 $[num]
3.2.3 if ... elif ... fi 语句
if ... elif ... fi 语句可以对多个条件进行判断,语法为:
if [ expression ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if no expression is true
fi
哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。
a=
b=
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
3.3 test命令
test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
3.3.1 数值比较
语法:
if test $[num1] -eq $[num2]
3.3.2 字符串比较
语法:
if test str1=str2
3.3.3 文件比较
语法:
if test -e ./bash
另外,Shell还提供了与( ! )、或( -o )、非( -a )三个逻辑操作符用于将测试条件连接起来,其优先级为:“!”最高,“-a”次之,“-o”最低。
3.4 case ... esac语句
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。
语法:
case 值 in
模式1)
command1
command2
command3
;;
模式2)
command1
command2
command3
;;
*)
command1
command2
command3
;;
esac
case工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。
echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
) echo 'You select 1'
;;
) echo 'You select 2'
;;
) echo 'You select 3'
;;
) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac
3.5 循环
3.5.1 for循环
语法:
for 变量 in 列表
do
command1
command2
...
commandN
done
列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量。
in 列表是可选的,如果不用它,for 循环使用命令行的位置参数。
for loop in
#for str in 'I love Spring'
do
echo "The value is: $loop"
#echo ${str}
done
3.5.2 while循环
while循环用于不断执行一系列命令,也用于从输入文件中读取数据;命令通常为测试条件。
语法:
while command
do
Statement(s) to be executed if command is true
done
COUNTER=
while [ $COUNTER -lt ]
do
COUNTER='expr $COUNTER+1'
echo $COUNTER
done
3.5.3 util循环
until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用。
语法:
until command
do
Statement(s) to be executed until command is true
done
a=
until [ ! $a -lt ]
do
echo $a #输出1~
a=`expr $a + `
done
3.5.4 break和continue命令
break命令允许跳出所有循环(终止执行后面的所有循环);continue命令会跳出当前循环。
在嵌套循环中,这两个命令还有较高级的用法:
break #跳出2层循环
continue
3.6 Shell函数
3.6.1 函数定义
函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Shell 也支持函数。Shell 函数必须先定义后使用。
函数的定义语法如下:
[ function ] function_name () {
list of commands
[ return value ]
}
函数名前可加上关键字 function,也可不加,效果一样。
函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"
结果:
[root@centoszang testShell]# ./myShell.sh
The function is to get the sum of two numbers...
Input first number:
Input another number:
The two numbers are and !
The sum of two numbers is !
像删除变量一样,删除函数也可以使用 unset 命令,不过要加上 .f 选项
unset .f funWithReturn
3.6.2 函数参数
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !" # 参数个数
echo "The string of the parameters is $* !" # 传递给函数的所有参数
}
funWithParam 18
输出
[root@centoszang testShell]# ./myShell.sh
The value of the first parameter is !
The value of the second parameter is !
The value of the tenth parameter is !
The value of the tenth parameter is !
The value of the eleventh parameter is !
The amount of the parameters is !
The string of the parameters is !
3.7 Shell文件包含
像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本。
两种语法:
. filename
source filename
创建被调用脚本 test.sh
name="Java Web"
使用主文件 myShell.sh来引用该脚本
. ./test.sh
echo ${name} #输出Java Web
需要注意的是,被包含脚本(test.sh)不需要有执行权限。
(三)Linux Shell编程——Shell常用命令(输出、判断、循环、函数、包含)的更多相关文章
- Linux Shell 编程 教程 常用命令
概述: Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户 ...
- shell 脚本之获取命令输出字符串以及函数参数传递
在ubuntu 14.04之后,所有的U盘挂载也分用户之分,最近很多操作也和U盘有关,所以就研究了一上午shell脚本函数以及字符串操作的方法. 字符串操作: 获取他的命令输出比较简单,打个简单的比方 ...
- Shell编程——vim常用命令
[vim]工作模式切换: 在普通模式下输入 i(插入).c(修改).o(另起一行) 命令时进入编辑模式:按 esc 键退回到普通模式. 在普通模式下输入冒号(:)可以进入命令模式.输入完命 ...
- (转载)shell日志分析常用命令
shell日志分析常用命令总结 时间:2016-03-09 15:55:29来源:网络 导读:shell日志分析的常用命令,用于日志分析的shell脚本,统计日志中百度蜘蛛的抓取量.抓取最多的页面.抓 ...
- linux 查看服务器性能常用命令
一.top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来 ...
- Linux基础 - 系统优化及常用命令
目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...
- Linux基础系统优化及常用命令
# Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...
- Linux文件管理和编辑常用命令
Linux文件管理和编辑常用命令 mkdir 命令 功能说明 mkdir 命令用于创建一个目录,mkdir是make directory的缩写 格式: mkdir [选项] 目录名 mkdir 命令的 ...
- Linux系统管理和维护常用命令
Linux系统管理和维护常用命令 ls 命令 功能说明 ls 命令显示指定工作目录下的内容,列出工作目录所包含的文件及子目录. 语法结构: ls [选项] [路径或文件] ls 选项及说明 -a 显示 ...
- [转帖]「日常小记」linux中强大且常用命令:find、grep
「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...
随机推荐
- LR杂记-nmon+analyser监控linux系统资源
1.查看linux具体版本号信息 file /sbin/init 2.下载相应nmon版本号 http://pkgs.repoforge.org/nmon/ 3.安装 rpm -ivh nmon-14 ...
- 算法:四种冒泡排序(Bubble Sort)实现
背景 大学关于排序的算法,好像就学会了冒泡排序,这个算是排序界的 hello,world 了,冒泡排序的定义如下: 重复的遍历数组. /// <summary> /// 重复的遍历数组. ...
- selenium3+python自动化50-环境搭建(firefox)
前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...
- rman多通道全备份脚本
run{ allocate channel d1 type disk; allocate channel d2 type disk; allocate channel d3 type disk; ...
- spring事务管理器的源码和理解
原文出处: xieyu_zy 以前说了大多的原理,今天来说下spring的事务管理器的实现过程,顺带源码干货带上. 其实这个文章唯一的就是带着看看代码,但是前提你要懂得动态代理以及字节码增强方面的知识 ...
- sudo:抱歉,您必须拥有一个终端来执行 sudo 解决办法;ssh执行sudo命令的方法;给用户增加sudo免密权限
1.supervisor使用sudo执行命令的时候,报错 2.解决办法: 编辑 /etc/sudoers 文件,将Default requiretty注释掉. sudo vim /etc/sudoer ...
- 混沌数学之二维logistic模型
上一节讲了logistic混沌模型,这一节对其扩充一下讲二维 Logistic映射.它起着从一维到高维的衔接作用,对二维映射中混沌现象的研究有助于认识和预测更复杂的高维动力系统的性态.通过构造一次藕合 ...
- [leetcode]Sort List @ Python
原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...
- org.dom4j.DocumentException: unknown protocol: d Nested exception: unknown
最近用dom4j写了一个修改XML文件的小例子,抛出了如下异常: org.dom4j.DocumentException: unknown protocol: d Nested exception: ...
- scala 学习笔记六 推导
1.介绍 在Scala中,推导将生成器.过滤器.和定义组合在一起. 2.例子 有一种将result用作val(而不是var)的方式,:“就地”构建result,而不是逐项构建,利用yield关键字,当 ...