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

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

Recommended Answers

All 3 Replies

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.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.