LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-03-2010, 06:16 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Error codes in a for loop


I've got a 'nested' for loop which has a grep in it, if the grep fails there's no output - however the error code is still $0 and the second for loop is still entered, there's also a grep in the second for loop.

I guess ultimately what i need to know is whether there's a way of making grep generate an error code. when no results are found?
 
Old 11-03-2010, 06:25 AM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
You'll probably have to show a script segment. If you just do a grep on the command line and then immediately echo $?, you will see that it returns a code. You need to capture that code in your script before anything else is done that changes $?.
 
Old 11-03-2010, 06:26 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

from the manpage:
Quote:
EXIT STATUS
Normally, the exit status is 0 if selected lines are found and 1
otherwise
. But the exit status is 2 if an error occurred, unless the
-q or --quiet or --silent option is used and a selected line is found.
Note, however, that POSIX only mandates, for programs such as grep,
cmp, and diff, that the exit status in case of error be greater than 1;
it is therefore advisable, for the sake of portability, to use logic
that tests for this general condition instead of strict equality
with 2.
 
1 members found this post helpful.
Old 11-03-2010, 06:31 AM   #4
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Yeah - that does seem accurate actually (works on my tests). I have two subsequent greps though, perhaps this is effecting the code :S

Code:
xe vm-list | grep -A1 NOTREAL | grep power | awk '{print $5}'
[root@MYSERVER ]# echo $?
0
xe vm-list | grep -A1 REAL | grep uuid | awk '{print $5}'
offline
[root@MYSERVER ]# echo $?
0
 
Old 11-03-2010, 06:35 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by genderbender View Post
Yeah - that does seem accurate actually (works on my tests). I have two subsequent greps though, perhaps this is effecting the code :S

Code:
xe vm-list | grep -A1 NOTREAL | grep power | awk '{print $5}'
[root@MYSERVER ]# echo $?
0
xe vm-list | grep -A1 REAL | grep uuid | awk '{print $5}'
offline
[root@MYSERVER ]# echo $?
0
Actually, it is the return code of the last awk command that returns 0.
 
1 members found this post helpful.
Old 11-03-2010, 06:36 AM   #6
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Oh, suddenly it's clicking! Thanks Will google a bit before I ask my next obvious question :S
 
Old 11-03-2010, 06:42 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Try also including PIPESTATUS in your google search.
 
Old 11-03-2010, 09:36 AM   #8
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
OK, managed to handle this with perl - however it doesn't work with my for loops now
 
Old 11-03-2010, 06:38 PM   #9
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Gonna need some help with this, will post 'similar' code tomorrow but basically my oneliner enters a for loop which grabs a uuid (have some perl to echo a none zero exit code if theres no match) this uuid is then used in another for loop (again using perl for the none zero exit code thing) to list virtual server snapshots (specificall the snapshot uuid). This then reverts to a specified snapshot - essentially it's a one liner to rewind xen snapshots, but if insert incorrect info in either of my for loops the exit code is always zero - if I run the code outside the for loop the exit code is always nonezero. Hard to explain without an example... This is psuedocode, expect something which actually works tomorrow at some point:

Code:
for uuid in `list virtual servers | grep 'correct-server' | perl -alne '{ print $F[4] } END { exit($.==0) }'`;
	do
		for snapshotuuid in `list virtual snapshot | grep $uuid | grep snaoshotname | perl -alne '{ print $F[4] } END { exit($.==0) }'`;
		do
			revert $uuid to $snapshotuuid;
		done;
	done;
 
Old 11-04-2010, 05:09 AM   #10
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Code:
for UID in `xe vm-list | grep -B1 VMNAME | grep uuid | perl -alne '{ print $F[4] } END { exit($.==0) }'`; do for SNAP in `xe snapshot-list snapshot-of=$UID | grep -B1 SNAPSHOTNAME | grep uuid | perl -alne '{ print $F[4] } END { exit($.==0) }'`; do xe snapshot-revert uuid=$SNAP; done; done;
VMNAME and SNAPSHOTNAME are legitimate values. With the correct values my exit status is 0, with incorrect values - my exit status is still 0... I'm happy for people to find alternative methods of achieving the same thing, but it really needs to be as short as possible.
 
Old 11-04-2010, 06:29 AM   #11
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by genderbender View Post
Code:
xe vm-list | grep -A1 NOTREAL | grep power | awk '{print $5}'
In any sequence like that, the exit status after the sequence is going to be that of the last command in the sequence. No way around that. So, you have to break it up if you want an intermediate exit status.

Something like:
Code:
if xe vm-list | grep -A1 NOTREAL > scratch; then
  cat scratch | grep power | awk '{print $5}';
else
  ...
fi
The exit status of the `grep -A1 NOTREAL` controls the if, and it's output goes to the file scratch. Then in the next line you can continue with `cat scratch` piped to whatever else you wanted to do.
 
Old 11-04-2010, 06:32 AM   #12
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by choogendyk View Post
In any sequence like that, the exit status after the sequence is going to be that of the last command in the sequence. No way around that. So, you have to break it up if you want an intermediate exit status.

Something like:
Code:
if xe vm-list | grep -A1 NOTREAL > scratch; then
  cat scratch | grep power | awk '{print $5}';
else
  ...
fi
The exit status of the `grep -A1 NOTREAL` controls the if, and it's output goes to the file scratch. Then in the next line you can continue with `cat scratch` piped to whatever else you wanted to do.
I solved this with my perl code above. I can do it without 'breaking it up'. The perl doesn't work with my loop though (each individual line does, but just not the loop itself).

Last edited by genderbender; 11-04-2010 at 06:35 AM.
 
Old 11-04-2010, 09:05 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
crts gave good advice in post 7
 
Old 11-05-2010, 09:09 AM   #14
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by catkin View Post
crts gave good advice in post 7
pipestatus was always 0

The issue is the loop, the loop continues to function if the exit status is 1

Code:
[root@localhost ~]# xe vm-list | grep -B1 WRONGVMNAME | grep uuid | perl -alne '{ print $F[4] } END { exit($.==0) }'
[root@localhost ~]# echo $?
1
[root@localhost ~]# xe snapshot-list snapshot-of=1234:1234:1234:1234 | grep -B1 WRONGNAME | grep uuid | perl -alne '{ print $F[4] } END { exit($.==0) }'
[root@localhost ~]# echo $?
1

Last edited by genderbender; 11-05-2010 at 02:18 PM.
 
Old 11-05-2010, 12:56 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Code:
$ help for
for: for NAME [in WORDS ... ] ; do COMMANDS; done
...
    Exit Status:
    Returns the status of the last command executed.
From a few tests I ran, it seems that "last command" refers only to commands in the body of the loop (i.e. COMMANDS). I think the easiest way to solve this problem is to use a flag variable:
Code:
status=1
for uuid in ...
	do
		for snapshotuuid in ...
		do
			revert $uuid to $snapshotuuid && status=0
		done;
	done;

exit $status
 
1 members found this post helpful.
  


Reply

Tags
grep forloop



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
[SOLVED] How to compile .jar Java byte-codes into native machine codes? ilgaar Linux - Software 1 08-30-2010 12:52 AM
How to compile .jar Java byte-codes into native machine codes? ilgaar Linux - Software 6 08-28-2010 11:20 PM
httpd error codes and custom error messages ninja master Linux - Software 1 01-02-2010 03:56 PM
Error codes ReV0h Linux - Newbie 2 03-14-2006 04:03 PM
Are the hex codes for colors in a jpg the same codes as used in html? abefroman Linux - Security 3 07-31-2005 03:21 PM

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

All times are GMT -5. The time now is 05:01 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