分享

高级Bash脚本编程指南(18):内部命令与内建命令(一)

 mingjiu 2016-10-15

高级Bash脚本编程指南(18):内部命令与内建命令(一)

成于坚持,败于止步

内建命令指的就是包含在Bash工具包中的命令, 从字面意思上看就是built in.这主要是考虑到执行效率的问题:内建命令将比外部命令执行的更快, 一部分原因是因为外部命令通常都需要fork出一个单独的进程来执行,另一部分原因是特定的内建命令需要直接访问shell的内核部分.

当一个命令或者是shell本身需要初始化(或者创建)一个新的子进程来执行一个任务的时候, 这种行为被称为fork.这个新产生的进程被叫做子进程, 并且这个进程是从父进程中fork出来的.当子进程执行它的任务时, 父进程也在运行.

注意: 当父进程获得了子进程的进程ID时, 父进程可以给子进程传递参数,然而反过来却不行. 这将会产生不可思议的并且很难追踪的问题.

看一个fork出多个自身实例的脚本

  1. #!/bin/bash  
  2.   
  3. PIDS=$(pidof bash $0)  # 这个脚本不同实例的进程ID.  
  4. P_array=( $PIDS )    # 把它们放到数组里(为什么?).  
  5. echo $PIDS           # 显示父进程和子进程的进程ID.  
  6. let "instances = ${#P_array[*]} - 2"  # 计算元素个数, 至少为1.  
  7.                                       # 为什么减1?  
  8. echo "$instances instance(s) of this script running."  
  9. echo "[Hit Ctl-C to exit.]"; echo  
  10.   
  11.   
  12. sleep 1              # 等一下.  
  13. bash $0                # 再来一次, Sam.  
  14.   
  15. exit 0               # 没必要; 脚本永远不会运行到这里.  
  16.                      # 为什么运行不到这里?  
  17.   
  18. #  在使用Ctl-C退出之后,  
  19. #+ 是否所有产生出来的进程都会被kill掉?  
  20. #  如果是这样的话, 为什么?  
  21.   
  22. # 注意:  
  23. # ----  
  24. # 小心, 不要让这个脚本运行太长时间.  
  25. # 它最后会吃掉你绝大多数的系统资源.  
结果也许会让你吃惊,不过不重要,了解这种fork机制就足够了

  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test1.sh   
  2. 28641 20791 18642  
  3. 1 instance(s) of this script running.  
  4. [Hit Ctl-C to exit.]  
  5.   
  6. 28644 28641 20791 18642  
  7. 2 instance(s) of this script running.  
  8. [Hit Ctl-C to exit.]  
  9.   
  10. 28647 28644 28641 20791 18642  
  11. 3 instance(s) of this script running.  
  12. [Hit Ctl-C to exit.]  
  13.   
  14. 28650 28647 28644 28641 20791 18642  
  15. 4 instance(s) of this script running.  
  16. [Hit Ctl-C to exit.]  
  17.   
  18. 28653 28650 28647 28644 28641 20791 18642  
  19. 5 instance(s) of this script running.  
  20. [Hit Ctl-C to exit.]  
  21.   
  22. ^C  
  23. root@ubuntu:~/resource/shell-study/0614-2013#   
常情况下, 脚本中的Bash内建命令在运行的时候是不会fork出一个子进程的. 但是脚本中的外部或者过滤命令通常会fork出一个子进程.

一个内建命令通常会与一个系统命令同名, 但是Bash在内部重新实现了这些命令.比如, Bash的echo命令与/bin/echo就不尽相同,虽然它们的行为在绝大多数情况下都是一样的.

  1. #!/bin/bash  
  2.   
  3. echo "This line uses the \"echo\" builtin."  
  4. /bin/echo "This line uses the /bin/echo system command."  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test2.sh   
  2. This line uses the "echo" builtin.  
  3. This line uses the /bin/echo system command.  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   

关键字的意思就是保留字, 对于shell来说关键字具有特殊的含义,并且用来构建shell语法结构.比如, "for", "while", "do", 和 "!" 都是关键字.与内建命令相似的是, 关键字也是Bash的骨干部分, 但是与内建命令不同的是,关键字本身并不是一个命令, 而是一个比较大的命令结构的一部分.

echo

