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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2008
Posts: 6
Reputation: nehamore is an unknown quantity at this point 
Solved Threads: 0
nehamore nehamore is offline Offline
Newbie Poster

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

 
0
  #1
Feb 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Feb 4th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 6
Reputation: nehamore is an unknown quantity at this point 
Solved Threads: 0
nehamore nehamore is offline Offline
Newbie Poster

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

 
0
  #3
Feb 4th, 2008
Originally Posted by Ancient Dragon View Post
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?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

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

 
0
  #4
Feb 4th, 2008
Originally Posted by nehamore View Post
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:
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC