66

Bash scripts are very useful and can save a lot of programming time. So how do you start a bash script in a C++ program? Also if you know how to make user become the super-user that would be nice also. Thanks!

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
Kredns
  • 36,461
  • 52
  • 152
  • 203

5 Answers5

87

Use the system function.

system("myfile.sh"); // myfile.sh should be chmod +x
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 6
    Thanks but you forgot to include this: system("./myfile.sh"); – Kredns Mar 14 '09 at 16:51
  • 3
    The current directory `.` *shouldn't* be in your `$PATH`. – Keith Thompson Jul 11 '13 at 22:16
  • 1
    @KeithThompson The script doesn't have to be located in your current directory though. The point is, there's no requirement in the `system` function for prefixing scripts with ".". – Mehrdad Afshari Jul 12 '13 at 00:24
  • 4
    @MehrdadAfshari: How about this. If the script is in the current directory, you should use `./`. If it's somewhere in your `$PATH`, don't use `./`. (`.` probably shouldn't be in `$PATH`, and definitely shouldn't be at the front of `$PATH`.) – Keith Thompson Jul 12 '13 at 00:58
  • Will system() block if the script file prompts? – rstackhouse Jan 28 '14 at 18:09
  • Im trying to do that but is not working. I don't know another way to send commands to the shell. Doing that is so much easy to handle files and so on. Maybe i need to digg around the popen, but `system("cmd")` looks simpler even if doesn't work to me at the moment. – m3nda Apr 16 '15 at 11:15
  • 9
    `system()` must be used with caution. According to it's man page: _Do not use `system()` from a program with set-user-ID or set-group-ID privileges, because strange values for some environment variables might be used to subvert system integrity. Use the `exec(3)` family of functions instead, but not `execlp(3)` or `execvp(3)`. `system()` will not, in fact, work properly from programs with set-user-ID or set-group-ID privileges on systems on which `/bin/sh` is bash version 2, since bash 2 drops privileges on startup._ – max Oct 24 '15 at 04:16
  • Hi I am using system sys call only to call my shell script, what i saw was my c++ program is running as root but when the shell script is executed it is running as different user, I don't want this behaviour, i want my shell script to run as same as the caller c++ program which is root. My shell script uses #!bin/bash – Aryaman Gupta Jun 14 '21 at 14:44
26
#include <stdio.h>
#include <stdlib.h>

// ....


system("my_bash_script.sh");
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • 7
    What if the bash script was to echo some text to tty? E.g. what if it were a "cat"? Could the c program have that output? – Joe C Jul 30 '13 at 15:50
  • 2
    Does this wait until the process completes? – Tom Dec 14 '20 at 00:39
16

Since this is a pretty old question, and this method hasn't been added (aside from the system() call function) I guess it would be useful to include creating the shell script with the C binary itself. The shell code will be housed inside the file.c source file. Here is an example of code:

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

#define SHELLSCRIPT "\
#/bin/bash \n\
echo -e \"\" \n\
echo -e \"This is a test shell script inside C code!!\" \n\
read -p \"press <enter> to continue\" \n\
clear\
"

int main() {

system(SHELLSCRIPT);
return 0;
}

Basically, in a nutshell (pun intended), we are defining the script name, fleshing out the script, enclosing them in double quotes (while inserting proper escapes to ignore double quotes in the shell code), and then calling that script's name, which in this example is SHELLSCRIPT using the system() function in main().

Yokai
  • 1,170
  • 13
  • 17
  • Fisrt, thank you for your kind sharing. I found that the program will not pause at this line "read -p \"press to continue\" \n\". Do you know why? – Azreal Dec 10 '21 at 02:25
11

The only standard mandated implementation dependent way is to use the system() function from stdlib.h.

Also if you know how to make user become the super-user that would be nice also.

Do you want the script to run as super-user or do you want to elevate the privileges of the C executable? The former can be done with sudo but there are a few things you need to know before you can go off using sudo.

dirkgently
  • 108,024
  • 16
  • 131
  • 187
6

StackOverflow: How to execute a command and get output of command within C++?

StackOverflow: (Using fork,pipe,select): ...nobody does things the hard way any more...

Also if you know how to make user become the super-user that would be nice also. Thanks!

sudo. su. chmod 04500. (setuid() & seteuid(), but they require you to already be root. E..g. chmod'ed 04***.)

Take care. These can open "interesting" security holes...

Depending on what you are doing, you may not need root. (For instance: I'll often chmod/chown /dev devices (serial ports, etc) (under sudo root) so I can use them from my software without being root. On the other hand, that doesn't work so well when loading/unloading kernel modules...)

Community
  • 1
  • 1
Mr.Ree
  • 8,320
  • 27
  • 30