| | |
how to return value from shell script to the calllin C program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
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
thanks in advance
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
c Syntax (Toggle Plain Text)
int main() { int fd,i,j,flag; char *fname,member[40],ch,str[40]; fd = open("/etc/members",O_RDONLY); if(fd == -1) printf("Members info can not be retrived."); ch = 0; flag = 0; while(read(fd,&ch,1)) { i = 0; while(1) { if(ch == '\t') break; member[i++]=ch; read(fd,&ch,1); } member[i]='\0'; strcpy(str,"./shscrpt "); strcat(str,member); j=system(str); printf("%d",j); while(read(fd,&ch,1)) if(ch == '\n') break; } return 0; }
thanks in advance
Last edited by Ancient Dragon; Feb 4th, 2008 at 9:55 am. Reason: add code tags
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.
•
•
Join Date: Feb 2008
Posts: 6
Reputation:
Solved Threads: 0
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?
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
•
•
•
•
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?
•
•
•
•
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>.
Try: man 2 wait or info wait.
Last edited by jim mcnamara; Feb 4th, 2008 at 5:22 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: why this happens?
- Next Thread: precedence of logican and and or operator
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking hardware histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






