我有以下几点:
test.sh
. Foo.sh Foo.bar Foo.baz Foo.blah
和
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
这是我运行test.sh时得到的结果:
$ ./tesh.sh I am a bar! But, I am a baz! ./test.sh: line 5: Foo.blah: command not found
我想要:
command not found错误。 Foo.blah: command not found文本传递给Foo.error()以根据错误执行一些操作。 我想我可以redirect错误到/ dev / null像这样:
./test.sh 2> /dev/null
但是这并不像我想要的那么优雅。 在任何情况下,它仍然不会给我的错误()函数访问错误文本。
为什么不使用test来检查命令是否存在并且是可执行的? 喜欢这个:
if [[ -x /my/executable/file ]]; then echo "Oh, there we go!"; else echo "Oh damn! =("; fi;
氰!