LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-02-2013, 11:33 PM   #31
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by konsolebox View Post
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.
 
Old 08-02-2013, 11:34 PM   #32
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
I'm not exactly sure what you mean by "compress it further"
I mean removing extraneous whitespace and newlines. It doesn't seem to be possible, but it's compressed enough as is.
 
Old 08-03-2013, 05:52 AM   #33
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
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???
 
Old 08-03-2013, 06:02 AM   #34
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by apottere View Post
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).

Last edited by jpollard; 08-03-2013 at 06:05 AM.
 
Old 08-03-2013, 07:44 AM   #35
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 08-03-2013, 09:19 AM   #36
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
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.

Last edited by apottere; 08-03-2013 at 09:22 AM.
 
Old 08-03-2013, 10:22 AM   #37
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by apottere View Post
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
 
Old 08-03-2013, 10:34 AM   #38
apottere
LQ Newbie
 
Registered: Jul 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by konsolebox
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.
 
Old 08-03-2013, 06:45 PM   #39
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Easier to use make to rebuild - that is exactly what it is for.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I extract columns from a file without using awk or perl? KG425 Programming 13 06-06-2012 11:40 AM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
How to extract lines from file using AWK keenboy Linux - General 7 08-05-2010 08:29 AM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 08:17 PM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 06:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration