-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
  • 825
  • 4
  • 16
  • 29
  • 3
    Can you define `this was not successful`? Regarding `find` or `sed`? – fedorqui Sep 11 '13 at 11:18
  • 1
    Can you tell us what could be not successful particularly? – konsolebox Sep 11 '13 at 11:18
  • You appear to be using the `PATH` variable to store a file name. Don't; it's a built-in shell parameter that stores a list of directories for command lookup. Use a different variable name. – chepner Sep 11 '13 at 13:30

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
  • 72,135
  • 12
  • 99
  • 105