-3

i want to write fucntion, that check previous command. Succes or not.

My code:

#!/bin/bash

function check_previous {
        RESULT=$?
        if [ $RESULT -eq 0 ]; then
        echo "success"
        else
        echo "failed"
        fi
}

echo "wwe"
check_previous

ls - l
check_previous

Yes, it works. No problem. But, i have a lot of command, that i should check, one by one. And i have only two messages "success" and "failed". But if i want to add a several messages "directory successfully deleted" or "file was changed". how to do this in one function for different command ? for creating file "file was created", for "deleting", "file was deleted". Any idea ?

Valeriu
  • 1
  • 2

1 Answers1

0

You need to pass a string parameter to your function to describe the previous command status and use that parameter in the echo.

#!/bin/bash

function check_previous {
        RESULT=$?
        if [ $RESULT -eq 0 ]; then
        echo "$1: success"
        else
        echo "$1: failed"
        fi
}

echo "wwe"
check_previous "echo"

ls - l
check_previous "ls"
Khaled
  • 36,533
  • 8
  • 72
  • 99