midnx
Posts: 7
Joined: Thu Mar 16, 2017 11:38 pm

Sending a terminal command through C

Thu Mar 16, 2017 11:48 pm

Hello,

I want to hook up a buttons to the GPIO pins on the Pi, and then when it is pressed, it will send a simple command that I can see on the terminal.

So If I press the button, it will send this to terminal:
echo hello world

I need to do this in C/C++

I am not sure how to do it, so far I have done the tutorial on wiringpi GPIO which makes a led blink, but this is a little different and I don't know how to proceed. Can anyone help me? Eventually I plan to hook up many buttons and run more complex commands but I want to get the basics down to get me going.

Thanks

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Sending a terminal command through C

Fri Mar 17, 2017 1:59 am

midnx wrote:So If I press the button, it will send this to terminal:
echo hello world
I need to do this in C/C++
I am not sure how to do it,
printf should be able to output any text to the terminal you need.

ElEscalador
Posts: 680
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Sending a terminal command through C

Fri Mar 17, 2017 2:24 am

printf or cout will work. cout is much easier, but printf can do much more later on. I learned cout first and thus, never made myself really learn printf...which slows me down sometimes. Still..if you're in a hurry:

Code: Select all

cout << "Hello world" << endl;  // "endl" starts a new line
Above is the basics. Below is how your code might look:

Code: Select all

int button=0;
button=yourgpioreadfunction(yourGPIOpin);
if(button == 1){
cout << "Hello world" << endl;
}
return 0;
This does nothing if the variable named button does not equal one when it gets to the if statement.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

midnx
Posts: 7
Joined: Thu Mar 16, 2017 11:38 pm

Re: Sending a terminal command through C

Fri Mar 17, 2017 4:12 am

Thanks, but I need it to do the actual command, not just print the raw text.

To put it another way. For example, in linux the "date" commands prints out the date.

But in C++ if you do

cout << "date" << endl

It will just print the word date. I need it to send the COMMAND called date to the terminal, which the terminal will then interpret and show as month/day/hours/etc.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Sending a terminal command through C

Fri Mar 17, 2017 5:40 am

Use the system() function:

Code: Select all

system("date");
The string can be any command you would give on the command line.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Sending a terminal command through C

Fri Mar 17, 2017 5:55 am

This code will do it:

Code: Select all

      if ((ppid=fork())==0)
      {
         printf("Child process\n");
         execlp("bash","bash","./your_shell_script.sh","",NULL);
         return; //terminates child process
      }

      //does this block? YES!
//      printf("Parent process %d\n",ppid);
      wait(&status);
If you need to fully specify the contents of your shell script, you can write it explicitly and I think you might be able chmod it the same way. If not this will run a static shell script.

Here is supporting code for it:

Code: Select all

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
...
        int ppid;
        int status;
        FILE *fptr;
        char c;

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23710
Joined: Sat Jul 30, 2011 7:41 pm

Re: Sending a terminal command through C

Fri Mar 17, 2017 9:38 am

woodystanford wrote:This code will do it:

Code: Select all

      if ((ppid=fork())==0)
      {
         printf("Child process\n");
         execlp("bash","bash","./your_shell_script.sh","",NULL);
         return; //terminates child process
      }

      //does this block? YES!
//      printf("Parent process %d\n",ppid);
      wait(&status);
If you need to fully specify the contents of your shell script, you can write it explicitly and I think you might be able chmod it the same way. If not this will run a static shell script.

Here is supporting code for it:

Code: Select all

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
...
        int ppid;
        int status;
        FILE *fptr;
        char c;
Or you could just use the system call as mentioned above? Saves all that boiler plate code.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

midnx
Posts: 7
Joined: Thu Mar 16, 2017 11:38 pm

Re: Sending a terminal command through C

Sun Mar 19, 2017 9:26 am

Thanks, this helped alot. I got the hang of it now.

midnx
Posts: 7
Joined: Thu Mar 16, 2017 11:38 pm

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:12 pm

I have another question if you don't mind.

How can I store the output of a system command into a C string?

Say I do:

Code: Select all

 system("date"); 
And let's say that prints out

Code: Select all

JAN 01 00:00:00 2017
Now I want to store "JAN 01 00:00:00 2017" into a string variable in the C code.

Code: Select all

char date[] = "JAN 01 00:00:00 2017";
Except I want it to get the date from the output of the 1st system command, instead of typing it in.

User avatar
DougieLawson
Posts: 36182
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:19 pm

If you're just trying to read the system date/time use

Code: Select all

#include <time.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
  time_t rawtime;
  struct tm * timeinfo;
  char time_of_day[21];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (time_of_day,21,"%T %b %d %Y",timeinfo);
  printf("%s\n", time_of_day);

  return 0;
}
You can change the format string to get it the way you want it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jahboater
Posts: 4697
Joined: Wed Feb 04, 2015 6:38 pm

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:23 pm

Look up popen( "date", "r" ) (man popen) which will return a FILE pointer which can be read. This is the output from your program, in this case date. Remember the pclose() at the end.

Edit: crossed with Dougie's post. If "date was just an example, use popen, if you really do want the date, use the proper code in the his post.

midnx
Posts: 7
Joined: Thu Mar 16, 2017 11:38 pm

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:38 pm

DougieLawson wrote:If you're just trying to read the system date/time use

Code: Select all

#include <time.h>
[/quote]

I'm not, I just used date as an example.  I want to be able to take the output of any system/bash command and store it into a C string.

jahboater
Posts: 4697
Joined: Wed Feb 04, 2015 6:38 pm

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:41 pm

midnx wrote: I'm not, I just used date as an example. I want to be able to take the output of any system/bash command and store it into a C string.
Then popen() is your friend!

User avatar
DougieLawson
Posts: 36182
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Sending a terminal command through C

Sat Mar 25, 2017 7:41 pm

Look carefully at the commands because lots of things have official Linux APIs to get the value from the system.

Also if you're just trying to run a sequence of commands and test results then a plain old bash script is better than a C program every time.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
ab1jx
Posts: 868
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: Sending a terminal command through C

Sun Mar 26, 2017 3:29 am

This sounds dangerously like a homework assignment.

You could redirect the output of the command like:
command >& afile.txt
then in another terminal window watch afile.txt like
tail -f afile.txt

The tail should work even if command is being run from a program. See man (1) tail

afile.txt will contain a permanent record of output from command, you watch it growing with tail. I use this on problematic builds to catch all the error messages.

Return to “C/C++”