Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if command succeeded makes shell script very long [duplicate]

Tags:

bash

shell

I am writing a shell installation script
After each command I need to check if the command is successful or not - And I must notify the user what failed.
If something fails - installation cannot continue, currently after each command I am adding

if [ $? -eq 0 ]; then  

But this adds like 6 lines to every command of the shell script
Is there a way to make this checks shorter?

sample:

do some command
if [ $? -eq 0 ]; then
    echo notify user OK
else
    echo notify user FAIL
    return -1
fi
do some command
if [ $? -eq 0 ]; then
    echo notify user OK
else
    echo notify user FAIL
    return -1
fi
like image 974
JavaSheriff Avatar asked Jan 04 '18 16:01

JavaSheriff


People also ask

How do you check if a shell script is executed successfully?

Using Special Variable $? The shell treats several parameters as special variables. The $? special variable expands to the exit status of the last executed command. By comparing that value with the expected one, we can check whether the command has been executed successfully or not.

Which command can be used to check if the last executed command was successful or failed?

“echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred.

What echo $? Does?

echo $? returns the return value (exit status) of the last executed command (0 is usually success ).


1 Answers

First, the idiomatic way to check if a command worked is directly in the if statement.

if command; then
    echo notify user OK >&2
else
    echo notify user FAIL >&2
    return -1
fi

(Good practice: Use of >&2 to send the messages to stderr.)

There are several ways you could simplify this.

Write a function

Just like in other programming languages, common logic can be moved into shared functions.

check() {
    local command=("$@")

    if "${command[@]}"; then
        echo notify user OK >&2
    else
        echo notify user FAIL >&2
        exit 1
    fi
}

check command1
check command2
check command3

Don't print anything

In idiomatic shell scripts, successful commands don't print anything. Printing nothing means success in UNIX. Also, any well-behaved command that fails will already print an error message, so you don't need to add one.

Taking advantage of those two facts, you could use || exit to exit whenever a command fails. You can read || as "or else".

command1 || exit
command2 || exit
command3 || exit

Use -e

Alternatively, you could enable the -e shell flag to have the shell exit whenever a command fails. Then you don't need anything at all.

#!/bin/bash -e

command1
command2
command3

Don't print anything

If you do in fact want error messages, but are okay with no success messages, a die() function is popular.

die() {
    local message=$1

    echo "$message" >&2
    exit 1
}

command1 || die 'command1 failed'
command2 || die 'command2 failed'
command3 || die 'command3 failed'
like image 75
John Kugelman Avatar answered Oct 31 '22 16:10

John Kugelman