LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2020, 02:45 PM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Rep: Reputation: 15
Help with bash for loop


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.

Code:
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

Last edited by woodson2; 10-19-2020 at 03:21 PM. Reason: Add code tags
 
Old 10-19-2020, 04:24 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
Code:
if [ $? -eq 0 ]
That will always be 0
Code:
echo $USER
me
echo $?
0

echo $UUSERRRR
echo $?
0
Are you testing for kinit or $USER
Code:
a="fred"
b=""

if [ -z "$a" ]; then
    echo "a is empty"
else
    echo "a is "$a""
fi
a is fred

if [ -z "$b" ]; then
    echo "b is empty"
else
    echo "b is "$b""
fi
b is empty

[[ ! -z "$a" ]] && echo "Not empty" || echo "Empty"
Not empty
[[ ! -z "$b" ]] && echo "Not empty" || echo "Empty"
Empty
 
1 members found this post helpful.
Old 10-19-2020, 04:28 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
You do not have any test to exit after three times around the loop, instead you have a loop which runs three times - not the same thing.

Instead of setting the loop to run three times, I would write an endless loop which breaks on the success condition (exit status zero), and exits after three unsuccessful trips around the loop. To do that we need a loop counter and two tests.

Here is one way to do it:

Code:
loop_count=0
while [ 1 -gt 0 ] ; do
    ((loop_count++))
    echo "Verifying..."
    kinit $USER
    #Break loop on success
    if [ $? -eq 0 ]; then
        break;
    fi
    #Exit with error after third fail
    if [ $loop_count -ge 3 ]; then
        exit 1;
    fi
    echo "Please try again..."
    sleep 1;
done

#Arrive here only on success within three tries
echo "Continuing..."

Last edited by astrogeek; 10-19-2020 at 04:39 PM.
 
2 members found this post helpful.
Old 10-19-2020, 04:36 PM   #4
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by astrogeek View Post
You do not have any test to exit after three times around the loop, instead you have a loop which runs three times - not the same thing.

Instead of setting the loop to run three times, I would write an endless loop which breaks on the success condition (exit status zero), and exits after three unsuccessful trips around the loop. To do that we need a loop counter and two tests.

Here is one way to do it:

Code:
loop_count=0
while [ 1 -gt 0 ] ; do
    ((loop_count++))
    echo "Verifying..."
    kinit $USER
    #Break loop on success
    if [ $? -eq 0 ]; then
        break;
    fi
    #Exit with error after third fail
    if [ $loop_count -ge 3 ]; then
        exit 1;
    fi
    sleep 1;
done

#Arrive here only on success within three tries
echo "Continuing..."

This is precisely what I needed. Thank you.
 
Old 10-19-2020, 05:05 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
You are welcome, glad it helped!

If the problem is solved, please mark the thread SOLVED using the Thread Tools option at top of first post.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration