LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-27-2009, 02:24 AM   #1
laginagesh
Member
 
Registered: Jul 2009
Posts: 42

Rep: Reputation: 16
Question How to capture Script return value from C program


hi,

I have written a program in that i am calling a script using system command
In the script i am returning a value.
how to capture script return value in the C program.
System command always returning 0(zero)
Is there any alternate to capture script return value.

------------------------------------------------------------
C -Program (test.c)
------------------------------------------------------------
#include<stdio.h>
int main()
{
int status=-1;

printf("Status = %d\n",status);
status = system("./test.sh");
printf("Status = %d\n",status);

return 0;
}
---------------------------------------------------------------

Script (test.sh)
----------------------------------------------------------------

#!/usr/bin/expect
spawn telnet 192.168.2.115
expect {
"login: " {send "nagesh\r"}
timeout {puts "\nLogin Failed" ; exit}
}
return -1
exit

-----------------------------------------------------------------
-----------------------------------------------------------------


Cheers
Nagesh
 
Old 08-28-2009, 10:03 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Are you certain that your script is even returning a non-zero exit status? I don't know much about expect, but it doesn't look to me like the exit commands in it are configured to send anything but zero.
 
Old 08-31-2009, 12:11 AM   #3
laginagesh
Member
 
Registered: Jul 2009
Posts: 42

Original Poster
Rep: Reputation: 16
In expect terminal return command returning the value and the same thing displaying on the terminal.

[nagesh@zepplin ~]$ expect
expect1.1> return -1
-1
expect1.2> return 0
0
expect1.3> return 123
123

The same thing when i am trying from script,script is exiting at return command but the return value is not displaying on terminal.

expect1.4> echo 123
123
expect1.5> echo "hi linux"
hi linux

Even echo command working on expect terminal but not working from script.
How to use these(echo,return)commands from the script.

How to return a value from a script and how to capture a script return value from C program.
 
Old 08-31-2009, 02:17 AM   #4
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
Hello laginagesh

The key issue is the expect script exit status. Easier to investigate that at the command line than in C. When it is working OK then call it from the C program.

Test your expect script like this
Code:
./test.sh
echo $?
Bash sets $? to the exit status of the last command.

You will get more help if you attract expect experts by putting expect in the title of your post, maybe "How can I set expect script exit status?". You could start a new thread.

BTW1 please use CODE tags around your code (that's CODE and /CODE in [] brackets -- or you can go into Advanced posting mode and use the # icon).

BTW2 it is safer to give the full path to ./test.sh in your C, rather than assume the compiled C program is being run in the directory containing test.sh.

BTW3 the file name extension .sh is conventionally used to indicate a shell script. Maybe clearer to call the expect script test.expect?

Best

Charles
 
Old 08-31-2009, 09:46 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
As I said, I know next-to-nothing about expect, but I took a look at the man page, and it clearly says that "exit" returns zero unless you specify a different number for it. So I'm thinking you need to specify "exit 1" if the login action times out, or something like that.

It's not clear to me at all what "return" is supposed to do, as there's no clear section describing it as a command word, or whether it should take any arguments. Is it supposed to output something to stdout? It doesn't look to me like it's directly related to the script's exit status.
 
  


Reply



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
program exit return 0 or 1? blackzone Programming 4 07-20-2006 05:00 AM
bash script Want to capture return key and assign a value procfs Programming 9 07-07-2006 01:38 AM
How to call another cgi program to return a variable <db> Programming 3 04-27-2006 03:36 AM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
how to check the return value of a program Sammy2ooo Linux - General 1 04-27-2004 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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