Solved

Sed exit status

Posted on 2004-08-19
8
5,066 Views
Last Modified: 2013-12-26
HI All,
         this is my code template

                             sed -e s/AAA/aaa/ -e s/BBB/bbb/ -e s/CCC/cccc/ inputfile > outputfile.$$
                              RC=$?
                             if [   $RC -ne 0 ]
                             then
                                    echo "Error with exit staus $RC"
                                   
                             fi

the above shell script will run concurrently at maximum 4.Somtimes the sed returns exit status 2 and the script fails saying the error message : "Error with exit status 2"
It happens sometime and sometimes its not happening.What will be the problem??
What is that exit status 2 means in sed command.
Plz explain me
Or give me some pointers or websites to study the description of the exit status 2 in the sed command.

-Pari.
0
Comment
Question by:pari205
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility

Hi,

try the following from the cmdline:

$ sed 's/aaa/AAA' file.that.does.not.exist
sed: can't read file.that.does.not.exist: No such file or directory
$ echo $?
2
$

So, it looks to be like occasionally the input file does not exist (try a "if [ ! -f inputfile ]; then echo File missing; fi" before the sed command). Perhaps the way the concurrency (not sure what you meant by that..) is being done some files are being listed for processing more than once, or something like that...

Cheers,
C
0
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
In errno.h, error 2 is:

#define ENOENT          2               /* No such file or directory */

So, apparently sed does not find the inputfile for some reason.

0
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
brett, could it be that one process opens the file for reading and it is momentarily locked to other processes? I really don't know enough about locking...

The other issue I can think of is that programs may not use errno.h, so the return codes would be whatever the programmer decided they should be...Is there a way of working out what standard include files were used?

pari205, what Unix (& version) do you use? It may be important to enable us work out where the problem lies...or at least get a definitive answer to what sed return code 2 means
0
 
LVL 23

Expert Comment

by:brettmjohnson
Comment Utility
> brett, could it be that one process opens the file for reading and it is momentarily
> locked to other processes?  I really don't know enough about locking...

Possibly, although I would expect an error of 13 (EACCES).


> The other issue I can think of is that programs may not use errno.h, so the return codes
> would be whatever the programmer decided they should be...Is there a way of working out
> what standard include files were used?

The common unix utilities usually return error codes from errno.h for consistency.
If you are using Linux, the full source to the utilities that shipped with your distribution
should be available.  But is is not possible to determine from a shipped (stripped) executable
which header files were included during compilation.


pari205, what Unix (& version) do you use? It may be important to enable us work out where the problem lies...or at least get a definitive answer to what sed return code 2 means
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:pari205
Comment Utility
But i am sure that inputfile always will be there.

But as  tfewste said "could it be that one process opens the file for reading and it is momentarily locked to other processes? I really don't know enough about locking..."

it may be the problem.But why its not showing any error like file is not ready or No such file or directory.How can i solve this problem.?Do i need to put lock on the input file.And by checking lock file,
do i have to proceed sed command.??Can u plz give me the solution.

Is there any material to study abt this kind of locking in UNIX os.

My UNix flavour is AIX Version 5

-pari


0
 
LVL 20

Expert Comment

by:tfewster
Comment Utility
From this and your other question, I suspect that somewhere in your script the error messages are being lost; Try

sed -e s/AAA/aaa/ -e s/BBB/bbb/ -e s/CCC/cccc/ inputfile > outputfile.$$  2> errors.$$            (or 2>> all_errors.log )
0
 
LVL 5

Expert Comment

by:ITcrow
Comment Utility

You have two problems in your code:
1. $$ doesn't always guarantee a unique filename.
2. You did not mention, how you are launching simultaneous processes.

In #2 if you are using some loop and launching it from a script, then you need to know
what's the limit on number of handles used by a process.

Check using unix commands limit / ulimit.

You can confirm the problem, by printing value of $$ and iteration of your loop.

The reason you don't always see the problem is bot #1 & #2.

Handles / Descriptors are the number of simultaneos files / process a process can launch.
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 125 total points
Comment Utility
The sed exit code isn't documented in the info or the man page.  I looked in the gnu sed-4.0.9 source.  sed returns an error code of 2 whenever it fails to open one of the files given it on the command line.  Gnu sed also has a 'Q' command which enables the script to return an arbitrary error code, and it is possible that the script contains a "Q 2" command.  The error code is hardcoded as 2, and the errno for failing is never used to determine the exit code, so it has no direct relationship to ENOENT.  

Sed detects failure to open the file when an fopen() call returns NULL.  fopen() fails when open() fails.  See the open() man page in section 2 to get a list of all possible causes of failure to open a file.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 105
notReplace  challenge 53 102
difference between String.subString() and String.subSequence() 6 133
Problem to event 3 48
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now