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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [solved] awk: return code from system() always 0 ??
# 1  
Old 07-11-2013
[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:
Code:
                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!

Last edited by zaxxon; 07-11-2013 at 05:44 AM.. Reason: added info
# 2  
Old 07-11-2013
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?
Code:
 
 
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

# 3  
Old 07-11-2013
Have you tired explicitly returning the exit code??
Code:
return $?

or
Code:
return $rc

# 4  
Old 07-11-2013
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 ?
# 5  
Old 07-11-2013
@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 $? Smilie

@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.

Last edited by zaxxon; 07-11-2013 at 08:14 AM.. Reason: rephrasing, added info
# 6  
Old 07-11-2013
Here is my test script:
Code:
$ 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:
Code:
...
                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 Smilie

The cmd_str with the Perl script is exactly the same as in test.sh...
# 7  
Old 07-11-2013
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 Smilie
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.

Last edited by zaxxon; 07-11-2013 at 09:47 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

[Solved] Redhat system is not booting in GUI mode

Hi Guys Required help in Redhat 6.1. After installation of Redhat 6.1 in VMware system is not going in GUI mode. please to solve the issue... Thanks... (5 Replies)
Discussion started by: deviltech
5 Replies

2. Shell Programming and Scripting

[Solved] Process dies when launched in system startup script

Good morning! I'm trying to add Maven to the system boot by the moment without success. Testing the operation of the script I realize that the process isn't persistent when the program is launched with the start option. ---- #Startup Script ---- #! /bin/sh # chkconfig: 345 99 1 #... (5 Replies)
Discussion started by: carpannav
5 Replies

3. HP-UX

[Solved] Increase the file system size

Dear Friends, I would like to increase the size of a file system from 10GB to 15GB. System is runing on HP-UX 11.31. Please help in the matter. Regards, Bhagawati Pandey (3 Replies)
Discussion started by: BPANDEY
3 Replies

4. HP-UX

[Solved] Mount HP-UX File System in Windows 7

Hello, We just got a rx4640 that is running on: HP-UX B.11.31 U ia64 2426088954 unlimited-user license There is a plenty of space in it and we would like to use it for storage. I can access with ftp and place the files in there but i need to mount the file system in every PC that is running... (7 Replies)
Discussion started by: gjk
7 Replies

5. Shell Programming and Scripting

Managing awk return code over SSH command

Hello all, This bellow code works. I'm just trying to find a simplified way to achieve this. I'm sure there is an easier way and it must be to simple for me to find. Verify that the OS version is 6.1. If not exit the script. Of course if i just put a && exit 1 at the end of the ssh... (11 Replies)
Discussion started by: maverick72
11 Replies

6. Shell Programming and Scripting

[Solved] File System Monitoring Script

Hello Scripts Guru I had created a shell script to monitor the threshold of the file system, but some where it is not giving the correct output. Request to all to hel me out I am getting the following output /dev/vg00/lvol3 mounted on 1865224 10% / is 2097152% /dev/vg00/lvol1 mounted on... (2 Replies)
Discussion started by: indrajit_renu
2 Replies

7. Shell Programming and Scripting

C, sh, perl, system(): Can't transfer a return code appropriately: help, pls

I am using a perl-script from C-code, executing it by the 'system(..)' comand. The problem is to return the perl-return code to the C correctly. Default the 'system()' shell is Bourne: sh My try: (perl_src.c_pl - the perl script; t_sys - C-program with system() call (I will show it... (7 Replies)
Discussion started by: alex_5161
7 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Programming

Return code from system()

Hi, Can any one help me in knowing how can I get the return codes/Error codes when using the system() command to fork a command? Regards, MK (1 Reply)
Discussion started by: mradulkaushik
1 Replies

10. Programming

How to get system() function executed cmd return value ?

Hi, How I can get system function executed command return value ? I want to know mv command success or not ? #include <stdio.h> main() { int ret; ret = system( "mv x.dat y.dat" ); printf( "system ret:\n", ret ); } (3 Replies)
Discussion started by: haiudhaya
3 Replies
Login or Register to Ask a Question