LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-03-2013, 12:33 AM   #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-03-2013, 12:34 AM   #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, 06:52 AM   #33
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 07:02 AM   #34
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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 07:05 AM.
 
Old 08-03-2013, 08: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, 10: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 10:22 AM.
 
Old 08-03-2013, 11: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, 11: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, 07:45 PM   #39
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:40 PM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 12:55 PM
How to extract lines from file using AWK keenboy Linux - General 7 08-05-2010 09:29 AM
using awk substring function on a file in a bash script matt007 Programming 3 06-17-2008 09:17 PM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 07:37 PM


All times are GMT -5. The time now is 10:58 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration