LoginSignup
1
1

More than 5 years have passed since last update.

try..catch via shell

Last updated at Posted at 2016-08-09

set -e & trap

#!/bin/bash
set -e
trap "echo err" ERR

fuga()
{
  echo "fuga"
  return 1
}

hoge()
{
  fuga
  echo "hoge"
  return 1
}

hoge
echo "after hoge"

### output ###
fuga

set -eE & trap

#!/bin/bash
set -eE
trap "echo err" ERR

fuga()
{
  echo "fuga"
  return 1
}

hoge()
{
  fuga
  echo "hoge"
  return 1
}

hoge
echo "after hoge"

### output ###
fuga
err

trap ... untrap ... trap

#!/bin/bash
set -e
trap "echo err" ERR

echo "ok-state"

set +e
trap ERR
find /not_found_file
echo "returned non 0 , but continued"
trap "echo err" ERR
set -e

find /not_found_file

echo "guess un-reachable!"

### output ###
ok-state
find: /not_found_file: No such file or directory
returned non 0 , but continued
find: /not_found_file: No such file or directory
err
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1