Why the ERR trap get triggerred in bash from "echo <(cat <<EOF" - bash

I have this script (test.sh) :
#!/bin/bash
set -o errtrace
trap 'echo $BASH_VERSION >&2' ERR
echo <(cat<<EOF
Hello world
EOF
)
Running it, I get :
~/tmp$ bash test.sh
/dev/fd/63
~/tmp$ 5.0.18(1)-release
Two questions :
Why ERR trap get triggerred ?
Why 5.0.18(1)-release is after next prompt, not before it

Your copy of cat exits with a nonzero exit status because it's not able to write its output to stdout (since nothing in your code running in the parent shell actually reads from /dev/fd/63). The subshell spawned to run that copy of cat thus registers an error. As errtrace causes your ERR handler to be used in subshells and other contexts, this thus causes the process substitution's subshell to invoke the handler.
Because this process substitution is running in a subshell, it's asynchronous from the rest of your script; and cat is only given an error after the echo has exited (since it's only at that time that the read end of the FIFO is closed, and thus cat's attempts to write result in a SIGPIPE).

Related

What causes CTRL-C trap echo to fail when using `tee` and no `--ignore-interrupts`?

This is a very specific question, but I am interested to know what causes this behaviour
Observed
Run the following command and trigger "CTRl-C" signal after the fist wait echo.
bash -c "trap 'echo trapped' EXIT; until false &>/dev/null; do echo 'Waiting...'; sleep 3; done" | tee /tmp/test.txt
Notice the following error:
^Cbash: line 1: echo: write error: Broken pipe
Now run the following command and do the same:
bash -c "trap 'echo trapped' EXIT; until false &>/dev/null; do echo 'Waiting...'; sleep 3; done" | tee -i /tmp/test.txt
Notice how there is no error and the trap message is correctly shown.
Question
What would cause this behaviour and why does it seem that I need to use the -i/--ignore-interrupts option in order to correctly support trapping with tee?
Specs
GNU bash, version 5.1.12(1)-release
What causes CTRL-C trap echo to fail when using tee
It told you:
write error: Broken pipe
Because tee terminated, Bash can't write data to the pipe, which causes it to fail.
no --ignore-interrupts?
Because tee is not closed, Bash can write data to the pipe, so it does not exit.
What would cause this behaviour
Typing ctrl+c or sending SIGINT woudl cause tee to terminate, closing the pipe, and making Bash unable to send any more data to the pipe. As you do. (?)
why does it seem that I need to use the -i/--ignore-interrupts option in order to correctly support trapping with tee?
The statement in the question is false. The trap is executed, it just does not print the requested text, because the pipe is closed. the trap actually prints bash: line 1: echo: write error: Broken pipe error message.
I believe you meant to write the error to another file descriptor:
bash -xc "trap 'echo trapped >&2' ...
Also use set -x to debug your scripts, as above.
until null is super odd - just while true or while :. There is no command named null, you could do until false but that's like double negation.

Parent trap visible but not run by subshell

Tested for Bash 5.0.2
According to the GNU Bash Reference Manual,
Bash performs the expansion [of a command substitution] by executing [the] command in a subshell environment
According to The Open Group Base Specifications Issue 6:
when a subshell is entered, traps that are not being ignored are set to the default actions.
So when running the following script:
function a {
trap -p EXIT
}
trap "echo 'parent'" EXIT
echo "$(a)"
(a)
trap - EXIT
echo 'exiting'
... i would expect an output of:
exiting
... but instead I get:
trap -- 'echo '\''parent'\''' EXIT
trap -- 'echo '\''parent'\''' EXIT
exiting
... meaning that the function a - eventhough it is being run in a subshell - is seeing the the parent shell's trap commands (via trap -p) but not executing them.
What is going on here?
You'll notice that the traps trigger exactly according to spec. It's just the output from trap that's unexpected.
This is a feature in Bash 4.2 (release notes):
b. Subshells begun to execute command substitutions or run shell functions or
builtins in subshells do not reset trap strings until a new trap is
specified. This allows $(trap) to display the caller's traps and the
trap strings to persist until a new trap is set.
Normally, people would take this for granted. Consider this totally unsurprising Bash exchange:
bash$ trap
trap -- 'foo' EXIT
trap -- 'bar' SIGINT
bash$ trap | grep EXIT
trap -- 'foo' EXIT
Now look at the result in other shells like Dash, Ksh or Zsh:
dash$ trap
trap -- 'foo' EXIT
trap -- 'bar' INT
dash$ trap | grep EXIT
(no output)
This is perhaps more correct, but I doubt many people would expect it.
You appear to be reading an older version of the specification. In the most recent one,
When a subshell is entered, traps that are not being ignored shall be set to the default actions, except in the case of a command substitution containing only a single trap command, when the traps need not be altered.

