This is a bit kludgy, but – write a C program that looks like this:
#include <stdio.h>
#include <stdlib.h>
︙
FILE *sh;
sh = popen("/bin/sh", "w"); // Use "/bin/bash" if you need to.
if (sh == (FILE *)NULL) {
perror("popen");
exit(1);
}
<-------- Insert magic here.
fclose(sh);
At the “Insert magic here.” point, insert code to write the script to sh
; for example,
fprintf(sh, "for x in red blue green\n");
fprintf(sh, "do\n");
fprintf(sh, " echo x = \"$x\"\n");
fprintf(sh, "done\n");
If it’s more than just a few lines, it may be more manageable to store it as a string array.
Then compile this program and make the binary execute-only (e.g., mode 710 or 711).
No solution will be foolproof,
because users will be able to see what commands are being run by using ps
in another window,
but that would give them only fragmentary glimpses at the script.
Of course, if the logic of your script is really sensitive,
the real answer is to translate it into a compilable language.
+rx
or+x
for the user (or group) that can execute it bychmod '+rx' <user or group> '<exec file>'
-rwxr-x---
can't be modified by myself. If I copy it (and therefore I am the owner of that copy) I am able to write and execute afterwards... @RichHomolka Edited the question