echo命令需要-e参数来打印转义字符. 通常情况下, 每个echo命令都会在终端上新起一行, 但是-n参数会阻止新起一行.echo命令可以作为输入, 通过管道传递到一系列命令中去.

  1. if echo "$VAR" | grep -q txt   # if [[ $VAR = *txt* ]]  
  2. then  
  3.   echo "$VAR contains the substring sequence \"txt\""  
  4. fi  
echo命令可以与命令替换组合起来, 这样可以用来设置一个变量.

a=`echo "HELLO" | tr A-Z a-z`

小心echo `command`将会删除任何由command所产生的换行符.$IFS (内部域分隔符) 一搬都会将 \n (换行符) 包含在它的空白字符集合中.Bash因此会根据参数中的换行来分离command的输出, 然后echo.最后echo将以空格代替换行来输出这些参数.

  1. root@ubuntu:~/resource/shell-study/0614-2013# ls -l   
  2. total 8  
  3. -rwxr-xr-x 1 root root 860 2013-06-14 03:16 test1.sh  
  4. -rwxr-xr-x 1 root root 114 2013-06-14 03:20 test2.sh  
  5. root@ubuntu:~/resource/shell-study/0614-2013# echo `ls -l`  
  6. total 8 -rwxr-xr-x 1 root root 860 2013-06-14 03:16 test1.sh -rwxr-xr-x 1 root root 114 2013-06-14 03:20 test2.sh  
  7. root@ubuntu:~/resource/shell-study/0614-2013#   
所以, 我们怎么做才能够在一个需要echo出来的字符串中嵌入换行呢?看看下面的实例

  1. #!/bin/bash  
  2.   
  3. # 嵌入一个换行?  
  4. echo "Why doesn't this string \n split on two lines?"  
  5. # 上边这句的\n将被打印出来. 达不到换行的目的.  
  6.   
  7. # 让我们再试试其他方法.  
  8.   
  9. echo  
  10.       
  11. echo $"A line of text containing  
  12. a linefeed."  
  13. # 打印出两个独立的行(嵌入换行成功了).  
  14. # 但是, 是否必须有"$"作为变量前缀?  
  15.   
  16. echo  
  17.   
  18. echo "This string splits  
  19. on two lines."   # 并不是非有"$"不可.  
  20.   
  21. echo  
  22. echo "---------------"  
  23. echo  
  24.   
  25. echo -n $"Another line of text containing  
  26. a linefeed."  
  27. # 打印出两个独立的行(嵌入换行成功了).  
  28. # 即使使用了-n选项, 也没能阻止换行. (译者注: -n 阻止了第2个换行)  
  29.   
  30.   
  31. echo  
  32. echo "---------------"  
  33. echo  
  34.   
  35. # 然而, 下边的代码就没能像期望的那样运行.  
  36. # 为什么失败? 提示: 因为分配到了变量.  
  37. string1=$"Yet another line of text containing  
  38. a linefeed (maybe)."  
  39.   
  40. echo $string1  
  41. # 换行变成了空格.only one line  
  42. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test3.sh   
  2. Why doesn't this string \n split on two lines?  
  3.   
  4. A line of text containing  
  5. a linefeed.  
  6.   
  7. This string splits  
  8. on two lines.  
  9.   
  10. ---------------  
  11.   
  12. Another line of text containing  
  13. a linefeed.  
  14. ---------------  
  15.   
  16. Yet another line of text containing a linefeed (maybe).  
  17. root@ubuntu:~/resource/shell-study/0614-2013#   

所以这里echo命令是shell的一个内建命令, 与/bin/echo不同, 虽然行为相似,同样你可以用下面语句方法得到验证:

  1. root@ubuntu:~/resource/shell-study/0614-2013# type -a echo  
  2. echo is a shell builtin  
  3. echo is /bin/echo  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   

printf

printf命令, 格式化输出, 是echo命令的增强版. 它是C语言printf()库函数的一个有限的变形, 并且在语法上有些不同.

printf format-string... parameter...

这是Bash的内建版本, 与/bin/printf或者/usr/bin/printf命令不同. 如果想更深入的了解, 请察看printf(系统命令)的man页.老版本的Bash可能不支持printf.同样你可以用以下方法查究:

  1. root@ubuntu:~/resource/shell-study/0614-2013# type -a printf  
  2. printf is a shell builtin  
  3. printf is /usr/bin/printf  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   
