Skip to main content
added 118 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

This seems like it would do what you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

To similarly get the exit status of the previous function ran, see How can I set a trap to print the name and exit status of each function as it's executed.

This seems like it would do what you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

This seems like it would do what you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

To similarly get the exit status of the previous function ran, see How can I set a trap to print the name and exit status of each function as it's executed.

added 5 characters in body
Source Link
Stéphane Chazelas
  • 584.5k
  • 96
  • 1.1k
  • 1.7k

This seems like it would do what you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

This seems like it would do you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

This seems like it would do what you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

deleted 2 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

This seems like thisit would do you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

This seems like this would do you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

This seems like it would do you want and doesn't require you to write/modify/call the functions in any specific way for it to work:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_x() {
  false
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

Note that it's printing the name of the last function that ended, not the last function that started, which I assume is what you want:

$ cat tst.sh
#!/usr/bin/env bash

set -o functrace
trap 'previous_function="$FUNCNAME"' RETURN

function_y() {
  false
}

function_x() {
  function_y
}

if ! function_x ; then
  echo "$previous_function has failed"
else
  echo "call another function here"
fi

$ ./tst.sh
function_x has failed

See https://www.gnu.org/software/bash/manual/bash.html for more information on set -o functrace (or set -T) and RETURN.

added 4 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
Rollback to Revision 4
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 283 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 5 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 65 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
added 125 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60
Loading