32

I would like to return exit code "0" from a failed command. Is there any easier way of doing this, rather than:

function a() {
  ls aaaaa 2>&1;
}

if ! $(a); then
  return 0
else
  return 5
fi
meso_2600
  • 1,940
  • 5
  • 25
  • 50
  • How about `return ! a` – user000001 Mar 21 '16 at 11:58
  • No need for command substitution simple use `if ! a; then ...` – Andreas Louv Mar 21 '16 at 12:13
  • if the `stderr` output is written to `stdout`, in-case of command failures the script will throw a nasty error which can be avoided by silencing the command output and by getting only the command execution status from `$?` – Inian Mar 21 '16 at 12:59
  • If I understand you right, you want to exit status "OK", if the `ls` command fails, and exit status 5 (BTW, why are you writing `return` and not `exit`?), if the `ls` command succeeds. Why, then, do you need to do an `ls`? Wouldn't it be simpler (and clearer to understand) to query the existence of `aaaaa`, i.e. something like `[[ -e aaaaa ]] && echo 'ERROR: aaaaa exists!' && exit 5` ? – user1934428 Mar 21 '16 at 13:06
  • @dev-null have to use cmd subst due to -e and couple other switches – meso_2600 Mar 21 '16 at 15:15

2 Answers2

52

Simply append return 0 to the function to force a function to always exit successful.

function a() {
  ls aaaaa 2>&1
  return 0
}

a
echo $? # prints 0

If you wish to do it inline for any reason you can append || true to the command:

ls aaaaa 2>&1 || true
echo $? # prints 0

If you wish to invert the exit status simple prepend the command with !

! ls aaaaa 2>&1
echo $? # prints 0

! ls /etc/resolv.conf 2>&1
echo $? # prints 1

Also if you state what you are trying to achieve overall we might be able to guide you to better answers.

Michael Daffin
  • 796
  • 1
  • 7
  • 7
  • If the file is not there `ls /etc/resolv.conf 2>&1` will throw an ugly error in the console :) – Inian Mar 21 '16 at 12:49
  • 1
    It will. Output can be suppressed entirely by replacing `2>&1` with `&>/dev/null` or stderr only can be suppressed by replacing it with `2>/dev/null`. – Michael Daffin Mar 21 '16 at 13:27
0

It may be helpful for some people to try timeout command for commands that expect input (like SIGINT = keyboard interrupt) to be stopped like:

timeout 10 kubectl proxy &

This will execute kubectl proxy for 10 seconds (so you can perform the actions you need using the proxy) and then will gracefully terminate kubectl proxy

example:

timeout 3 kubectl proxy &
[1] 759
Starting to serve on 127.0.0.1:8001

echo $?
0

The help of timeout will also help on specific cases

timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

Mandatory arguments to long options are mandatory for short options too.
      --preserve-status
                 exit with the same status as COMMAND, even when the
                   command times out
      --foreground
                 when not running timeout directly from a shell prompt,
                   allow COMMAND to read from the TTY and get TTY signals;
                   in this mode, children of COMMAND will not be timed out
  -k, --kill-after=DURATION
                 also send a KILL signal if COMMAND is still running
                   this long after the initial signal was sent
  -s, --signal=SIGNAL
                 specify the signal to be sent on timeout;
                   SIGNAL may be a name like 'HUP' or a number;
                   see 'kill -l' for a list of signals
      --help     display this help and exit
      --version  output version information and exit