Ubuntu – How to use a C program to run a command on terminal

command linescripts

I want to create a program in C language that would allow me to run a command in the terminal.

I have made a program in shell script which would give me the ip of any website that is opened in my browser. This shell script is executed by entering this command in the terminal:

sudo tcpdump -n dst port 80 -i eth

My professor told me to create a program in C language which would open the terminal and enter this command and then my shell script would work.

Please tell me how to create such a program.

Best Answer

you could use the system() function available in stdlib.h to run commands.

DESCRIPTION

system() executes a command specified in string by calling /bin/sh -c string, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

You could read more about it over here http://linux.about.com/library/cmd/blcmdl3_system.htm