Putting bash script in C program

suppose i have a bash script:

#!/bin/bash

echo "hello"
echo "how are you"
echo "today"

how can i put the entire script above into a basic c program?

i do not want to translate the bash code to a c code. i want C to run the bash code. is this possible?

i found this on the internet but i'm not sure i understand it:

#include <stdio.h>
#include <stdlib.h>
 
#define SHELLSCRIPT "\
for ((i=0 ; i < 10 ; i++))\n\
do\n\
echo \"Count: $i\"\n\
done\n\
"
 
int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

i'm looking for a C program where i can just insert the bash code into it and have it run. something like this:

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

int main()
{
    puts("Will execute sh with the following script :");
    puts(#!/bin/bash
    echo "hello"
    echo "how are you"
    echo "today");
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"

int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}

The #define SHELLSCRIPT directive is used in C to define a named constant: SHELLSCRIPT which contains the shell script.

The back slash \ at the end of each line is used to type the code in next line for better readability.

The new line \n is used to put the code in next line.

Double quotes " within the shell script has to be escaped \" to distinguish it between the original opening and closing double quotes of constant value.

Finally the system function is called by passing the named constant: SHELLSCRIPT as argument which contains the shell script.

I hope it makes sense now.

1 Like

thank you soo much. you are a godsend. thank you.

i'm curious though, and please forgive my newbie status to c++, but can you please tell me how do i run this program? i like to keep things simple.

so if i named your script to mycplus, can i run it from the command line like this:

#mycplus

can a C program that contains a bash script like the one above be compiled and made into an executable?

how would you compile it?

thanks again!!!

First of all this is a C Program. Below are the steps to compile:

  1. Create a C program file:
$ cat > cprog.c
#include <stdio.h>
#include <stdlib.h>

#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
echo \"how are you\" \n\
echo \"today\" \n\
"

int main()
{
    puts("Will execute sh with the following script :");
    puts(SHELLSCRIPT);
    puts("Starting now:");
    system(SHELLSCRIPT);
    return 0;
}
  1. Compile the C program file using cc compiler:
$ cc cprog.c
  1. Compiler will compile & link and create an output file: a.out which you can run to see the result:
$ ./a.out

If you want to create an output file with a name of your choice, use -o option:

$ cc cprog.c -o cprog

Run the compiled & linked output file created: cprog

$ ./cprog
1 Like

thanks again. one last question.

the bash script i intend on embedding into this C program will be accepting several arguments from the command line. and it is a very big bash script. will C be able to catch the arguments?

meaning, after i have embedded the bash script into C, i will be running the C program from the command line like this:

./cprog arg1 arg2 arg3 arg4 arg5 'arg6' 'arg7' arg8 arg9 arg10

the arguments are meant for the bash script. will C be able to pass it to the script?

if i was running the bash script alone without it being embedded into C, the bash script will accept and use the arguments just fine. but i just want to make sure when it is embedded in C there wont be problems.

thanks again.

No, that will not work. To make things simple I would suggest to write the bash script in a separate file and call it from C program by passing the required arguments:

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

int main()
{
    puts("Starting now:");
    system("/path_to_script/script_name arg1 arg2 arg3");
    return 0;
}

This way it will be easy to maintain.

that was my first option. but the whole goal here was to conceal the bash code. so i figure if i can embed it into C, i'll be able to hide the code. but if C is calling the script, the script will be open for everyone to read.

as i was typing this, something else occurred to me. i'm thinking, i can encode the script using a password. tell C to decode the script using the password i put in the C code. and then C can call the script after script has been decoded.

dont know if that makes sense.

Yes, it makes sense, but did you consider using utilities like SHC - shell script compiler?

Won't work.

strings executablefile

try shc.

i tried shc but the bash script i have is too big for shc.

Why is it so big? Have you embedded data in it?

If you don't want anyone to know what your code does, you can't let them have any access at all to it.

If you distribute a binary to run on another's computer(s), anyone who really wants to is going to be able to figure out everything it does.

Although I will admit that there can be value in obfuscating what's going on, if only to prevent those just smart enough to be dangerous from damaging things....

yes. i just followed the instructions. my bash script is over 10MB in size. so when u run shc on it, it aborts complaining about the bytes.

i really believe this can be done. we just need to put heads together.

It would be better to put the data in the C part. If you make it a global array, they can be quite large. You could write it into the script's standard input with popen.

const unsigned char bytes[]={ 0x00, 0xab, ... };

int main(void)
{
        FILE *fp=popen("programname", "w");
        fwrite(bytes, 1, sizeof(bytes), fp);
}

you might want a decryption step between that and writing it of course, since people may be peeking in your file.

By definition your program includes complete instructions for decrypting itself. How could you possibly hope to hide it? At best you can obfuscate it. A truly determined person will find the contents.

If you could force people to login to your sever to use it, that could be made secure. the program wouldn't need to run on their machine, they wouldn't have access to the file.

1 Like

The bash script is over 10 MB in size? I think this needs to be converted to a compiled program in C, etc. How can you possibly maintain a bash script that large?

I think most of the 10mb is file contents, not script code. A big tarball that's been base64'ed into text or something. It's embedded in the script with the hopes of obscuring it.