接下来看一个使用printf的例子

  1. #!/bin/bash  
  2.   
  3. PI=3.14159265358979  
  4. DecimalConstant=31373  
  5. Message1="Greetings,"  
  6. Message2="Earthling."  
  7.   
  8. echo  
  9. printf "Pi to 2 decimal places = %1.2f" $PI  
  10. echo  
  11. printf "Pi to 9 decimal places = %1.9f" $PI  # 都能够正确的结束.  
  12.   
  13. printf "\n"                                  # 打印一个换行,  
  14.                                              # 等价于 'echo' . . .  
  15.   
  16. printf "Constant = \t%d\n" $DecimalConstant  # 插入一个 tab (\t).  
  17.   
  18. printf "%s %s \n" $Message1 $Message2  
  19.   
  20. echo  
  21.   
  22. # ==========================================#  
  23. # 模拟C函数, sprintf().  
  24. # 使用一个格式化的字符串来加载一个变量.  
  25.   
  26. echo  
  27.   
  28. Pi12=$(printf "%1.12f" $PI)  
  29. echo "Pi to 12 decimal places = $Pi12"  
  30.   
  31. Msg=`printf "%s %s \n" $Message1 $Message2`  
  32. echo $Msg; echo  
  33.   
  34. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test4.sh   
  2.   
  3. Pi to 2 decimal places = 3.14  
  4. Pi to 9 decimal places = 3.141592654  
  5. Constant =  31373  
  6. Greetings, Earthling.   
  7.   
  8.   
  9. Pi to 12 decimal places = 3.141592653590  
  10. Greetings, Earthling.  
  11.   
  12. root@ubuntu:~/resource/shell-study/0614-2013#  
使用printf的其中最主要的应用就是格式化错误消息.

  1. #!/bin/bash  
  2.   
  3. E_BADDIR=65  
  4.   
  5. var=nonexistent_directory  
  6.   
  7. error()  
  8. {  
  9.   printf "$@" >&2  
  10.   # 格式化传递进来的位置参数, 并把它们送到stderr.  
  11.   echo  
  12.   exit $E_BADDIR  
  13. }  
  14.   
  15. cd $var || error $"Can't cd to %s." "$var"  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test5.sh   
  2. ./test5.sh: line 15: cd: nonexistent_directory: No such file or directory  
  3. Can't cd to nonexistent_directory.  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   
这里有个$@,也许你看这有点面熟,不过就是不知道是什么意思,这里多花点时间来说明一下$*,$@,$#这三兄弟到底有什么区别,看实例吧,这是最好的办法

  1. #!/bin/bash   
  2. my_fun() {   
  3.     echo "$#"   
  4.     echo "$@"  
  5.     echo "$*"  
  6. }   
  7. echo "result:"  
  8. my_fun "1" "2" "3"   
  9. echo   
  10. my_fun "1 2" "3"  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test6.sh   
  2. result:  
  3. 3  
  4. 1 2 3  
  5. 1 2 3  
  6.   
  7. 2  
  8. 1 2 3  
  9. 1 2 3  
  10. root@ubuntu:~/resource/shell-study/0614-2013#   
其中:

$*表示所有这些参数都被双引号引住。若一个脚本接收两个参数,$*等于$1$2

$@表示所有这些参数都分别被双引号引住,若一个脚本接收到两个参数,$@等价于$1$2

$#表示提供给脚本的参数号

对于这里第一条语句:

$*为"1 2 3"(一起被引号包住)

$@为"1" "2" "3"(分别被包住)

$#为3(参数数量)

所以上面error传入参数为"Can't cd to %s."  和 “nonexistent_directory”,所以printf “$@” 就相当于printf "Can't cd to %s." “nonexistent_directory”

read

从stdin中"读取"一个变量的值, 也就是, 和键盘进行交互, 来取得变量的值. 使用-a参数可以read数组变量

