[关闭]
@yangfch3 2017-01-18T08:23:49.000000Z 字数 5042 阅读 3093

Shell 编程 - 02

shell


接前文:Shell 编程 - 01

01 篇介绍的是基础知识:

  1. 变量
  2. 运算符
  3. 数据类型

本章介绍其他语法与流程结构

echo - 回显

echo 用于在屏幕上打印出指定的字符串。

最好的理解 echo 的方法就是:

  1. mouth=8
  2. echo "${mouth}-1-2009"
  3. echo "OK!\n"
  4. echo "It is a test" > myfile
  5. echo '$name\"' # 注意单引号
  6. echo `date`

"" 内可以使用 $()(推荐) 或 `` 包裹命令

  1. echo "There are $(ls | wc -l) items here."

printf - 格式化输出

printf 命令用于格式化输出,是 echo 命令的增强版。它是 C 语言 printf() 库函数的一个有限的变形

  1. # format-string为双引号
  2. $ printf "%d %s\n" 1 "abc"
  3. 1 abc
  4. # 单引号与双引号效果一样
  5. $ printf '%d %s\n' 1 "abc"
  6. 1 abc
  7. # 没有引号也可以输出
  8. $ printf %s abcdef
  9. abcdef
  10. # 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
  11. $ printf %s abc def
  12. abcdef
  13. $ printf "%s\n" abc def
  14. abc
  15. def
  16. $ printf "%s %s %s\n" a b c d e f g h i j
  17. a b c
  18. d e f
  19. g h i
  20. j
  21. # 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
  22. $ printf "%s and %d \n"
  23. and 0
  24. # 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0
  25. $ printf "The first program always prints'%s,%d\n'" Hello Shell
  26. -bash: printf: Shell: invalid number
  27. The first program always prints 'Hello,0'

if - 条件分支

三种 if

  1. if ... fi 语句;
  2. if ... else ... fi 语句;
  3. if ... elif ... else ... fi 语句。

if ... else 语句的语法:

  1. if [ expression ]
  2. then
  3. Statement(s) to be executed if expression is true
  4. fi

if ... else ... fi 语句的语法:

  1. if [ expression ]
  2. then
  3. Statement(s) to be executed if expression is true
  4. else
  5. Statement(s) to be executed if expression is not true
  6. fi

if ... elif ... fi 语句可以对多个条件进行判断,语法为:

  1. if [ expression 1 ]
  2. then
  3. Statement(s) to be executed if expression 1 is true
  4. elif [ expression 2 ]
  5. then
  6. Statement(s) to be executed if expression 2 is true
  7. elif [ expression 3 ]
  8. then
  9. Statement(s) to be executed if expression 3 is true
  10. else
  11. Statement(s) to be executed if no expression is true
  12. fi

单行书写,使用 ; 分隔

  1. if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;

凡是使用分行的情况都可以使用 ; 连为一行

if ... else 语句也经常与 test 命令结合使用,如下所示:

  1. num1=$[2*3]
  2. num2=$[1+5]
  3. if test $[num1] -eq $[num2]
  4. then
  5. echo 'The two numbers are equal!'
  6. else
  7. echo 'The two numbers are not equal!'
  8. fi

case - 分支简写

case...esac 用于简写分支(不需要那么多 if)

  1. case in
  2. 模式1)
  3. command1
  4. command2
  5. command3
  6. ;;
  7. 模式2
  8. command1
  9. command2
  10. command3
  11. ;;
  12. *)
  13. command1
  14. command2
  15. command3
  16. ;;
  17. esac
  1. option="${1}"
  2. case ${option} in
  3. -f) FILE="${2}"
  4. echo "File name is $FILE"
  5. ;;
  6. -d) DIR="${2}"
  7. echo "Dir name is $DIR"
  8. ;;
  9. *)
  10. echo "`basename ${0}`:usage: [-f file] | [-d directory]"
  11. exit 1 # Command to come out of the program with status 1
  12. ;;
  13. esac

运行输出:

  1. $./test.sh
  2. test.sh: usage: [ -f filename ] | [ -d directory ]
  3. $ ./test.sh -f index.htm
  4. $ vi test.sh
  5. $ ./test.sh -f index.htm
  6. File name is index.htm
  7. $ ./test.sh -d unix
  8. Dir name is unix

for - 循环

  1. 遍历参数序列

    1. for 变量 in 参数序列
    2. do
    3. command1
    4. command2
    5. ...
    6. commandN
    7. done

    其中参数序列的写法:

    1. {m..n}:表示从 m 到 n 的数值区间
    2. x y z:表示 x y z 三个值做为遍历时的参数
  2. 区间循环

    1. for ((a=1; a <= 3; a++))
    2. do
    3. command1
    4. command2
    5. done

例如:

  1. for loop in 1 2 3 4 5
  2. do
  3. echo "The value is: $loop"
  4. done
  5. for str in 'This is a string'
  6. do
  7. echo $str
  8. done
  9. for FILE in $HOME/.bash*
  10. do
  11. echo $FILE
  12. done

while - 条件循环

语法格式:

  1. while [command] # 这里的 command 与 if 哪里的条件语句一样
  2. do
  3. Statement(s) to be executed if command is true
  4. done

