Putting bash script in C program


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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  
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:
# 3  
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  
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:
# 5  
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  
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  
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.
Login or Register to Reply

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

More UNIX and Linux Forum Topics You Might Find Helpful
Running a C/C++ program and/or bash script from a server
frad
I wish to be able to give to a client the opportunity to : 0) Turn one of my ubuntu computers into a webserver 1) See a webpage after visiting a url where an external user/client can set a couple of variables (e.g. Number1= ?, Number2=?) 2) By pressing "run" the program runs on my machine 3)...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
Putting a separator in file using awk/bash
askerbis
I have a file with the following content: a-123-345-232 a-23343-4545-545 a-67676-45454-8787 a-129-8912-9824 b-564-78678-2322 b-5454-76767-8899 b-85554-124-152-29 c-34534-654543-323 (... and so on, actually these are pretty huge records) Now, I want that the file should not be broken in to...... Shell Programming and Scripting
8
Shell Programming and Scripting
bash script to check if a program is running
mcknz
I'm a bit new to bash programming and I was assigned the job of writing a script that will check to see if a program server is running and to restart the program if it is not up. The script is supposed to check the program every hour (which I have looked up and I believe I know how to do) and send...... Shell Programming and Scripting
3
Shell Programming and Scripting
Problem with bash shell script program
gskumar1234
Hi, This is my program. #!/bin/bash today=`date +"%b-%d-%Y"` SERVICE="pbxconnect.php" if ; then echo "pbx program is running" else nohup php pbxconnect.php > logpbx-$today.txt & fi On executing using "sh myprogram.sh" , i get the following error. myprogram.sh: line 4: ' My...... Shell Programming and Scripting
7
Shell Programming and Scripting
Run Program from Bash CGI-Script
JMooney5115
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is a problem I am having with my 2 semester senior project. I have a LAMP server running Ubuntu 9.10 with...... Homework & Coursework Questions
8
Homework & Coursework Questions