-1

Bash file is as

#!/bin/bash



SomeErrorHandler () {
    (( errcount += $? ))

 echo -e $errcount


}

trap SomeErrorHandler ERR


uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null

uname -a 1>/dev/null

declare -a f
         # this is never printed

f="$(SomeErrorHandler)"

echo -e "$f" | awk 'END {print}'

I want the returning value be from the value of a as

1
2
3
4
5

to return the last match which is 5 instead the output just echos everything in a.

I also tried with sed but output is same. It seems that this is just one record

lazereyes
  • 107
  • 6
  • I doubt if it's your problem (I can't figure out what it is you're really trying to do) but `awk 'END {print}'` is undefined behavior per POSIX. It'll either print the value of the last line read or print a null string or do something/anything else. Use `awk '{rec=$0} END{print rec}'` for portability if you're trying to use awk to mimic `tail -1`. – Ed Morton Apr 22 '19 at 20:49

1 Answers1

0

Do you think that all the echoes from SomeErrorHandler() are stored in variable f?
This is not happening.

SomeErrorHandler () {
   if (( $# > 0 )); then
      echo -e $errcount
   else
      (( errcount += $? ))
   fi
}

trap SomeErrorHandler ERR
errcount=0

uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null
uname +a 2>/dev/null

uname -a 1>/dev/null

# Use a random parameter for displaying the current value
SomeErrorHandler show
Walter A
  • 19,067
  • 2
  • 23
  • 43