Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
2018-03-16 13:56
561 查看
一、Shell echo命令
echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式:echo arg您可以使用echo实现更复杂的输出格式控制。
显示转义字符
echo "\"It is a test\""结果将是:
"It is a test"
双引号也可以省略。
显示变量
name="OK" echo "$name It is a test"结果将是:
OK It is a test
同样双引号也可以省略。
如果变量与其它字符相连的话,需要使用大括号({ }):
mouth=8 echo "${mouth}-1-2009"结果将是:
8-1-2009
显示换行
echo "OK!\n" echo "It is a test"输出:
OK!
It is a test
显示不换行
echo "OK!\c"echo "It is a test"
输出:
OK!It si a test
显示结果重定向至文件
echo "It is a test" > myfile原样输出字符串
若需要原样输出字符串(不进行转义),请使用单引号。例如:echo '$name\"'显示命令执行结果
echo `date`结果将显示当前日期
二、shell printf命令:格式化输出语句
printf 命令用于格式化输出, 是echo命令的增强版。它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同。注意:printf 由 POSIX 标准所定义,移植性要比 echo 好。
如同 echo 命令,printf 命令也可以输出简单的字符串:$printf "Hello, Shell\n"
Hello, Shell
$
printf 不像 echo 那样会自动换行,必须显式添加换行符(\n)。
printf 命令的语法:
printf format-string [arguments...]format-string 为格式控制字符串,arguments 为参数列表。
printf()在C语言入门教程中已经讲到,功能和用法与 printf 命令类似,请查看:C语言格式输出函数printf()详解
这里仅说明与C语言printf()函数的不同:printf 命令不用加括号
format-string 可以没有引号,但最好加上,单引号双引号均可。
参数多于格式控制符(%)时,format-string 可以重用,可以将所有参数都转换。
arguments 使用空格分隔,不用逗号。
请看下面的例子:[cpp] view plain copy# format-string为双引号
$ printf "%d %s\n" 1 "abc"
1 abc
# 单引号与双引号效果一样
$ printf '%d %s\n' 1 "abc"
1 abc
# 没有引号也可以输出
$ printf %s abcdef
abcdef
# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
$ printf %s abc def
abcdef
$ printf "%s\n" abc def
abc
def
$ printf "%s %s %s\n" a b c d e f g h i j
a b c
d e f
g h i
j
# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
$ printf "%s and %d \n"
and 0
# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
$ printf "The first program always prints'%s,%d\n'" Hello Shell
-bash: printf: Shell: invalid number
The first program always prints 'Hello,0'
$
注意,根据POSIX标准,浮点格式%e、%E、%f、%g与%G是“不需要被支持”。这是因为awk支持浮点预算,且有它自己的printf语句。这样Shell程序中需要将浮点数值进行格式化的打印时,可使用小型的awk程序实现。然而,内建于bash、ksh93和zsh中的printf命令都支持浮点格式。
三、Shell if else语句
if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句:if ... fi 语句;if ... else ... fi 语句;
if ... elif ... else ... fi 语句。
1) if ... else 语句
if ... else 语句的语法:if [ expression ] then Statement(s) to be executed if expression is true fi如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。
最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。
注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。
举个例子:[cpp] view plain copy#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
运行结果:
a is not equal to b
2) if ... else ... fi 语句
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如果 expression 返回 true,那么 then 后边的语句将会被执行;否则,执行 else 后边的语句。
举个例子:[cpp] view plain copy#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
执行结果:
a is not equal to b
3) if ... elif ... fi 语句
if ... elif ... fi 语句可以对多个条件进行判断,语法为:if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] then Statement(s) to be executed if expression 2 is true elif [ expression 3 ] then Statement(s) to be executed if expression 3 is true else Statement(s) to be executed if no expression is true fi哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。
举个例子:[cpp] view plain copy#!/bin/sh
a=10
b=20
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
运行结果:
a is less than b
if ... else 语句也可以写成一行,以命令的方式来运行,像这样:[cpp] view plain copyif test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;
if ... else 语句也经常与 test 命令结合使用,如下所示:[cpp] view plain copynum1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出:
The two numbers are equal!test 命令用于检查某个条件是否成立,与方括号([ ])类似。
四、Shell case esac语句
case ... esac 与其他语言中的 switch ... case 语句类似,是一种多分枝选择结构。case 语句匹配一个值或一个模式,如果匹配成功,执行相匹配的命令。case语句格式如下:case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command1 command2 command3 ;; esaccase工作方式如上所示。取值后面必须为关键字 in,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至 ;;。;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令。
下面的脚本提示输入1到4,与每一种模式进行匹配:[cpp] view plain copyecho 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac
输入不同的内容,会有不同的结果,例如:
Input a number between 1 to 4 Your number is:3 You select 3
再举一个例子:[cpp] view plain copy#!/bin/bash
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac
运行结果如下:
$./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] $ ./test.sh -f index.htm $ vi test.sh $ ./test.sh -f index.htm File name is index.htm $ ./test.sh -d unix Dir name is unix $
相关文章推荐
- Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
- Shell 脚本知识回顾 (四) —— Shell 命令及Shell 相关语句
- 通过脚本学shell编程——相关基础知识之shell内置变量命令(更新中)
- shell脚本相关知识
- Shell 脚本知识回顾 (一) —— 基础篇
- shell脚本知识(二)命令字符操作
- Shell 脚本知识回顾 (二) —— Shell变量
- Shell 脚本知识回顾 (二) —— Shell变量
- Shell 脚本知识回顾 (六) —— Shell 函数
- shell的相关知识(变量、脚本定义)
- shell脚本:使用if-then语句和test命令
- shell脚本之文件相关的命令
- Shell 脚本知识回顾 (一) —— 基础篇
- 【Shell脚本学习15】shell printf命令:格式化输出语句
- x-wrt webif2 shell脚本相关知识小结
- SHELL脚本的基础知识2——使用结构化命令
- SHELL脚本的基础知识3——更多结构化命令
- 测试相关shell命令总结2——结构控制语句,命令行参数
- 【Shell脚本学习15】shell printf命令:格式化输出语句
- Shell 脚本知识回顾 (六) —— Shell 函数