LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-13-2010, 07:06 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Testing the successful execution of for loop


Hi All,

I want to check weather a for loop has executed properly
Code:
for name in `awk -F: '{printf " "$1}' /etc/passwd`;do
echo $name;done
[ $? -eq 0 ] && echo "SUCCESS" || echo "Failed_to_fetch_the_user";
Every time this gives output as SUCESS even If I change /etc/passwd to other file name.

How can I resolve this ?
 
Old 07-13-2010, 07:52 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
The exit status of a loop is equal to the exit status of the last command executed inside it. Since the last command in the above is an echo that always works, whether there's any value in the variable or not, it will always return 0.

You should probably use some other method of determining whether your list is successful or not, like checking whether /etc/passwd is readable. Personally, I'd probably direct the awk output into a variable instead, which you could then test the contents of and print out as necessary.

PS: $(..) is generally preferred over `..`

Last edited by David the H.; 07-13-2010 at 07:57 AM.
 
Old 07-13-2010, 07:58 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by David the H. View Post
The exit status of a loop is equal to the exit status of the last command executed inside it. Since the last command in the above is an echo that always works, whether there's any value in the variable or not, it will always return 0.

You should probably use some other method of determining whether your list is successful or not, like checking whether /etc/passwd is readable. Personally, I'd probably put the awk output into a variable instead, which you could then test the contents of and print out as necessary.
Hi David,

Thanks for your reply.

Here echo statement will not get executed. I have tested it in debug mode.

Code:
[root@localhost test]# sh -x fetch.sh 
++ awk -F: '{printf " "$1}' /etc/passwdfdsa
awk: cmd. line:1: fatal: cannot open file `/etc/passwdfdsa' for reading (No such file or directory)
+ '[' 0 -eq 0 ']'
+ echo RC_SUCCESS_RC
RC_SUCCESS_RC
In the above output there is no line to indicate echo $name execution to set $? to zero. Still it is getting set to 0.

Please advice..
 
Old 07-13-2010, 08:56 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
A-ha. I got it. And I learned something I didn't know before.

http://www.gnu.org/software/bash/man...ing-Constructs

Quote:
The return status is the exit status of the last command that executes. If there are no items in the expansion of words, no commands are executed, and the return status is zero.
This is true of all loop constructs; if the internal statements don't execute, the loop exits with a 0 status.

So you'll still have to find some other way to test the results you want.
 
Old 07-14-2010, 03:01 AM   #5
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
Using $? would be difficult in this case as pointed out by David.
You can set the value of name to some default value before the loop and then once the loop is over compare the default value with the current value of name,if the value has not been modified then the loop has failed else its a success.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
Testing parallel port loop device freeindy Linux - Hardware 2 01-21-2008 01:49 AM
temporily suspending the execution of loop to allow other method to run linuxmandrake Programming 1 04-16-2006 07:56 PM
loop execution suchi_s Programming 4 11-05-2004 08:06 AM


All times are GMT -5. The time now is 07:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration