943,621 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10943
  • C RSS
Feb 4th, 2008
0

how to return value from shell script to the calllin C program

Expand Post »
i am writing a pogram where i have to find out which all computers are up on my lan...for this i have called shell script program from my C code which pings to all computers on lan....now i want the shell script to return to the C code "1" incase the computer is up...how do i do that...

i=$(ping $1 -c 2 | grep -c "ttl=")
if [ $i -eq 2 ]
then echo host is reachable
else echo host is not reachable
fi

my C code
  1. int main()
  2. {
  3. int fd,i,j,flag;
  4. char *fname,member[40],ch,str[40];
  5. fd = open("/etc/members",O_RDONLY);
  6. if(fd == -1) printf("Members info can not be retrived.");
  7. ch = 0; flag = 0;
  8. while(read(fd,&ch,1))
  9. {
  10. i = 0;
  11. while(1)
  12. {
  13.  
  14. if(ch == '\t') break;
  15. member[i++]=ch;
  16. read(fd,&ch,1);
  17. }
  18. member[i]='\0';
  19. strcpy(str,"./shscrpt ");
  20. strcat(str,member);
  21. j=system(str);
  22. printf("%d",j);
  23. while(read(fd,&ch,1)) if(ch == '\n') break;
  24. }
  25. return 0;
  26. }

thanks in advance
Last edited by Ancient Dragon; Feb 4th, 2008 at 9:55 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nehamore is offline Offline
6 posts
since Feb 2008
Feb 4th, 2008
0

Re: how to return value from shell script to the calllin C program

The system function on line 21 does not return the shell program's return value. You will have to call some other function that spawns processes, such as one of the spawn family of functions.
Last edited by Ancient Dragon; Feb 4th, 2008 at 10:01 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Feb 4th, 2008
0

Re: how to return value from shell script to the calllin C program

The system function on line 21 does not return the shell program's return value. You will have to call some other function that spawns processes, such as one of the spawn family of functions.
hey thanks for ur help but can u please elaborate on this...what should i add to my code so that it works the way i want it to work?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nehamore is offline Offline
6 posts
since Feb 2008
Feb 4th, 2008
0

Re: how to return value from shell script to the calllin C program

Click to Expand / Collapse  Quote originally posted by nehamore ...
hey thanks for ur help but can u please elaborate on this...what should i add to my code so that it works the way i want it to work?
It sounds like you are coding UNIX. From the man page for system:
Quote ...
If the return value of system() is not -1, its value can be decoded
through the use of the macros described in <sys/wait.h>. For
convenience, these macros are also provided in <stdlib.h>.
Generally, that would be WEXITSTATUS(system("./myscript") );
Try: man 2 wait or info wait.
Last edited by jim mcnamara; Feb 4th, 2008 at 5:22 pm.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: why this happens?
Next Thread in C Forum Timeline: precedence of logican and and or operator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC