1

I have the following:

test.sh

. Foo.sh

Foo.bar
Foo.baz
Foo.blah

and

Foo.sh

function Foo.bar() {
    echo 'I am a bar!'
}

function Foo.baz() {
    echo 'But, I am a baz!'
}

function Foo.error() {
    # I should suppress the 'command not found' error based on the pattern '^Foo\.([^:]+):'
    # If the pattern is matched, I'll need to perform some action based on the captured pattern.
}
trap Foo.error ERR

This is what I get when I run test.sh:

$ ./tesh.sh
I am a bar!
But, I am a baz!
./test.sh: line 5: Foo.blah: command not found

I would like to:

  1. suppress the command not found error.
  2. pass the Foo.blah: command not found text to Foo.error() to perform some action based on the error.

I suppose I could redirect errors to /dev/null like so:

./test.sh 2> /dev/null

But this is not as elegant as I'd like it to be. In any case, it still doesn't give my error() function access to the error text.

Housni
  • 111
  • 1
  • 6

1 Answers1

1

Why don't you just use test to check if the command exists and it is executable? Like this:

if [[ -x /my/executable/file ]]; then
    echo "Oh, there we go!";
else
    echo "Oh damn! =(";
fi;

Cya!

Stefano Martins
  • 1,221
  • 8
  • 10
  • Because the contents of `test.sh` would be written by someone else who would only know the very basic commands. My goal is to grab the error message and determine what to do based on the error message. Thanks for the suggestion though. – Housni Oct 22 '15 at 21:39