Compilation

Hi All,

Do we have a way to compile a shell program?

I am using ksh/bash/sh and not CSH.

Cheers
Ravi

Why would you need to compile shell script?

Good Question.

Yes , indeed I feel it's better to compile it intstead of executing it on production. Execution always leads to moving/removing/modifying the file content (If the script designed to do so and while testing it always you dont want that to happen)

This is gibberish. Please re-phrase in common English.

Though it is possible to compile Shell Script there is rarely a reason to compile a Shell Script.

Hi Methyl,

Please ignore that statement.

Please let me know how to do that?

Regards
Ravi Sastry P

Compiling a Shell Script is a complete waste of time and effort.
Learn about unix file permissions instead. Just secure the script against change using unix file permissions.
Please do not ask how to do this, it is easy to research.

I'm sure there's all sorts of shell scripts that run on your production machine. Shell scripts aren't inherently flimsy.

Hi All,

Thanks for the reply.

Methyl-> thanks for your suggestion.

I just raised this question out of curoisity and felt few times compilation might have hepled me.

In answer to that question, there is no DIRECT way to compile a shell script. There ARE ways to PACKAGE a shell script, but to truly COMPILE a shell script? None that I know of.

My own methods start with: "first translate the shell script into C, FORTRAN, ADA, or Pascal, then...".

The entire reason for using a shell script instead of assembler, C, Pascal, etc is that it is easy and natural to call utilities, pipe processes, and replicate the commands you would use to perform the same functions manually from the command-line. If you need a compiled program, then you really need to translate the process into something that is somewhat self-sufficient and does not require those external utilities, the shell, or anything but the resources of the executable environment.

Pick a language, but not at random. Examine what your script does, and consider the available compilers in terms of which language implementation answers you need in the most elegant, natural fashion. Design your project with the script functions and this language in mind. When it looks complete (a peer overview may be useful at this point) you can decide on some coding standards and get to the fun part: coding.

You will find the process somewhat slower and more complex, but potentially far more rewarding, compared to scripting.

I would almost bet, however, that compiling is terrible overkill and an inefficient use of resources in answer to your real need.
Can you elaborate a bit on the situation that prompted your question?

@wpeckham
You are right. Most of the unix Shell Script compilers generate "C" language code, then compile.

Hi wpeckham,

Thanks for the explanation. As I said , I asked this just out of curiosity. I found that there is an utility called "shc" which can be used to compile the shell scripts.

The reason for doing the compilation is , so many users will use the same application account to login to the box and If we feel to protect our scripts being inspected/modified by some one else. I know that we can handle this by changing file permissions how ever this I can do only if the user accounts are different. As i stated since all users are using same applicationa account and we dont have any individual accounts to login to the box.So,I just thought it might be a worth to keep only the compiled copies in the directory needed.

Appreciate all for your valuable thoughts on this.

Regards
Ravi Sastry P

So make a new account -- that's what they're there for. Grant read-write permissions to the new user, execute-only permissions to the application group, and nothing else at all to anything. Application users will still be able to run the script under the 'application' user without being able to read or modify it, and people with proper clearance can login to the new account to modify it.

Compiling a script is fundamentally dodgy anyway. I wouldn't trust a compiled script to do exactly what the shell script did for anything but rudimentary scripts, and if it starts messing up how in the world are you going to figure it out? Proper permissions properly is much better than compiling it, that just obfuscates the file in the hope nobody's clever enough to run 'strings' on it.

1 Like

Hi Corona,

Thanks for the suggestion.

Cheers
Ravi