I am attempting to code and execute bash scripts using C lang. Purpose is for a personal project I'm writing in C, and seeing if I can include bash capabilities as well. I created a simple C file script.c
(compiled as script
) for testing, but I am getting the error:
bash: ./script: Permission denied
I have tried running with sudo permissions, but still getting similar response:
sudo: unable to execute ./script: Permission denied
All I intend to do with this file is run the bash command: echo "hello"
using C
Here is the C file:
#include <stdio.h>
#include <stdlib.h>
#define SHELLSCRIPT "\
#/bin/bash \n\
echo \"hello\" \n\
"
int main() {
puts("executing script");
system(SHELLSCRIPT);
return 0;
}
My wild guess is that the line with system(SHELLSCRIPT);
is the cause, being that not even a root user can make system calls. However, after looking through the internet consensus is that it is possible to execute bash scripts in this fashion. Any help or insight is apprecitated.
chmod +x ./script
(on the compiled binary) first before trying to execute it directly? – Thomas Ward Dec 04 '19 at 19:31ls -l /path/to/script
and [edit] the result into your post. Thank you for helping us help you! – Elder Geek Dec 04 '19 at 19:33sudo: unable to execute ./script: Permission denied
Forgot to put that in the question – jsfer Dec 04 '19 at 19:33ls -l script
, andfile script
. It seems the program never ran. If you have not done so, please ensure you can compile a .c file located in the same directory as your "script" program--like a simple hello world program--and run the executable, using commands analogous to those you're using for "script". In your [edit] you can clarify if that succeeded and give the details if it didn't. – Eliah Kagan Dec 04 '19 at 19:35cc bla-script.c -o bla-script
(I changed the file name). Executed with./bla-script
– Doug Smythies Dec 04 '19 at 19:37gcc -o script script.c
– jsfer Dec 04 '19 at 19:39sudo
to execute your script which doesn't need elevated privileges? – Thomas Ward Dec 04 '19 at 19:40