[solved] awk: return code from system() always 0 ??

Hi all,

I currently have the following problem:
In an awk script, I am calling a predifend function from the END{} and handing over a command string. This string arrives flawless and is executed like this:

                function send_msg( cmd_str )
                {
                        debug_send_msg=sprintf("SENDING: %s", cmd_str)
                        debug( debug_send_msg )

                        rc=system( cmd_str )
                        print "--> RETURN CODE:", rc
                }

The command string is calling a Perl script with plenty parameters. It gets executed, but the return code is always 0.

When I change the system() against a failing ls llllll for example, it works and I get the correct return code.

When I run a one-line on the command line with the same call of the Perl script inside awk like in the code above, I get the correct return code.

Somewhere there the return code gets lost. I also tried adding ; echo $? to system(), but it is 0 too.

Thanks in forward for any explanation and hint!

I might be wrong here..

system returns the exit status of the command thats executed.. since echo $? was the last command and exit staus was 0..

However it should have returned the exit status from script too not sure why its not the case

I hope you are trying something similar below?

 
 
vidya> cat vv
date
exit 120
vidya> awk 'BEGIN{a="vv";v=system(a" ; echo $?");print v}'
Thu Jul 11 10:51:14 CEST 2013
120
0

Have you tired explicitly returning the exit code??

return $?

or

return $rc

Not sure why your code doesnt work, i tried something almost similar and it returned the exit code from the perl script.. What is your OS and awk version ?

@vidyadhar85
Sorry I altered my code to avoid confusion. I removed the $? - that was just there to verify if at this point there is any RC other than 0. You saw the code before I altered it. Forget the $? :wink:

@n70arun
Thanks for the input, but the exit code of the command being called by system() will be assigned to the variable rc. And since I print this, it is displayed from inside the function, but not with the expected return code. The Perl script produces a 16 and not a 0, which I have tested, as said. Though I have a look at it again.

@rajamadhavan
RHEL 5.9
gawk-3.1.5-16.el5

Update:
I simplified what I am doing in an extra script and it works. No clue why it doesn't work in my other script - though I will give another update when I found it.

Here is my test script:

$ cat test.sh
awk '
        function mach( cmd_str ){
                rc=system( cmd_str )
                print "RC:", rc
        }

        END{
                cmd_str="/path/to/some.pl arg1 arg2 arg3"
                mach( cmd_str )
}' infile
$ ./test.sh
Can't connect to somehost; Timeout!
RC: 16

So far so good.
Now from my problematic script:

...
                function send_msg( cmd_str )
                {
                        #cmd_str="ls laaa"
                        rc=system( cmd_str )
                        print "RC:", rc
                }

...
END{
...
                cmd_str="/path/to/some.pl arg1 arg2 arg3"
                send_msg( cmd_str )
...
}' infile
$ ./script.sh
### tons of output...
Can't connect to somehost; Timeout!
RC: 0

When I enable the line with the "ls laaa", which overwrites cmd_str, I get the correct RC of 2, which is for "file not found"...

I am confused :confused:

The cmd_str with the Perl script is exactly the same as in test.sh...

Update:
It seems the cmd_str is a problem. It is a very long list of parameters and contains blanks, semicolons, double quotes. Somewhere there must be a problem, because when I issue the Perl script without parameters it returns a RC of 1 which is also displayed when I alter it in the script.
I have to check how I can encapsule that long string so that it will be handed over correctly and if the correct RC will be displayed.

Update:
I found out, that when there is a semicolon in string variable, that is handed over to system(), it will give the RC = 0, because the parameters delimeted by the semicolon are things like "msg=blabla" and this is a valid variable definiton in the shell, so it's RC = 0 :rolleyes:
test.sh worked, because it had a shortened version of the command, where the semicolons were left out... sorry.

I tried escaping it, but that doesn't help - any ideas are welcome.
I can't change the delimeter to something else, because the server on the other side expects them as delimeter.

To simplify my blathering it comes down to this:

Works:

$ echo "test1; test2"

Does not work:

$ awk 'END{system("echo test1; test2")}' bla
test1
sh: test2: command not found

Any ideas? As said, escaping and trying single quotes etc. didn't help so far.

Update:
Sorry guys, maybe I am not able to see the wood for the trees at the moment ...
Escaping worked for this case easily:

$ awk 'END{system("echo \"test1; test2\"")}' bla
test1; test2

I am currently trying to figure that out in a garbled sprintf() with lots of these. I think I am done more or less. Thanks all for trying to help. As often, the problems sits in front of the computer :wink:
I have to enclose every semicolon like a=aaa"\;\"b=bbb to make it work.