使用read来进行变量分配的实例:

  1. #!/bin/bash  
  2.   
  3. echo -n "Enter the value of variable 'var1': "  
  4. # -n 选项, 阻止换行.  
  5.   
  6. read var1  
  7. # 注意: 在var1前面没有'$', 因为变量正在被设置.  
  8.   
  9. echo "var1 = $var1"  
  10.   
  11. echo  
  12.   
  13. # 一个单独的'read'语句可以设置多个变量.  
  14. echo -n "Enter the values of variables 'var2' and 'var3'\未换行  
  15.  (separated by a space or tab): "  
  16. read var2 var3  
  17. echo "var2 = $var2      var3 = $var3"  
  18. # 如果你只输入了一个值, 那么其他的变量还是处于未设置状态(null).  
  19.   
  20. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test7.sh   
  2. Enter the value of variable 'var1': df sdf df  
  3. var1 = df sdf df  
  4.   
  5. Enter the values of variables 'var2' and 'var3'\未换行  
  6.  (separated by a space or tab): dsf dfgd dfa sfdsa  
  7. var2 = dsf      var3 = dfgd dfa sfdsa  
  8. root@ubuntu:~/resource/shell-study/0614-2013#  
很简单的实例,没有必要多做说明,不过,一个不带变量参数的read命令, 将会把来自键盘的输入存入到专用变量$REPLY中,这一点倒是值得我们关注的

  1. #!/bin/bash  
  2.   
  3. echo  
  4.   
  5. # -------------------------- #  
  6. echo -n "Enter a value: "  
  7. read var  
  8. echo "\"var\" = "$var""  
  9. # 到这里为止, 都与期望的一样.  
  10. # -------------------------- #  
  11.   
  12. echo  
  13.   
  14. # ------------------------------------------------------------------- #  
  15. echo -n "Enter another value: "  
  16. read           #  没有变量分配给'read'命令, 所以...  
  17.                #+ 输入将分配给默认变量, $REPLY.  
  18. var="$REPLY"  
  19. echo "\"var\" = "$var""  
  20. # 这部分代码和上边的代码等价.  
  21. # ------------------------------------------------------------------- #  
  22.   
  23. echo  
  24.   
  25. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test8.sh   
  2.   
  3. Enter a value: df  
  4. "var" = df  
  5.   
  6. Enter another value: fg fg  
  7. "var" = fg fg  
  8.   
  9. root@ubuntu:~/resource/shell-study/0614-2013#   
一般的, 当输入给read时, 输入一个\, 然后回车,将会阻止产生一个新行. -r选项将会让 \ 转义.

  1. #!/bin/bash  
  2.   
  3. echo  
  4.   
  5. echo "Enter a string terminated by a \\, then press <ENTER>."  
  6. echo "Then, enter a second string, and again press <ENTER>."  
  7. read var1     # 当 read $var1 时, "\" 将会阻止产生新行.  
  8.               #     first line \  
  9.               #     second line  
  10.   
  11. echo "var1 = $var1"  
  12. #     var1 = first line second line  
  13.   
  14. #  对于每个以 "\" 结尾的行,  
  15. #+ 你都会看到一个下一行的提示符, 让你继续向var1输入内容.  
  16.   
  17. echo; echo  
  18.   
  19. echo "Enter another string terminated by a \\ , then press <ENTER>."  
  20. read -r var2  # -r 选项会让 "\" 转义.  
  21.               #     first line \  
  22.   
  23. echo "var2 = $var2"  
  24. #     var2 = first line \  
  25.   
  26. # 第一个 <ENTER> 就会结束var2变量的录入.  
  27.   
  28. echo  
  29.   
  30. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# chmod +x test9.sh   
  2. root@ubuntu:~/resource/shell-study/0614-2013# ./test9.sh   
  3.   
  4. Enter a string terminated by a \, then press <ENTER>.  
  5. Then, enter a second string, and again press <ENTER>.  
  6. first line \  
  7. second line  
  8. var1 = first line second line  
  9.   
  10.   
  11. Enter another string terminated by a \ , then press <ENTER>.  
  12. first line \  
  13. var2 = first line \  
  14.   
  15. root@ubuntu:~/resource/shell-study/0614-2013#   
read命令有些有趣的选项, 这些选项允许打印出一个提示符, 然后在不输入ENTER的情况下, 可以读入你所按下的字符的内容.不过这个功能也许并不怎么常用,知道就好

  1. #!/bin/bash  
  2. # 不敲回车, 读取一个按键字符.  
  3.   
  4. read -s -n5 -p "Hit a key " keypress  
  5. echo; echo "Keypress was "\"$keypress\""."  
  6.   
  7. # -s 选项意味着不打印输入.  
  8. # -n N 选项意味着只接受N个字符的输入.  
  9. # -p 选项意味着在读取输入之前打印出后边的提示符.  
  10.   
  11. # 使用这些选项是有技巧的, 因为你需要用正确的顺序来使用它们.  
  12. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test10.sh   
  2. Hit a key   
  3. Keypress was "HELLO".  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   
read命令的-n选项还有一个特殊的用途,可以检测方向键, 和一些控制按键,当然上面的实例已经说了,-n还可以指定接收的字符数,就像上面做到的那样

下面看一个-n选项作为检测方向键,或者控制按键的实例

  1. #!/bin/bash  
  2.   
  3. # --------------------------------------------  
  4. # 按键所产生的字符编码.  
  5. arrowup='\[A'  
  6. arrowdown='\[B'  
  7. arrowrt='\[C'  
  8. arrowleft='\[D'  
  9. insert='\[2'  
  10. delete='\[3'  
  11. # --------------------------------------------  
  12.   
  13. SUCCESS=0  
  14. OTHER=65  
  15.   
  16. echo -n "Press a key...  "  
  17. # 如果不是上边列表所列出的按键,  
  18. # 可能还是需要按回车. (译者注: 因为一般按键是一个字符)  
  19. read -n3 key                      # 读取3个字符.  
  20.   
  21. echo -n "$key" | grep "$arrowup"  # 检查输入字符是否匹配.  
  22. if [ "$?" -eq $SUCCESS ]  
  23. then  
  24.   echo "Up-arrow key pressed."  
  25.   exit $SUCCESS  
  26. fi  
  27.   
  28. echo -n "$key" | grep "$arrowdown"  
  29. if [ "$?" -eq $SUCCESS ]  
  30. then  
  31.   echo "Down-arrow key pressed."  
  32.   exit $SUCCESS  
  33. fi  
  34.   
  35. echo -n "$key" | grep "$arrowrt"  
  36. if [ "$?" -eq $SUCCESS ]  
  37. then  
  38.   echo "Right-arrow key pressed."  
  39.   exit $SUCCESS  
  40. fi  
  41.   
  42. echo -n "$key" | grep "$arrowleft"  
  43. if [ "$?" -eq $SUCCESS ]  
  44. then  
  45.   echo "Left-arrow key pressed."  
  46.   exit $SUCCESS  
  47. fi  
  48.   
  49. echo -n "$key" | grep "$insert"  
  50. if [ "$?" -eq $SUCCESS ]  
  51. then  
  52.   echo "\"Insert\" key pressed."  
  53.   exit $SUCCESS  
  54. fi  
  55.   
  56. echo -n "$key" | grep "$delete"  
  57. if [ "$?" -eq $SUCCESS ]  
  58. then  
  59.   echo "\"Delete\" key pressed."  
  60.   exit $SUCCESS  
  61. fi  
  62.   
  63.   
  64. echo " Some other key pressed."  
  65.   
  66. exit $OTHER  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test11.sh   
  2. Press a key...  DFD Some other key pressed.  
  3. root@ubuntu:~/resource/shell-study/0614-2013# ./test11.sh   
  4. Up-arrow key pressed.  
  5. root@ubuntu:~/resource/shell-study/0614-2013#   
  6. root@ubuntu:~/resource/shell-study/0614-2013# ./test11.sh   
  7. Press a key...  ^[[B  
  8. Down-arrow key pressed.  
  9. root@ubuntu:~/resource/shell-study/0614-2013#   
  10. root@ubuntu:~/resource/shell-study/0614-2013# ./test11.sh   
  11. Press a key...  ^[[D  
  12. Left-arrow key pressed.  
  13. root@ubuntu:~/resource/shell-study/0614-2013#   

对于read命令来说, -n选项不会检测ENTER(新行)键.read命令的-t选项允许时间输入

read命令也可以从重定向的文件中"读取"变量的值. 如果文件中的内容超过一行, 那么只有第一行被分配到这个变量中. 如果read命令的参数个数超过一个, 那么每个变量都会从文件中取得一个分配的字符串作为变量的值, 这些字符串都是以定义的空白字符来进行分隔的. 小心使用!

看一个实例,通过文件重定向来使用read命令

  1. #!/bin/bash  
  2.   
  3. read var1 <data-file  
  4. echo "var1 = $var1"  
  5. # var1将会把"data-file"的第一行的全部内容都为它的值.  
  6.   
  7. read var2 var3 <data-file  
  8. echo "var2 = $var2   var3 = $var3"  
  9. # 注意, 这里的"read"命令将会产生一种不直观的行为.  
  10. # 1) 重新从文件的开头开始读入变量.  
  11. # 2) 每个变量都设置成了以空白分割的字符串.  
  12. #    而不是之前的以整行的内容作为变量的值.  
  13. # 3) 而最后一个变量将会取得第一行剩余的全部部分(译者注: 不管是否以空白分割).  
  14. # 4) 如果需要赋值的变量个数比文件中第一行以空白分割的字符串个数还多的话,  
  15. #    那么这些变量将会被赋空值.  
  16.   
  17. echo "------------------------------------------------"  
  18.   
  19. # 如何用循环来解决上边所提到的问题:  
  20. while read line  
  21. do  
  22.   echo "$line"  
  23. done <data-file  
  24. # 感谢, Heiner Steven 指出了这点.  
  25.   
  26. echo "------------------------------------------------"  
  27.   
  28. # 使用$IFS(内部域分隔变量)来将每行的输入单独的放到"read"中,  
  29. # 前提是如果你不想使用默认空白的话.  
  30.   
  31. echo "List of all users:"  
  32. OIFS=$IFS; IFS=:       # /etc/passwd 使用 ":" 作为域分隔符.  
  33. while read name passwd uid gid fullname ignore  
  34. do  
  35.   echo "$name ($fullname)"  
  36. done </etc/passwd   # I/O 重定向.  
  37. IFS=$OIFS              # 恢复原始的$IFS.  
  38. # 这段代码也是Heiner Steven编写的.  
  39.   
  40.   
  41.   
  42. #  在循环内部设置$IFS变量,  
  43. #+ 而不用把原始的$IFS  
  44. #+ 保存到临时变量中.  
  45. #  感谢, Dim Segebart, 指出了这点.  
  46. echo "------------------------------------------------"  
  47. echo "List of all users:"  
  48.   
  49. while IFS=: read name passwd uid gid fullname ignore  
  50. do  
  51.   echo "$name ($fullname)"  
  52. done </etc/passwd   # I/O 重定向.  
  53.   
  54. echo  
  55. echo "\$IFS still $IFS"  
  56.   
  57. exit 0  
查看结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# chmod +x test12.sh   
  2. root@ubuntu:~/resource/shell-study/0614-2013# ls  
  3. data-file  test11.sh  test1.sh  test3.sh  test5.sh  test7.sh  test9.sh  
  4. test10.sh  test12.sh  test2.sh  test4.sh  test6.sh  test8.sh  
  5. root@ubuntu:~/resource/shell-study/0614-2013# ./test12.sh   
  6. var1 = 123 456 678 45 23 5678  
  7. var2 = 123   var3 = 456 678 45 23 5678  
  8. ------------------------------------------------  
  9. 123 456 678 45 23 5678  
  10. sdf sdg sdf ghds  
  11. ------------------------------------------------  
  12. List of all users:  
  13. root (root)  
  14. daemon (daemon)  
  15. bin (bin)  
  16. sys (sys)  
  17. sync (sync)  
  18. games (games)  
  19. man (man)  
  20. lp (lp)  
  21. mail (mail)  
  22. news (news)  
  23. uucp (uucp)  
  24. proxy (proxy)  
  25. www-data (www-data)  
  26. backup (backup)  
  27. list (Mailing List Manager)  
  28. irc (ircd)  
  29. gnats (Gnats Bug-Reporting System (admin))  
  30. nobody (nobody)  
  31. libuuid ()  
  32. syslog ()  
  33. messagebus ()  
  34. avahi-autoipd (Avahi autoip daemon,,,)  
  35. avahi (Avahi mDNS daemon,,,)  
  36. couchdb (CouchDB Administrator,,,)  
  37. speech-dispatcher (Speech Dispatcher,,,)  
  38. usbmux (usbmux daemon,,,)  
  39. haldaemon (Hardware abstraction layer,,,)  
  40. kernoops (Kernel Oops Tracking Daemon,,,)  
  41. pulse (PulseAudio daemon,,,)  
  42. rtkit (RealtimeKit,,,)  
  43. saned ()  
  44. hplip (HPLIP system user,,,)  
  45. gdm (Gnome Display Manager)  
  46. hai-tao (ubuntu 10.04,,,)  
  47. statd ()  
  48. ------------------------------------------------  
  49. List of all users:  
  50. root (root)  
  51. daemon (daemon)  
  52. bin (bin)  
  53. sys (sys)  
  54. sync (sync)  
  55. games (games)  
  56. man (man)  
  57. lp (lp)  
  58. mail (mail)  
  59. news (news)  
  60. uucp (uucp)  
  61. proxy (proxy)  
  62. www-data (www-data)  
  63. backup (backup)  
  64. list (Mailing List Manager)  
  65. irc (ircd)  
  66. gnats (Gnats Bug-Reporting System (admin))  
  67. nobody (nobody)  
  68. libuuid ()  
  69. syslog ()  
  70. messagebus ()  
  71. avahi-autoipd (Avahi autoip daemon,,,)  
  72. avahi (Avahi mDNS daemon,,,)  
  73. couchdb (CouchDB Administrator,,,)  
  74. speech-dispatcher (Speech Dispatcher,,,)  
  75. usbmux (usbmux daemon,,,)  
  76. haldaemon (Hardware abstraction layer,,,)  
  77. kernoops (Kernel Oops Tracking Daemon,,,)  
  78. pulse (PulseAudio daemon,,,)  
  79. rtkit (RealtimeKit,,,)  
  80. saned ()  
  81. hplip (HPLIP system user,,,)  
  82. gdm (Gnome Display Manager)  
  83. hai-tao (ubuntu 10.04,,,)  
  84. statd ()  
  85.   
  86. $IFS still        
  87.   
  88. root@ubuntu:~/resource/shell-study/0614-2013#   

管道输出到read命令中, 使用管道echo输出来设置变量将会失败.然而, 使用管道cat输出看起来能够正常运行.

  1. #!/bin/bash  
  2.   
  3. cat data-file |  
  4. while read line  
  5. do  
  6.     echo $line  
  7. done  
  8.   
  9. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test13.sh   
  2. 123 456 678 45 23 5678  
  3. sdf sdg sdf ghds  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   
不过我在使用echo测试时也是可以的

  1. #!/bin/bash  
  2.   
  3. echo "dfdfsdf";echo "1234" |  
  4. while read line  
  5. do  
  6.     echo $line  
  7. done  
  8.   
  9. exit 0  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# ./test13.sh   
  2. dfdfsdf  
  3. 1234  
  4. root@ubuntu:~/resource/shell-study/0614-2013#   

管道输出到read中的问题

  1. #!/bin/sh  
  2. # readpipe.sh  
  3. # 这个例子是由Bjon Eriksson所编写的.  
  4.   
  5. last="(null)"  
  6. cat $0 |  
  7. while read line  
  8. do  
  9.     echo "{$line}"  
  10.     last=$line  
  11. done  
  12. printf "\nAll done, last:$last\n"  
  13.   
  14. exit 0  # 代码结束.  
  15.         # 下边是脚本的(部分)输出.  
  16.         # 'echo'出了多余的大括号.  
结果:
  1. root@ubuntu:~/resource/shell-study/0614-2013# chmod +x test14.sh   
  2. root@ubuntu:~/resource/shell-study/0614-2013# ./test14.sh   
  3. {#!/bin/sh}  
  4. {# readpipe.sh}  
  5. {# 这个例子是由Bjon Eriksson所编写的.}  
  6. {}  
  7. {last="(null)"}  
  8. {cat $0 |}  
  9. {while read line}  
  10. {do}  
  11. {echo "{$line}"}  
  12. {last=$line}  
  13. {done}  
  14. {printf "nAll done, last:$lastn"}  
  15. {}  
  16. {exit 0  # 代码结束.}  
  17. {# 下边是脚本的(部分)输出.}  
  18. {# 'echo'出了多余的大括号.}  
  19.   
  20. All done, last:(null)  
  21. root@ubuntu:~/resource/shell-study/0614-2013#   

先到这里了,O(∩_∩)O~

我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html

待续。。。。。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多