Putting bash script in C program

Tags
programming

 
Thread Tools Search this Thread
# 1  
Old 02-17-2013
Putting bash script in C program

suppose i have a bash script:
Code:
#!/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:


Code:
#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:

Code:
#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;
}

# 2  
Old 02-17-2013
Code:
#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.
This User Gave Thanks to Yoda For This Post:
SkySmart (02-17-2013)
# 3  
Old 02-17-2013
Quote:
Originally Posted by bipinajith
Code:
#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.
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:

Code:
#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!!!
# 4  
Old 02-17-2013
First of all this is a C Program. Below are the steps to compile:

1. Create a C program file:
Code:
$ 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;
}

2. Compile the C program file using cc compiler:
Code:
$ cc cprog.c

3. Compiler will compile & link and create an output file: a.out which you can run to see the result:
Code:
$ ./a.out

If you want to create an output file with a name of your choice, use -o option:
Code:
$ cc cprog.c -o cprog

Run the compiled & linked output file created: cprog
Code:
$ ./cprog

This User Gave Thanks to Yoda For This Post:
SkySmart (02-17-2013)
# 5  
Old 02-17-2013
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:

Code:
./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.
# 6  
Old 02-17-2013
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:
Code:
#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.
# 7  
Old 02-17-2013
Quote:
Originally Posted by bipinajith
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:
Code:
#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.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Program or bash script to see total progress of copy robertkwild Programming 20 09-22-2017 12:20 PM
Help with Bash Shell Script Spell Checker program. DsmRacer2k14 Shell Programming and Scripting 1 10-20-2014 07:26 PM
Passing arguments to interactive program through bash script, here document biochemist Shell Programming and Scripting 13 07-17-2014 04:53 PM
Taking information from a postgres sql query and putting it into a shell script array JSNY Shell Programming and Scripting 3 05-05-2014 05:19 AM
Running a C/C++ program and/or bash script from a server frad UNIX for Dummies Questions & Answers 1 12-13-2013 12:50 PM
Putting a separator in file using awk/bash askerbis Shell Programming and Scripting 8 08-16-2013 11:30 AM
Making a bash script and small C program for a homework assignment twk101 Shell Programming and Scripting 1 04-26-2013 03:22 AM
Putting s Perl module into your own script olearydc Shell Programming and Scripting 2 08-19-2012 07:36 AM
Bash script to start program and answer prompts? lanew Shell Programming and Scripting 2 06-12-2012 07:49 PM
Bash script to execute a program to rename files raymen UNIX for Dummies Questions & Answers 4 04-07-2012 10:48 AM
bash script to check if a program is running mcknz Shell Programming and Scripting 3 08-08-2011 09:37 PM
Problem with bash shell script program gskumar1234 Shell Programming and Scripting 7 08-08-2011 07:43 AM
putting color on output file script lhareigh890 Shell Programming and Scripting 3 03-14-2011 06:06 AM
Run Program from Bash CGI-Script JMooney5115 Homework & Coursework Questions 8 01-27-2011 07:51 PM
Script to open program and send/execute command in program tuathan UNIX for Dummies Questions & Answers 1 11-19-2008 09:59 PM