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

  免費注冊 查看新帖 |

Chinaunix

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

[學(xué)習(xí)共享] 求助,關(guān)于shell內(nèi)使用tee記錄日志,同時獲取函數(shù)返回值。 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-11-28 18:22 |只看該作者 |倒序瀏覽
5可用積分
我是想在入口函數(shù)直接加上 tee命令將標(biāo)準(zhǔn)輸出打印在屏幕的同時,也記錄到文件中。 同時也希望能夠捕捉到main的返回值。

但是發(fā)現(xiàn)在使用了管道符號之后,main的返回值取不到了,$?代表的是管道之后命令的返回值。  本想用一個全局變量retCode來記錄main的退出值,但發(fā)現(xiàn)retCode的修改同樣無法傳出main函數(shù)外部。

請教解決方案,謝謝!
  1. #!/bin/bash
  2. retCode=0
  3. function main()
  4. {
  5.    retCode=100
  6.    exit 100
  7. }

  8. main 2>&1 | tee -a logfile
  9. echo $?
  10. echo $reCode
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2013-11-28 19:22 |只看該作者
自問自答吧。

網(wǎng)上查來的,bash解決方案如下:
The exit codes are provided in the PIPESTATUS special array. cmd1 exit code is in ${PIPESTATUS[0]}, cmd3 exit code in ${PIPESTATUS[2]}, so that $? is always the same as ${PIPESTATUS: -1}.

所以以上問題只需要判斷 ${PIPESTATUS[0]},就是main的退出碼了。。

同樣也可以在執(zhí)行main之前設(shè)置:set -o pipefail, 就可以用$?的值了。

論壇徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16賽季CBA聯(lián)賽之北京
日期:2016-07-06 15:42:0715-16賽季CBA聯(lián)賽之同曦
日期:2016-06-12 10:38:0915-16賽季CBA聯(lián)賽之佛山
日期:2016-05-27 11:54:56黃金圣斗士
日期:2015-12-02 11:44:35白銀圣斗士
日期:2015-11-25 14:32:43白銀圣斗士
日期:2015-11-23 12:53:352015亞冠之布里斯班獅吼
日期:2015-10-21 16:55:482015亞冠之首爾
日期:2015-09-01 16:46:052015亞冠之德黑蘭石油
日期:2015-08-31 11:39:192015亞冠之薩濟拖拉機
日期:2015-08-28 21:06:5315-16賽季CBA聯(lián)賽之廣東
日期:2016-07-12 14:58:53
3 [報告]
發(fā)表于 2013-11-28 19:25 |只看該作者
函數(shù)跟管道結(jié)合在一起,似乎是變成了一個subshell了,所以遇到了exit,腳本也沒有退出,函數(shù)里的變量也獲取不到(子進程嘛,退出了啥也沒了),這種情況以前還真沒注意,有點兒意思,我暫時也沒找到相關(guān)的文檔,等待高手回答。

論壇徽章:
5
白羊座
日期:2014-10-28 11:23:27水瓶座
日期:2015-01-20 10:19:022015亞冠之柏斯波利斯
日期:2015-07-11 18:17:2015-16賽季CBA聯(lián)賽之同曦
日期:2015-12-23 12:38:582016猴年福章徽章
日期:2016-02-18 15:30:34
4 [報告]
發(fā)表于 2013-11-28 19:25 |只看該作者
回復(fù) 2# legone2008
學(xué)習(xí)了


   

論壇徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16賽季CBA聯(lián)賽之北京
日期:2016-07-06 15:42:0715-16賽季CBA聯(lián)賽之同曦
日期:2016-06-12 10:38:0915-16賽季CBA聯(lián)賽之佛山
日期:2016-05-27 11:54:56黃金圣斗士
日期:2015-12-02 11:44:35白銀圣斗士
日期:2015-11-25 14:32:43白銀圣斗士
日期:2015-11-23 12:53:352015亞冠之布里斯班獅吼
日期:2015-10-21 16:55:482015亞冠之首爾
日期:2015-09-01 16:46:052015亞冠之德黑蘭石油
日期:2015-08-31 11:39:192015亞冠之薩濟拖拉機
日期:2015-08-28 21:06:5315-16賽季CBA聯(lián)賽之廣東
日期:2016-07-12 14:58:53
5 [報告]
發(fā)表于 2013-11-28 19:31 |只看該作者
回復(fù) 2# legone2008


    學(xué)習(xí)了。還真是,求源鏈接說明文檔

論壇徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16賽季CBA聯(lián)賽之北京
日期:2016-07-06 15:42:0715-16賽季CBA聯(lián)賽之同曦
日期:2016-06-12 10:38:0915-16賽季CBA聯(lián)賽之佛山
日期:2016-05-27 11:54:56黃金圣斗士
日期:2015-12-02 11:44:35白銀圣斗士
日期:2015-11-25 14:32:43白銀圣斗士
日期:2015-11-23 12:53:352015亞冠之布里斯班獅吼
日期:2015-10-21 16:55:482015亞冠之首爾
日期:2015-09-01 16:46:052015亞冠之德黑蘭石油
日期:2015-08-31 11:39:192015亞冠之薩濟拖拉機
日期:2015-08-28 21:06:5315-16賽季CBA聯(lián)賽之廣東
日期:2016-07-12 14:58:53
6 [報告]
發(fā)表于 2013-11-28 19:43 |只看該作者
打開ABS,看看第106頁。這是@Shell_HAT大俠的慣用語,同時問下大俠,樓主這一例,main 后接管道,而main里有exit,卻沒有退出腳本,是否是這樣的方式成了subshell?

Advanced Bash-Scripting Guide 3.9.1 中文版.pdf
http://72891.cn/thread-1610033-1-1.html

論壇徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16賽季CBA聯(lián)賽之北京
日期:2016-07-06 15:42:0715-16賽季CBA聯(lián)賽之同曦
日期:2016-06-12 10:38:0915-16賽季CBA聯(lián)賽之佛山
日期:2016-05-27 11:54:56黃金圣斗士
日期:2015-12-02 11:44:35白銀圣斗士
日期:2015-11-25 14:32:43白銀圣斗士
日期:2015-11-23 12:53:352015亞冠之布里斯班獅吼
日期:2015-10-21 16:55:482015亞冠之首爾
日期:2015-09-01 16:46:052015亞冠之德黑蘭石油
日期:2015-08-31 11:39:192015亞冠之薩濟拖拉機
日期:2015-08-28 21:06:5315-16賽季CBA聯(lián)賽之廣東
日期:2016-07-12 14:58:53
7 [報告]
發(fā)表于 2013-11-28 20:08 |只看該作者
本帖最后由 reyleon 于 2013-11-28 20:19 編輯


http://www.unix.com/showthread.php?p=302265010
  1. ======================================================================

  2. 13. 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

  77. ======================================================================
復(fù)制代碼

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
8 [報告]
發(fā)表于 2013-11-28 20:30 |只看該作者
  1. 不明覺厲
復(fù)制代碼

論壇徽章:
6
摩羯座
日期:2013-08-24 10:43:10獅子座
日期:2013-08-25 10:27:06天秤座
日期:2013-09-11 20:28:44午馬
日期:2014-09-28 16:06:0015-16賽季CBA聯(lián)賽之八一
日期:2016-12-19 13:55:0515-16賽季CBA聯(lián)賽之天津
日期:2016-12-20 14:01:23
9 [報告]
發(fā)表于 2013-11-28 22:23 |只看該作者
用一個文件來記錄函數(shù)的退出值

論壇徽章:
33
榮譽會員
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT運維版塊每日發(fā)帖之星
日期:2016-04-17 06:23:27操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-04-18 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-04-24 06:20:0015-16賽季CBA聯(lián)賽之天津
日期:2016-05-06 12:46:59
10 [報告]
發(fā)表于 2013-11-29 08:27 |只看該作者
回復(fù) 1# legone2008

在 函數(shù)里 用 exit 還想拿到 返回值, 有性格.

體育老師告訴我, 函數(shù) 要用 return 才會 返回到主程序, exit 就退出程序直接返回系統(tǒng)了.


   
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(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