Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exec either a shell script or an executable?

Tags:

c

exec

This is a school project I'm working on.

I'm writing a web server. If the requested resource has the extension 'cgi', I need to fork and exec the program. If the cgi is a compiled executable, this works:

execl(path, (char*) 0);

But if the cgi program is a script that needs to be interpreted, I need to do something like this:

execl("/bin/sh", path, path, (char*) 0);

How can I write my code so it will handle either scenario? How does my shell do this? Should I use the file program to determine if it's an executable or text, and if it's text, then assume it needs to be interpreted?

like image 329
Trevor Dixon Avatar asked Feb 17 '23 15:02

Trevor Dixon


1 Answers

Why do you need to invoke "/bin/sh" explicitly if it's a script?

As long as the script has execute permission and the proper shebang at the top, you should be able to execute it just as if it were an executable.

like image 74
Keith Thompson Avatar answered Mar 05 '23 14:03

Keith Thompson