Computing.Net > Forums > Programming > Shell command in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Shell command in C

Reply to Message Icon

Name: seeJ
Date: January 28, 2004 at 09:19:11 Pacific
OS: Linux
CPU/Ram: 1.3
Comment:

Hello all! I have a question on how to run a shell command in C and see the results. So far this is the code i have. When i compile and run it i just get nothing.

#include <stdio.h>
#include <stdlib.h>

int system(const char *string1);

int main()
{
char string1 = "ifconfig";
system(string1);
return(0);
}

Any help with be great. Im not wanting to use ifconfig but i just threw it in there as an example.
Thanks in advance.

C



Sponsored Link
Ads by Google

Response Number 1
Name: Infinite Recursion
Date: January 28, 2004 at 11:20:01 Pacific
Reply:

You are probably not getting values because ifconfig may not be a command (it isn't on my local box)... You are probably wanting to call 'ipconfig'.

There seem to be a few errors in your code that may or may not let it compile...

Notice:

1) No need to declare a system function, it should be included...

2) char string1 = "something"; will not work. You need to use *string1 as my example shows below.

3) Pause before and after so you can see if you are getting any results back, even a command error...


#include <stdio.h>
#include <stdlib.h>

int main (void)
{
char *string1 = "ipconfig";
system("pause");
system(string1);
system("pause");
return(0);
}

IR


0

Response Number 2
Name: seeJ
Date: January 28, 2004 at 12:37:18 Pacific
Reply:

ipconfig is a DOS command. If you noticed in the OS on the top im on Linux. ifconfig is the same as ipconfig in the DOS world. In linux im launching it in a shell so no need for the pause.

Try again



0

Response Number 3
Name: Infinite Recursion
Date: January 28, 2004 at 17:08:54 Pacific
Reply:

Ok. I missed your OS... not that it really matters in this case. However, just to pacify the question, I compiled and executed the code below and it works perfectly.

#include <iostream>

int main (void)
{
char *cmd = "ls -al";
system(cmd);
return 0;
}


0

Response Number 4
Name: Infinite Recursion
Date: January 28, 2004 at 17:09:58 Pacific
Reply:

For the record... I compiled and executed that code in Linux.


0

Response Number 5
Name: wt (by think)
Date: January 28, 2004 at 20:56:09 Pacific
Reply:

hello seeJ,

i am not so sure why you need the code

int system(const char *string1);

in your program, ie. the system function declaration, system() is in stdlib.h; think that is the problem.

=)


0

Related Posts

See More



Response Number 6
Name: Hoang Tran
Date: January 29, 2004 at 01:34:24 Pacific
Reply:

Hi, I haven't got a compiler with me
but this routine could do the job quickly

#define MAX_STRING 256

char *IntToBinary (int number)
{
static char binaryStr[MAX_STRING];
int i;

/* Reset string with 0s to set string terminator */
memset(&binaryStr, '\0', sizeof(binaryStr));

for (i = sizeof(int); i > 0; i--)
{
binaryStr[i] = (number & 1) + '0';
number = number >> 1;
}
return binaryStr;
}


0

Response Number 7
Name: Hoang Tran
Date: January 29, 2004 at 01:38:25 Pacific
Reply:

Hi,
Sorry for the line "for". It should read

for (i = sizeof(int)-1; i >= 0; i--)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Declaring global variable... calling batch file btween...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Shell command in C

vb shell command in win 2000 www.computing.net/answers/programming/vb-shell-command-in-win-2000/5429.html

DOS commands in C www.computing.net/answers/programming/dos-commands-in-c/9449.html

Executing DOS commands in C++ www.computing.net/answers/programming/executing-dos-commands-in-c/6930.html