Chinaunix

标题: SHELL中如何获得管道前面的命令的返回值? [打印本页]

作者: xuphys    时间: 2011-01-25 10:14
标题: SHELL中如何获得管道前面的命令的返回值?
SHELL中如何获得管道前面的命令的返回值?
bash中可以用PIPESTATUS,但是其他的shell呢?
谢谢。
作者: 南极雨    时间: 2011-01-25 10:42
本帖最后由 南极雨 于 2011-01-25 10:43 编辑

好像其他的shell也都支持这个pip吧...
windows下的powershell 用的不多,不太清楚!
作者: xuphys    时间: 2011-01-25 11:35
powershell貌似不行,不过我不关心。我想知道其他的UNIX shell是否支持呢?
作者: xuphys    时间: 2011-01-25 11:36
ksh好像就没有。。。
作者: cjaizss    时间: 2011-01-25 13:11
其他shell没有很正常
作者: ly5066113    时间: 2011-01-25 13:14
  1. comp.unix.shell FAQ(转载http://home.comcast.net/~j.p.h/)
  2. 11. How do I get the exit code of cmd1 in cmd1|cmd2

  3.     First, note that cmd1 exit code could be non-zero and still don't
  4.     mean an error. This happens for instance in

  5.     cmd | head -1

  6.     you might observe a 141 (or 269 with ksh93) exit status of cmd1,
  7.     but it's because cmd was interrupted by a SIGPIPE signal when
  8.     "head -1" terminated after having read one line.

  9.     To know the exit status of the elements of a pipeline
  10.     cmd1 | cmd2 | cmd3

  11.     a. with zsh:

  12.        The exit codes are provided in the pipestatus special array.
  13.        cmd1 exit code is in $pipestatus[1], cmd3 exit code in
  14.        $pipestatus[3], so that $? is always the same as
  15.        $pipestatus[-1].

  16.     b. with bash:

  17.        The exit codes are provided in the PIPESTATUS special array.
  18.        cmd1 exit code is in ${PIPESTATUS[0]}, cmd3 exit code in
  19.        ${PIPESTATUS[2]}, so that $? is always the same as
  20.        ${PIPESTATUS: -1}.

  21.     c. with any other Bourne like shells

  22.        You need to use a trick to pass the exit codes to the main
  23.        shell.  You can do it using a pipe(2). Instead of running
  24.        "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
  25.        to the shell.

  26.        exec 3>&1
  27.        eval `
  28.          # now, inside the `...`, fd4 goes to the pipe
  29.          # whose other end is read and passed to eval;
  30.          # fd1 is the normal standard output preserved
  31.          # the line before with exec 3>&1
  32.          exec 4>&1 >&3 3>&-
  33.          {
  34.            cmd1 4>&-; echo "ec1=$?;" >&4
  35.          } | {
  36.            cmd2 4>&-; echo "ec2=$?;" >&4
  37.          } | cmd3
  38.          echo "ec3=$?;" >&4
  39.        `

  40.     d. with a POSIX shell

  41.        You can use this function to make it easier:

  42.        run() {
  43.          j=1
  44.          while eval "\${pipestatus_$j+:} false"; do
  45.            unset pipestatus_$j
  46.            j=$(($j+1))
  47.          done
  48.          j=1 com= k=1 l=
  49.          for a; do
  50.            if [ "x$a" = 'x|' ]; then
  51.              com="$com { $l "'3>&-
  52.                          echo "pipestatus_'$j'=$?" >&3
  53.                        } 4>&- |'
  54.              j=$(($j+1)) l=
  55.            else
  56.              l="$l \"\$$k\""
  57.            fi
  58.            k=$(($k+1))
  59.          done
  60.          com="$com $l"' 3>&- >&4 4>&-
  61.                     echo "pipestatus_'$j'=$?"'
  62.          exec 4>&1
  63.          eval "$(exec 3>&1; eval "$com")"
  64.          exec 4>&-
  65.          j=1
  66.          while eval "\${pipestatus_$j+:} false"; do
  67.            eval "[ \$pipestatus_$j -eq 0 ]" || return 1
  68.            j=$(($j+1))
  69.          done
  70.          return 0
  71.        }
  72.       
  73.        use it as:
  74.       
  75.        run cmd1 \| cmd2 \| cmd3
  76.        exit codes are in $pipestatus_1, $pipestatus_2, $pipestatus_3
复制代码

作者: cjaizss    时间: 2011-01-25 13:33
ly5066113 发表于 2011-01-25 13:14



    呵呵,办法总是人想的
作者: xuphys    时间: 2011-02-03 17:08
似乎只能用很麻烦的的方法了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2