Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Then Shell Script Loader could be one solution. Scripts that work with Shell Script Loader could also be compiled to be made as one file. See the section about external compilers. And PlayShell makes use of all those features. You could examine it to see how it's done. See start.sh, compile.sh and loader/*.
It's also helpful since you can test your code with your modules separated without needing to compile all together first and makes debugging easier to detect which file actually had the error.
The compiling aspects of these are pretty much what I'm trying to do, thanks! I'm going to finish what I have just for fun if nothing else, but I may use one of these in the future.
The object is to end up with a completely contained script I can move anywhere and have work, not one that relies on other files being in the correct place.
Not to be picky, but if you are running a script over files to extract functions, I would say that you are still relying on files to be available???
The object is to end up with a completely contained script I can move anywhere and have work, not one that relies on other files being in the correct place. This is normally a fine solution, just not in this case.
So what prevents you from including it during editing?
Even a simple awk (though I would use perl) preprocessor could do that for you. (if line contains ### function ###, AND file "function.sh" exists, replace the line with the contents of function.sh).
It probably lies on the intention that he wants to keep things modular. On a module-oriented code if a part of a script-based application has to be modified only the part where the code works would have to be modified. It's also easier to understand as functions (module tasks) of the application is somehow mapped through different files. When an application gets too big it would already be difficult to edit and debug as you have to go up and down from one place of the code to another. It's also less flexible. With a modular code you could just have to keep the task on one module file and leave it that way. No matter how complex the codes are placed in there you just have to remember what it does and that's it. If it's placed on a large file instead one could even be confused for reading unnecessary codes which aren't really part of the main function. Last thing is, modular codes could be shared or reused among different applications.
So what prevents you from including it during editing?
Even a simple awk (though I would use perl) preprocessor could do that for you. (if line contains ### function ###, AND file "function.sh" exists, replace the line with the contents of function.sh).
Yeah, that's basically what I did, just with declare instead of awk. I source the file in a new shell, and spit out that function.
Quote:
Originally Posted by konsolebox
Last thing is, modular codes could be shared or reused among different applications.
This was precisely the intent, I script a lot and I find myself writing all the same functions over and over again. Now I can write them once, and it does the copying/pasting for me in a way that keeps my source readable.
Edit: I'm also going to add a feature that can update my scripts automatically, so if I change a portion of a module it'll recompile all the scripts that use that module.
I'm also going to add a feature that can update my scripts automatically, so if I change a portion of a module it'll recompile all the scripts that use that module.
Seriously SSL fits your needs. But looks like you like you really want to have your own implementation so good luck with that. Feel free to study the codes of compiler.gawk as you might find something there.
As for automatic recompilation when your scripts are updated you could make use of find and compare each module file to the compiled file. This can be done with Bash and [[ X -nt Y ]] or [[ X -ot Y ]]. When one of the modules are detected to be newer than the compiled file, you could then call your method to compile the scripts. With SSL you could just run compiler.gawk. Maybe incorporating it with make and Makefiles would be easier as well.
If you're actually using SSL with the include() function you wouldn't have to compile the scripts everytime you had to run them so it would be an easier form for debugging.
Example:
Code:
#!/bin/bash
TARGETFILE='/path/to/existing_compiled_file'
MODULES_DIR='/path/to/modules' ## e.g. ./source
MAIN_SCRIPT='mainscript.sh' ## could also be in a form like $MODULES_DIR/mainsript.sh
function compile {
gawk -f loader/compiler.gawk -- -a "$MODULES_DIR" -s /bin/bash -O -o "$TARGETFILE" "$MAIN_SCRIPT"
}
DOCOMPILE=false
if [[ ! -f $TARGETFILE ]]; then
DOCOMPILE=true
else
while read -r FILE; do
[[ $FILE -nt "$TARGETFILE" ]] && {
DOCOMPILE=true
break
}
done < <(exec find "$MODULES_DIR" -type f -name '*.sh')
fi
[[ $DOCOMPILE == false ]] || compile
Seriously SSL fits your needs. But looks like you like you really want to have your own implementation so good luck with that.
It definitely does, but this is more of an exercise/challenge for myself than it is a means to an end. It'll be useful once I have it working, but it's more about the learning. If I just wanted a bash compiler I would definitely drop this and use that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.