Home > Software design >  How can i apply 'set -e' for child process?
How can i apply 'set -e' for child process?

Time:09-16

I use bash shell script in my project.

Main script call many sub scripts, but sometimes sub script has error. I want script stop immediately.

I found -errexit option. It will works for me.

There are constraints. I can't edit sub scripts. plus, i don't want to touch exec line.

Lower is example.

#!/bin/bash

# do something

exec bash test.sh

test.sh

#!/bin/bash

cat as.txt  # error
cat ab.txt  # run

I want cat ab.txt not be run.

I know, exex bash -e test.sh will run. but real project code is this, touch exec line is dangerous.

exec "$@"

I can't expect what is $@.

If i write set -e in 'do something', it's not apply in child process(test.sh).

I can change default option script, like ~/.bashrc ? How can i apply shell option for child process?

Please help me!

CodePudding user response:

I've used a similar example and the script with the do something exited right away after the cat command didn't succeed.

doSome.sh

#!/bin/bash

exec bash test.sh

test.sh

#!/bin/bash

cat text.txt
touch newfile

output:

command output

maybe you don't need to use set -e?

EDIT:

the file was created:

$ ll newfile
-rw-r--r-- 1 0000 0000 0 Sep 14 13:57 newfile

and when replacing touch newfile with a cat operation it does keep going and the issue persists.

a working solution might be adding error handling on a per-line basis:

test.sh

#!/bin/bash

cat text.txt || exit 1
cat nofile.txt || exit 1

CodePudding user response:

This is solution.

export SHELLOPTS

This answer help me.

https://unix.stackexchange.com/questions/387186/are-all-the-shell-options-not-inherited-by-scripts

  • Related