Linux shell | how to exit a script if any command fails.

Hi,
i am new here let me say HI for all.
now i have a question please:

i am sending one command to my machine to create 3 names.
if one of the names exists then the box return error message that already have the name but will continue to create the rests.
How i can break the command and send the error message to a file and not let the command continue creating the rest names?

Thanks,
Amir

Hi,

if it is inside a loop, you can leave it with the break command. You can also just exit the whole script, if there is nothing anymore to do.

1 Like

Thanks but i dont know how to tell the command to break if we have error message. for example:

for i in {1..10}; do dirshell -c "address.create name=$i"; sleep 1 ; done

first execute will be ok but the if i execute it again the command return error message that name=1 is exists and so on.
i want to exist the command from the first error and write it to a file. is that possible?

thanks

I assume this is what you are looking for some sort of condition.
Example:-

.....
if [ -e "full/path/to/$1" ]
then
        : # This is a NOP but it could be your error report directed to file...
else
        .....your code.....
fi

You could also check if the command gives a return code with the value stored in the variable $? after executing it. Maybe dirshell can pass it through from the command it executed. Maybe in the man page for it there is some info.
If this is not possible, you could also try parsing the text output of the command with a test or grep , if there was an error or not.

I think you are tackling this from the wrong angle. The first step is to identify what you really want to do:

You might want to change that first. Instead of

command "user1" "user2" "user3"

you might want to first create a process which creates ONE user and then call that 3 times. Your "send a command to create 3 names" will become "send a command to create 1 name three times":

for USER in user1 user2 user3 ; do
     command $USER
done

This has two advantages: first, "command" can be any arbitrarily complex code, like a shell function. Into this you could pack everything you want to do whenever one such "command" is executed. For instance:

command ()
{
local user="$1"

do_something "$user"

return $?
}

# ----- main() part
for USER in user1 user2 user3 ; do
     command "$USER"
done

If you want to stop execution now it would be simple to add that - inside the command()-function:

command ()
{
local user="$1"

if ! do_something "$user" ; then
     echo "do_something returned ${?}, aborting."
     exit 1
fi

return 0
}

Notice that the main-part of the script stays the same, you have encapsulated the different parts.

Note: You might wonder about this:

if ! do_something "$user" ; then

This is in fact just the short form of

do_something "$USER"
if [ $? -ne 0 ] ; then

I hope this helps.

bakunin

1 Like

On top of what already has been said, please tell us if your operation should be "atomic" or not, i.e. if any name creation fails, should then all 3 names be discarded? Or would it be sort of "position dependent", like: if name1 fails, don't create any further name, but if name3 fails, leave name1 and name2 intact?

1 Like

Thanks all for your answers.