trap in bash function does not trigger

I have a fairly complicated bash script hierarchy that depends on functions for nearly everything. The main script is merely a shell that sources in appropriate functions (common ones like my output functions, etc. as well as specific ones for the task being executed) contained within their own files.
Within that framework, I have a particular function that needs to handle being interrupted and cleanup properly. I am handling it's error cleanup in a subfunction within that function
i.e.
function my_function {
function f_err_cleanup {
do error cleanup tasks here
} #/f_err_cleanup{}
echo do stuff
my_other_function -t "some var here"
my_other_other_function var1 var2 var3
echo do more stuff
} #/my_function{}
Adding a trap to the main script like this:
trap "declare -p FUNCNAME ; f_err_cleanup" INT QUIT TERM
fails due to it not knowing about the f_err_cleanup{} function - but the trap triggers.
^C./my_script.sh: line 1: declare: FUNCNAME: not found
./my_script.sh: line 1: f_err_cleanup: command not found
[ ERROR 13 ] Script "my_script.sh" failed.
Putting a trap inside my_function{} never triggers and cntl-c just drops out of the function with a 130 exit code (which the main script then handles generically.
trap "echo inside trap ; declare -p FUNCNAME" EXIT SIGHUP SIGQUIT SIGINT SIGTERM
The above snippet fails as follows when executed
^C[ ERROR 13 ] Script "my_script.sh" failed.
Here's a snip from the outer script:
. my_function
my_function "$#" 2>&1 | tee -a "${RUNTIME_LOG}"
declare -i RC=${PIPESTATUS[0]}
if [[ ${RC} -ne 0 ]]; then
f_err -f -c ${RC} -m "script \"${ME}\"failed\n"
else
f_out "success\n"
exit ${RC}
fi
f_err is a function to print an error message and exit with the provided code. How can I get the trap to work from within the function?
P.S. having the outer trap do a declare -p FUNCNAME returns that FUNCNAME is unknown as well, even though, as I understand it, it should show FUNCNAME[0]="main"
Putting a trap inside my_function{} never triggers
Yes it does. You just can't tell because you used echo to write to stdout, and stdout is a broken pipe because tee was killed by the sigint you're trying to handle.
Here's a MCVE:
#!/bin/bash
myfunction() (
trap 'mycleanup' INT
mycleanup() {
# Write to stderr so we can see it
echo "Cleaning up" >&2
}
echo "doing stuff"
sleep 42
echo "done with stuff"
)
myfunction | tee file
(The function's explicit subshell is not strictly necessary, but helps prevent accidentally botching the script's own traps)
Here's what happens when you run and interrupt it:
$ ./myscript
doing stuff
^CCleaning up
Posting as an answer because, while I have not resolved the conundrum of getting a trap to work when defined within a pipe, I have resolved my issue.
Because my pipe in this case is only to invoke tee to enable logging, I was able to change the piped line to
my_function "$#" &> >( tee -a "${RUNTIME_LOG}" )
Since there is no pipe involved anymore, the trap within the function will now trigger, leaving aside the questionable nature of bash error handling.

How to trap ERR when using 'set -e' in Bash

I have a simple script :
#!/bin/bash
set -e
trap "echo BOO!" ERR
function func(){
ls /root/
}
func
I would like to trap ERR if my script fails (as it will here b/c I do not have the permissions to look into /root). However, when using set -e it is not trapped. Without set -e ERR is trapped.
According to the bash man page, for set -e :
... A trap on ERR, if set, is executed before the shell exits. ...
Why isn't my trap executed? From the man page it seems like it should.
chepner's answer is the best solution: If you want to combine set -e (same as: set -o errexit) with an ERR trap, also use set -o errtrace (same as: set -E).
In short: use set -eE in lieu of just set -e:
#!/bin/bash
set -eE # same as: `set -o errexit -o errtrace`
trap 'echo BOO!' ERR
function func(){
ls /root/
}
# Thanks to -E / -o errtrace, this still triggers the trap,
# even though the failure occurs *inside the function*.
func
A more sophisticated example trap example that prints the message in red and also prints the exit code:
trap 'printf "\e[31m%s: %s\e[m\n" "BOO!" $?' ERR
man bash says about set -o errtrace / set -E:
If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment. The ERR trap is normally not inherited in such cases.
What I believe is happening:
Without -e: The ls command fails inside your function, and, due to being the last command in the function, the function reports ls's nonzero exit code to the caller, your top-level script scope. In that scope, the ERR trap is in effect, and it is invoked (but note that execution will continue, unless you explicitly call exit from the trap).
With -e (but without -E): The ls command fails inside your function, and because set -e is in effect, Bash instantly exits, directly from the function scope - and since there is no ERR trap in effect there (because it wasn't inherited from the parent scope), your trap is not called.
While the man page is not incorrect, I agree that this behavior is not exactly obvious - you have to infer it.
You need to use set -o errtrace for the function to inherit the trap.
We have these options for debugging:
-e Exit immediately on failure
-E If set, any trap on ERR is inherited by shell functions
-u Exit when there is an unbound variable
-o Give a option-name to set
pipefail The return values of last (rightmost) command (exit code)
-v Print all shell input lines as they are read
-x Print trace of commands
For handling the errors we can catch directory with trap
trap 'echo >&2 "Error - exited with status $? at line $LINENO' ERR
Or a better version ref :
trap 'echo >&2 "Error - exited with status $? at line $LINENO:";
pr -tn $0 | tail -n+$((LINENO - 3)) | head -n7' ERR
Or a function:
function __error_handing__(){
local last_status_code=$1;
local error_line_number=$2;
echo 1>&2 "Error - exited with status $last_status_code at line $error_line_number";
perl -slne 'if($.+5 >= $ln && $.-4 <= $ln){ $_="$. $_"; s/$ln/">" x length($ln)/eg; s/^\D+.*?$/\e[1;31m$&\e[0m/g; print}' -- -ln=$error_line_number $0
}
and call it this way:
trap '__error_handing__ $? $LINENO' ERR
Replace ERR with EXIT and it will work.
The syntax of the trap command is: trap [COMMANDS] [SIGNALS]
For more info, please read http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_12_02.html

Ending Timestamp not printing on Shell Script: Using trap

I have a shell script I use for deployments. Since I want to capture the output of the entire process, I've wrapped it in a subshell and tail that out:
#! /usr/bin/env ksh
# deploy.sh
########################################################################
(yadda, yadda, yadda)
########################################################################
# LOGGING WRAPPER
#
dateFormat=$(date +"%Y.%m.%d-%H.%M.%S")
(
print -n "EXECUING: $0 $*: "
date
#
########################################################################
(yadda, yadda, yadda)
#
# Tail Startup
#
trap 'printf "Stopping Script: ";date;exit 0"' INT
print "TAILING LOG: YOU MAY STOP THIS WITH A CTRL-C WHEN YOU SEE THAT SERVER HAS STARTED"
sleep 2
./tailLog.sh
) 2>&1 | tee "deployment.$dateFormat.log"
#
########################################################################
Before I employed the subshell, the trap command worked. When you pressed CNTL-C, the program would print Stopping Script: and the date.
However, I wanted to make sure that no one forgets to save the output of this script, so I employed the subshell to automatically save the output. And, now trap doesn't seem to be working.
What am I doing wrong?
NEW INFORMATION
A little more playing around. I now see the issue isn't the shell or subshell. It's the damn pipe!
If I don't pipe the output to tee, the trap works fine. If I pipe the output to tee, the trap doesn't work.
So, the real question is how do I tee the output and still be able to use trap?
TEST PROGRAM
Before you answer, please, please, try these test programs:
#! /bin/ksh
dateFormat=$(date +"%Y.%m.%d-%H:%M:%S")
(
trap 'printf "The script was killed at: %s\n", "$(date)"' SIGINT
echo "$0 $*"
while sleep 2
do
print -n "The time is now "
date
done
) | tee somefile
And
#! /bin/ksh
dateFormat=$(date +"%Y.%m.%d-%H:%M:%S")
(
trap 'printf "The script was killed at: %s\n", "$(date)"' SIGINT
echo "$0 $*"
while sleep 2
do
print -n "The time is now "
date
done
)
The top one pipes to somefile..... The bottom one doesn't. The bottom one, the trap works. The top one, the trap doesn't. See if you can get the pipe to work and the "The script was killed at" line to print into the teed out file.
The pipe does work. The trap doesn't, but only when I have the pipe. You can move the trap statement all around and put in layers and layers of sub shells. There's some minor thing I am doing that's wrong, and I have no idea what it is.
Since trap stops the running process – logShell.sh – I think the pipe doesn't get executed at all. You can't do it this way.
One solution could be editing logShell.sh to write line by line in your log file. Maybe you could post it and we can discuss how you manage it.
OK, now I've got it. You have to use tee with -i to ignore interrupt signals.
#! /bin/ksh
dateFormat=$(date +"%Y.%m.%d-%H:%M:%S")
(
trap 'printf "The script was killed at: %s\n", "$(date)"' SIGINT
echo "$0 $*"
while sleep 2
do
print -n "The time is now "
date
done
) | tee -i somefile
this one works fine!

Resources