LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 783|回复: 6

问个关于read的问题

[复制链接]
发表于 2005-10-31 13:02:22 | 显示全部楼层 |阅读模式
bash环境:

  1. ~$  echo $dd
  2. bb
  3. ~$  echo cc|read dd
  4. ~$  echo $dd
  5. bb
  6. ~$  echo cc|if read dd; then echo $dd; fi
  7. cc
  8. ~$  echo $dd
  9. bb
复制代码

怎么才能把这个值(cc)传递出来
发表于 2005-10-31 19:43:10 | 显示全部楼层
echo cc|read dd
无论dd没有被定义,cc都不会传给dd的,
这样的语句可以参考大多数的shell的手册,都有解释,例如:
http://www.cs.mun.ca/~michael/pdksh/
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-31 23:26:27 | 显示全部楼层
不是的, 其实我是想把字符串比如:
a='a b c'
一次性传给三个变量: e f g
使得
e=a
f=b
g=c
即:
echo $e $f $g
输出
a b c
我不明白的是
echo cc|if read dd; then echo $dd; fi
的时候在判断语句内部dd的值是cc,为什么出来的时候就没有了
回复 支持 反对

使用道具 举报

发表于 2005-11-1 09:36:14 | 显示全部楼层
是不是可以认为read新建了个bash环境?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-11-1 12:02:17 | 显示全部楼层
不知道,可能是以在 寂寞烈火 提到的地方的BUG REPORT看到这个

  1. * pdksh 5.1.3, - (reported by Brad Warkentin & others): if the last command of
  2.   a pipeline is a shell builtin, it is not executed in the parent shell,
  3.   so "echo a b | read foo bar" does not set foo and bar in the parent
  4.   shell (at&t ksh will).
  5.   [see Mail.7:32,Mail.9:65]
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-11-1 12:08:39 | 显示全部楼层
在Bash的FAQ里找到这个:

  1. E4) If I pipe the output of a command into `read variable', why doesn't
  2.     the output show up in $variable when the read command finishes?

  3. This has to do with the parent-child relationship between Unix
  4. processes.  It affects all commands run in pipelines, not just
  5. simple calls to `read'.  For example, piping a command's output
  6. into a `while' loop that repeatedly calls `read' will result in
  7. the same behavior.

  8. Each element of a pipeline, even a builtin or shell function,
  9. runs in a separate process, a child of the shell running the
  10. pipeline.  A subprocess cannot affect its parent's environment.
  11. When the `read' command sets the variable to the input, that
  12. variable is set only in the subshell, not the parent shell.  When
  13. the subshell exits, the value of the variable is lost.

  14. Many pipelines that end with `read variable' can be converted
  15. into command substitutions, which will capture the output of
  16. a specified command.  The output can then be assigned to a
  17. variable:

  18.         grep ^gnu /usr/lib/news/active | wc -l | read ngroup

  19. can be converted into

  20.         ngroup=$(grep ^gnu /usr/lib/news/active | wc -l)

  21. This does not, unfortunately, work to split the text among
  22. multiple variables, as read does when given multiple variable
  23. arguments.  If you need to do this, you can either use the
  24. command substitution above to read the output into a variable
  25. and chop up the variable using the bash pattern removal
  26. expansion operators or use some variant of the following
  27. approach.

  28. Say /usr/local/bin/ipaddr is the following shell script:

  29. #! /bin/sh
  30. host `hostname` | awk '/address/ {print $NF}'

  31. Instead of using

  32.         /usr/local/bin/ipaddr | read A B C D

  33. to break the local machine's IP address into separate octets, use

  34.         OIFS="$IFS"
  35.         IFS=.
  36.         set -- $(/usr/local/bin/ipaddr)
  37.         IFS="$OIFS"
  38.         A="$1" B="$2" C="$3" D="$4"

  39. Beware, however, that this will change the shell's positional
  40. parameters.  If you need them, you should save them before doing
  41. this.

  42. This is the general approach -- in most cases you will not need to
  43. set $IFS to a different value.

  44. Some other user-supplied alternatives include:

  45. read A B C D << HERE
  46.     $(IFS=.; echo $(/usr/local/bin/ipaddr))
  47. HERE

  48. and, where process substitution is available,

  49. read A B C D < <(IFS=.; echo $(/usr/local/bin/ipaddr))
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-11-1 12:38:25 | 显示全部楼层
exactly! because the varable definition is in the subprocess. you should write a script for it. then run it in the current shell like this $ . script. look at the attachment.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表