Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to create an executable script which is not readable/writable

However since an interpreter needs to read this file it is not possible to do so directly. Therefore I wanted to know if I could explicitly load an interpreter into a C program along with my script.

This would allow me to set the permission of the C program according to my needs.

share|improve this question
    
Your C wrapper is going to use setuid? If your question is simply about how to call the shell executable from a C program, this sounds more like a question for StackOverflow to me. –  depquid Apr 1 '13 at 14:15
add comment

1 Answer

up vote 3 down vote accepted

You can call shell scripts from C application using system() function:

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

Example:

#include <stdlib.h>

int main(int argc, char *argv[]) {

    char* command = "\
for i in 1 2 3 4 5;\
    do echo \"$i\"; \
done";

    system(command);
    return 0;
}

Running:

$ gcc test.c -o test
$ chmod 100 test
$ ./test
1
2
3
4
5

Remember that argument is a command string so you have to terminate lines with semicolons. You also have to escape quotation marks.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.