pipe - Bash: Is trap while piping work as expected? -


here minimal code issue demonstration: http://pastebin.com/5txdpsh5

#!/bin/bash set -e set -o pipefail  function echotraps() {     echo "= on start:"     trap -p     trap -- 'echo func-exit' exit     echo "= after set new:"     trap -p     # can ensure after script done - file '/tmp/tmp.txt' not created     trap -- 'echo sig 1>/tmp/tmp.txt' sigpipe sighup sigint sigquit sigterm }  trap -- 'echo main-exit1' exit  echo "===== subshell trap" ( echotraps; )  echo "===== pipe trap" echotraps | cat  echo "===== done everything" 

output

===== subshell trap = on start: = after set new: trap -- 'echo func-exit' exit func-exit ===== pipe trap = on start: = after set new: trap -- 'echo func-exit' exit ===== done main-exit1 

expected output

===== subshell trap = on start: = after set new: trap -- 'echo func-exit' exit func-exit ===== pipe trap = on start: = after set new: trap -- 'echo func-exit' exit func-exit                 <---- here expected difference ===== done main-exit1 

nb: tested osx 10.9.2 bash (3.2.51) - other versions of bash has same difference between actual expected output, , described bellow

here more test cases amusement:

$ cat traps.sh #!/bin/bash  echotraps() {         echo "entering echotraps()"          printf "  traps: %s\n" "$(trap -p)"          echo "  setting trap"         trap -- 'echo "func-exit()"' exit          printf "  traps: %s\n" "$(trap -p)"          echo "exiting echotraps()" }  trap -- 'echo "main-exit()"' exit  echo "===== calling '( echotraps; )'" ( echotraps; ) echo  echo "===== calling 'echotraps | cat'" echotraps | cat echo  echo "===== calling '( echotraps; ) | cat'" ( echotraps; ) | cat echo  echo "===== calling '{ echotraps; } | cat'" { echotraps; } | cat echo 

bash-4.2.25(1)

$ ./traps.sh ===== calling '( echotraps; )' entering echotraps()   traps:   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps() func-exit()  ===== calling 'echotraps | cat' entering echotraps()   traps: trap -- 'echo "main-exit()"' exit   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps()  ===== calling '( echotraps; ) | cat' entering echotraps()   traps:   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps() func-exit()  ===== calling '{ echotraps; } | cat' entering echotraps()   traps: trap -- 'echo "main-exit()"' exit   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps()  main-exit() 

bash-4.3.0(1)

$ bash-static-4.3.2/bin/bash-static traps.sh ===== calling '( echotraps; )' entering echotraps()   traps:   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps() func-exit()  ===== calling 'echotraps | cat' entering echotraps()   traps: trap -- 'echo "main-exit()"' exit   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps()  ===== calling '( echotraps; ) | cat' entering echotraps()   traps:   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps() func-exit()  ===== calling '{ echotraps; } | cat' entering echotraps()   traps: trap -- 'echo "main-exit()"' exit   setting trap   traps: trap -- 'echo "func-exit()"' exit exiting echotraps() func-exit()  main-exit() 

bottom line: don't rely on edge-cases this. remember investigating other inconsistencies (not traps) regards subshells , pipes , tried wrap head around bash source code , don't want go down route , try understand why behaves in situations (the code horrible, btw). can see, things seem have been "fixed" and/but both examples behave differently yours.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -