-3

I'm not a master in Scripting, so I have to ask here :)

How can I implement an errorhandling for this:

find . -type f|sed 's_\.\/__' > $PATH

i.e. abort when this was not successfull.

Thank you very much in advance Thomas

user2428207
  • 777
  • 3
  • 15
  • 25

2 Answers2

1

try to use conditions like && or ||

find . -type f|sed 's_./__' > $PATH && if_true_do_something || if_false_do_something

0

First thing is your command would fail at least most certainly since $PATH is an invalid single path.

find . -type f | sed 's_\.\/__' > $PATH

If you want to check if both commands worked well, use the pipefail option which would make a pipe call return a nonzero status if any of the commands along the line return one, not just the last one.

set -o pipefail  ## You have this only once in the script.

find . -type f | sed 's_\.\/__'  > $PATH || {
    echo "Failed."
    exit 1  ## This probably is what you mean to abort.
}

# Shell script continues here... 

An alternative:

...
find . -type f | sed 's_\.\/__' > $PATH
if [[ $? -ne 0 ]]; then
    echo "Failed."
    exit 1
fi

Example:

> set +o pipefail  ## Disable pipefail (default).
> true | false
> echo $?
1
> false | true
> echo $?
0

> set -o pipefail  ## Enable pipefail.
> true | false
> echo $?
1
> false | true
> echo $?
1

Notice that the last command returned 1 even if false was not at the end. While when pipefail was disabled, the pipe command would still return 0 even though one of the commands (false) returned 1.

konsolebox
  • 60,986
  • 9
  • 83
  • 94