1

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.

jsfer
  • 11
  • 2
    Did you chmod +x ./script (on the compiled binary) first before trying to execute it directly? – Thomas Ward Dec 04 '19 at 19:31
  • Welcome to AskUbuntu! I'm not a C programmer, and this appears to be either about that (so off-topic here) but on topic at https://stackoverflow.com/ or about file permissions. If you are unsure, issue the command ls -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:33
  • @ThomasWard Yes, returns sudo: unable to execute ./script: Permission denied Forgot to put that in the question – jsfer Dec 04 '19 at 19:33
  • I recommend you [edit] this with the exact command you used to compile the "script" program, all messages (if any) from the compiler, the exact command you used to run the program and its full output, ls -l script, and file 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:35
  • 1
    It works fine for me, exactly as written. Compiled with cc bla-script.c -o bla-script (I changed the file name). Executed with ./bla-script – Doug Smythies Dec 04 '19 at 19:37
  • @EliahKagan i compiled with gcc: gcc -o script script.c – jsfer Dec 04 '19 at 19:39
  • ... better question, why're you using sudo to execute your script which doesn't need elevated privileges? – Thomas Ward Dec 04 '19 at 19:40
  • Thank you for confirming @DougSmythies It appears I didn't properly mount the hard drive I'm working in with correct permissions. So some files just won't work outright. This program works when created in a different, correctly mounted, drive. – jsfer Dec 04 '19 at 19:51

1 Answers1

0

It appears I didn't properly mount the hard drive I'm working in with correct permissions. So files just won't work outright. This program works when created in a different, correctly mounted, drive.

jsfer
  • 11