免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7634 | 回复: 7
打印 上一主题 下一主题

SHELL中如何获得管道前面的命令的返回值? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-25 10:14 |只看该作者 |倒序浏览
SHELL中如何获得管道前面的命令的返回值?
bash中可以用PIPESTATUS,但是其他的shell呢?
谢谢。

论坛徽章:
0
2 [报告]
发表于 2011-01-25 10:42 |只看该作者
本帖最后由 南极雨 于 2011-01-25 10:43 编辑

好像其他的shell也都支持这个pip吧...
windows下的powershell 用的不多,不太清楚!

论坛徽章:
0
3 [报告]
发表于 2011-01-25 11:35 |只看该作者
powershell貌似不行,不过我不关心。我想知道其他的UNIX shell是否支持呢?

论坛徽章:
0
4 [报告]
发表于 2011-01-25 11:36 |只看该作者
ksh好像就没有。。。

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
5 [报告]
发表于 2011-01-25 13:11 |只看该作者
其他shell没有很正常

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
6 [报告]
发表于 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
复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
7 [报告]
发表于 2011-01-25 13:33 |只看该作者
ly5066113 发表于 2011-01-25 13:14



    呵呵,办法总是人想的

论坛徽章:
0
8 [报告]
发表于 2011-02-03 17:08 |只看该作者
似乎只能用很麻烦的的方法了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP