Hudson : “yes: standard output: Broken pipe”

后端 未结 5 877
一生所求
一生所求 2021-02-18 19:02

I need to run a shell script in hudson. That script needs an answer from the user. To give an automatic answer I did the following command line :  

yes         


        
5条回答
  •  孤独总比滥情好
    2021-02-18 19:17

    Since yes and ./MyScript.sh can each be run in an explicit subshell, it is possible to background the yes command, send yespid to the ./MyScript.sh subshell and then implement a trap on EXIT there to manually terminate the yes command. (The trap on EXIT should always be implemented in the subshell of the last command of a piped commmand sequence).

    # avoid hangup or "broken pipe" error message when parent process set SIGPIPE to be ignored
    # sleep 0 or cat /dev/null: do nothing but with external command (for a shell builtin command see: help :)
    (
    trap "" PIPE
    ( (sleep 0; exec yes) & echo ${!}; wait ${!} ) | 
       ( 
         trap 'trap - EXIT; kill "$yespid"; exit 0' EXIT
         yespid="$(head -n 1)"
         head -n 10  # replacement for ./MyScript.sh
       )
    echo ${PIPESTATUS[*]}
    )
    

    If you want to exit the yes subshell with exit code 0 you can do this as well:

    # avoid hangup or "broken pipe" error message when parent process set SIGPIPE to be ignored
    # set exit code of yes subshell to 0
    (
    trap "" PIPE
       (
          trap 'trap - TERM; echo "kill from yes subshell ..." 1>&2; kill "${!}"; exit 0' TERM 
          subshell_pid="$(bash -c 'echo "$PPID"')"
          (sleep 0; exec yes) & echo "${subshell_pid}"; wait ${!} 
       ) | 
       ( 
          trap 'trap - EXIT; kill -s TERM "$subshell_pid"; exit' EXIT
          subshell_pid="$(head -n 1)"
          head -n 10  # replacement for ./MyScript.sh
       )
    echo ${PIPESTATUS[*]}
    )
    

提交回复
热议问题