Sometimes there are two commands which I often invoke in a row. However the second command only makes sense in case the first command was successful.
I wanted to do something like this:
#!/bin/bash
if [ $? -gt 0 ]
then
echo "WARNING: previous command entered at the shell prompt failed"
fi
But it doesn't work:
t@quad:~$ echo "abc" | grep def
t@quad:~$ ./warnme.sh
Last command succeeded
What I'd like is something a bit like this:
t@quad:~$ echo "abc" | grep def
t@quad:~$ echo ${PIPESTATUS[1]}
1
Where we can clearly see that the last command failed.
The result I'd like to have:
t@quad:~$ echo "abc" | grep def
t@quad:~$ ./warnme.sh
WARNING: previous command entered at the shell prompt failed
I can't find a way to do it.