Help with bash shell script

woodson2

New Member
Credits
19
The code below works just fine as long as the exit status equals 0, however I'd like the script to exit after 3 times if the exit status is non-zero. Right now the script continues on even if the exit status is non-zero after the 3 iterations.

for run in {1..3}
do
kinit $USER
if [ $? -eq 0 ]; then
echo ""
echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
echo ""
sleep .5
break
else
sleep 1
echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
fi
done
 


JasKinasis

Well-Known Member
Credits
4,005
One way would be to put the code into a function, call it and then use its return value to determine the next course of action:
Bash:
function dosomething()
{
for run in {1..3}
do
    if kinit "$USER"; then
        echo ""
        echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
        echo ""
        sleep .5
        return 0
    else
        sleep 1
        echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
    fi
done

# if we get here - the function failed three times
return 1
}

if dosomething; then
    echo "Function succeeded!!"
   # do whatever else you need to do here
    exit 0
else
    echo "Function failed!!"
    # do whatever you need to do here
    exit 1
fi
NOTE:
Instead of calling the kinit command and check it's return value using $? - a better way to do it is just to check the return status directly in the if statement.
e.g.
Bash:
if kinit "$USER"; then
The loop inside the function will run a maximum of three times.
If kinit returns 0 (success) - the function will return 0
If kinit returns anything else (failure) - the function will loop again.
Once the loop has done three iterations, the function will return 1 (failure)

Then in the main body of the script we use:
Bash:
if dosomething; then
to run the function and check it's return status/value and act appropriately.

That's one way of dealing with it. But there are probably several other approaches that would work just as well!

Also - I don't know exactly what you're doing here - so I just called the function dosomething - you might want to give it a more descriptive name!
 
Last edited:

woodson2

New Member
Credits
19
One way would be to put the code into a function, call it and then use its return value to determine the next course of action:
Bash:
function dosomething()
{
for run in {1..3}
do
    if kinit "$USER"; then
    echo ""
    echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
    echo ""
    sleep .5
    return 0
else
    sleep 1
    echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
fi
done

# if we get here - the function failed three times
return 1
}

if dosomething; then
    echo "Function succeeded!!"
   # do whatever else you need to do here
    exit 0
else
    echo "Function failed!!"
    # do whatever you need to do here
    exit 1
fi
NOTE:
Instead of calling the kinit command and check it's return value using $? - a better way to do it is just to check the return status directly in the if statement.
e.g.
Bash:
if kinit "$USER"; then
The loop inside the function will run a maximum of three times.
If kinit returns 0 (success) - the function will return 0
If kinit returns anything else (failure) - the function will loop again.
Once the loop has done three iterations, the function will return 1 (failure)

Then in the main body of the script we use:
Bash:
if something; then
to run the function and check it's return status/value and act appropriately.

That's one way of dealing with it. But there are probably several other approaches that would work just as well!

Also - I don't know exactly what you're doing here - so I just called the function dosomething - you might want to give it a more descriptive name!

This is perfect and thank you for the detailed explanation of the script.
 

JasKinasis

Well-Known Member
Credits
4,005
Another way of doing it - without using a function would be to use a while loop and a counter.
e.g.
Bash:
((count=0))
while [[ $count -lt 3 ]]; do
    if kinit "$USER"; then
        echo
        echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
        echo
        sleep .5
        break;
    else
        sleep 1
        echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
        count=$((count + 1))
    fi      
done

if [[ $count -eq 3 ]] ; then
    echo "Failed three times!!"
    # do whatever you need to do in this instance
    exit 1 # I'm guessing?!
fi

# if we got here one of the calls to kinit worked!
# do whatever you have to do next
# ...
exit 0
 

JasKinasis

Well-Known Member
Credits
4,005
Although - that's overcomplicating things..... Another way would simply be to check the value of run in the for loop!
I should have noticed that straight away! What an idiot I am?!
This is closer to your original code!
Bash:
for run in {1..3}
do
    if kinit "$USER"; then
        echo ""
        echo -e "${bldgrn}Good. Now lets us continue${txtrst}"
        echo ""
        sleep .5
        break
    else
        sleep 1
        echo -e "${bldred}Please verify your credentials and try again!${txtrst}"
        if [[ $run -eq 3 ]]; then
            echo "Failed three times!"
            # do whatever you need to do in this case
            exit 1
        fi
    fi
done

# If we got here, one of the calls to kinit succeeded
# Rest of script here

exit 0
I feel pretty stupid not spotting that straight away! Ha ha ha!
 
Last edited:


Members online


Top