例如:条件递增

  1. COUNTER=0
  2. while [ $COUNTER -lt 5 ]
  3. do
  4. COUNTER='expr $COUNTER+1'
  5. echo $COUNTER
  6. done

例如:读取键盘信息。下面的例子中,输入信息被设置为变量FILM,按 Ctrl-D 结束循环

  1. echo 'type <CTRL-D> to terminate'
  2. echo -n 'enter your most liked film: '
  3. while read FILM
  4. do
  5. echo "Yeah! great film the $FILM"
  6. done

until - 寻真循环

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。

  1. until command
  2. do
  3. Statement(s) to be executed until command is true
  4. done

break continue - 调出循环

用在循环内,break, continue 用于跳出循环。break 是完全的跳出并结束循环,continue 是跳出当前,后面还没完成的循环还会继续。

不像 JS 中的 lable,break 后面可以接数值,用于表示调出循环的层级。如果没有接数值则表示跳出当前层循环。continue 后同样可以接数值。

  1. for var1 in 1 2 3
  2. do
  3. for var2 in 0 5
  4. do
  5. if [ $var1 -eq 2 -a $var2 -eq 0 ]
  6. then
  7. break 2
  8. else
  9. echo "$var1 $var2"
  10. fi
  11. done
  12. done
  1. while :
  2. do
  3. echo -n "Input a number between 1 to 5: "
  4. read aNum
  5. case $aNum in
  6. 1|2|3|4|5) echo "Your number is $aNum!"
  7. ;;
  8. *) echo "You do not select a number between 1 to 5!"
  9. continue
  10. echo "Game is over!"
  11. ;;
  12. esac
  13. done

function - 函数

其实 bash 许多命令的本质就是一个函数,利用函数我们能拆解一个复杂功能为小模块,达到原子代码的复用。

  1. # 形式1:直接函数名,无关键字
  2. function_name () {
  3. list of commands
  4. [ return value ]
  5. }
  6. # 形式2:function 关键字
  7. function function_name () {
  8. list of commands
  9. [ return value ]
  10. }

Shell 函数必须先定义后使用。

函数的返回值:可以显式增加 return 语句;如果不加,会将最后一条命令运行结果作为返回值

Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。
如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”

函数调用

只需要给出函数名,不需要加括号。在调用函数时可以在函数名后面添加任意数量的参数,以空格间隔

  1. function_name
  2. function_name var1 var2 var3

函数的嵌套

  1. number_one () {
  2. echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"
  3. number_two
  4. }
  5. number_two () {
  6. echo "Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"
  7. }
  8. number_one

删除函数

  1. unset .f function_name

函数的参数

  1. 不像其他语言,Shell 函数定义时,括号内不可以有任何参数
  2. 函数调用不会用到括号

函数调用时的传参:

  1. funWithParam 1 2 3 4 5 6 7 8 9 34 73

函数内部使用参数:$(n) 与特殊变量

  1. funWithParam(){
  2. echo "The value of the first parameter is ${1} !"
  3. echo "The value of the second parameter is ${2} !"
  4. echo "The value of the tenth parameter is $10 !" # 观察 $10 与 $(10) 的输出区别
  5. echo "The value of the tenth parameter is ${10} !"
  6. echo "The value of the eleventh parameter is ${11} !"
  7. echo "The amount of the parameters is $# !" # 参数个数
  8. echo "The string of the parameters is $* !" # 传递给函数的所有参数
  9. }

特殊变量:
image_1b5ss5lem11hb1phgsvm17bm34r9.png-34.5kB

输入输出重定向

输出重定向

  1. >:暴力重定向
  2. >>:追加输出到文件尾

命令的输出不仅可以是显示器(标准输出回显),还可以很容易的转移向到文件,这被称为输出重定向。

  1. which zsh > location.txt
  2. which bash >> location.txt

输入重定向

<:和输出重定向一样,Unix 命令也可以从文件获取输入。

  1. echo < location.txt

高级重定向

(n, m 可以为 0, 1, 2,分别表示标准输入,标准输出,标准错误输出)
image_1b5ssing11uratba1up4a1jv81m.png-82.3kB
(这里的标准输入文件,标准输出文件,标准错误文件即上面的 0, 1, 2)

重定向高级一点的写法:

  1. # 希望将 stdout 和 stderr 合并后重定向到 file
  2. command > file 2>&1
  3. # 对 stdin 和 stdout 都重定向
  4. command < file1 >file2

嵌入文档

  1. <<
  2. delimiter:自定义的标志,常见 EOF
  1. command << delimiter
  2. document
  3. delimiter

它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

例如:

  1. $wc -l << EOF
  2. This is a simple lookup program
  3. for good (and bad) restaurants
  4. in Cape Town.
  5. EOF
  6. 3

文件包含

Shell 也可以包含外部脚本,将外部脚本的内容在当前脚本执行

  1. #### subscript.sh
  2. url="http://see.xidian.edu.cn/cpp/view/2738.html"
  3. #### main.sh
  4. . ./subscript.sh # subscript.sh 表示的是另外一个 sh
  5. echo $url
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注