亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 7687 | 回復(fù): 7
打印 上一主題 下一主題

SHELL中如何獲得管道前面的命令的返回值? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-01-25 10:14 |只看該作者 |倒序瀏覽
SHELL中如何獲得管道前面的命令的返回值?
bash中可以用PIPESTATUS,但是其他的shell呢?
謝謝。

論壇徽章:
0
2 [報告]
發(fā)表于 2011-01-25 10:42 |只看該作者
本帖最后由 南極雨 于 2011-01-25 10:43 編輯

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

論壇徽章:
0
3 [報告]
發(fā)表于 2011-01-25 11:35 |只看該作者
powershell貌似不行,不過我不關(guān)心。我想知道其他的UNIX shell是否支持呢?

論壇徽章:
0
4 [報告]
發(fā)表于 2011-01-25 11:36 |只看該作者
ksh好像就沒有。。。

論壇徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-03 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-04 06:20:00
5 [報告]
發(fā)表于 2011-01-25 13:11 |只看該作者
其他shell沒有很正常

論壇徽章:
23
15-16賽季CBA聯(lián)賽之吉林
日期: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聯(lián)賽之山東
日期:2017-12-21 16:39:1915-16賽季CBA聯(lián)賽之廣東
日期:2016-01-19 13:33:372015亞冠之山東魯能
日期:2015-10-13 09:39:062015亞冠之西悉尼流浪者
日期:2015-09-21 08:27:57
6 [報告]
發(fā)表于 2011-01-25 13:14 |只看該作者
  1. comp.unix.shell FAQ(轉(zhuǎn)載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
復(fù)制代碼

論壇徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-03 06:20:00數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2016-08-04 06:20:00
7 [報告]
發(fā)表于 2011-01-25 13:33 |只看該作者
ly5066113 發(fā)表于 2011-01-25 13:14



    呵呵,辦法總是人想的

論壇徽章:
0
8 [報告]
發(fā)表于 2011-02-03 17:08 |只看該作者
似乎只能用很麻煩的的方法了
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP