0

I have a unix script in which I am calling functions. I want the function should return immediately if any of the command failed in between.

But checking $? after every command I can not do. Is there any other way to do this.

1
  • Are you looking for the set -e? Commented Jun 16, 2015 at 8:32

2 Answers 2

1

Maybe running the script from a file line by line (as long of course as each of your functions are one line long).

Maybe the following script can be a starting point:

#!/bin/sh

while read l
do
  eval "$l || break"
done <<EOF
  echo test | grep e
  echo test2 | grep r
  echo test3 grep 3
EOF
Sign up to request clarification or add additional context in comments.

Comments

0

This is another idea after my previous answer. It works with bash script and requires your functions to be quite simple (pipes may cause some issues):

#!/bin/bash

set -o monitor

check() {
  [ $? -eq 0 ] && exit
}

trap check SIGCHLD

/bin/echo $(( 1+1 ))
/bin/echo $(( 1/0 ))
/bin/echo $(( 2+2 ))

Furthermore: functions need to be external command (this is why I use /bin/echo rather than echo). Regards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.