1

1

First Steps Into Shells

Also relates to UNIX

I have had Cygwin sitting on my Windows OS for some time now, and only utilised it occasionally to get to grips with the intricacies of the UNIX system. I finally decided the other day to put it to work a bit, and start learning Shell scripting. The main incentive being that my text editors and macros were not reducing the workload enough in reworking old web sites to improved standards and accessibility levels. In only a few days graft, I am already revelling in the offerings of text utilities like SED, AWK, TR and GREP, and I have a few reasonable scripts to rework the content of some of my HTML files.

As a little challenge to learn the language a bit, I have been working at a simple text routine to crunch Javascript files down for bandwidth optimisation. A while ago I tried a Windows tool called JSCrucher (I cannot remember where I found this, but there are plenty of tools available in this Google Search), but this repeatedly crashed my system, and the tools available in the Bash shell appear to make this task relatively easy. While Javascript source files are well spaced, indented and commented for easy editing, the uploaded file can have all comments and whitespace removed. However, a space does need to remain in variable and function declaration (eg var obj or function validate()). As an initial shell command, I came up with this little pipeline:


# place a dot separator in variable declaration
sed -e 's/var /var./g'               
# place a dot separator in function declaration
    -e 's/function /function./g'     
# remove all comments
    -e 's///.*[a-z]$//' testfile | 
# remove all whitespace
tr -d '[:space:]' |                  
# remove dot separator in variable declaration
sed -e 's/var./var /g'               
# remove dot separator in function declaration
    -e 's/function./function /g'     

As a newcomer to Bash and Co., I am sure there are more efficient ways to go about this, but it is always nice to get out of the blocks with a new language. I can see the UNIX text tools getting significant use over the coming months. Hope to play around with this a bit more, improve it and get it into a shell script which also creates a source backup of the Javascript file and can run batches.

Posted on Oct 15, 2003 at 05:45:26. [Comments for First Steps Into Shells- 1]

Breadcrumbs Trail

[ Home ] -> TW Blog -> Shell Scripting
Site Map

The Severn Solutions website achieves the following standards:

[ XHTML 1.0 ] [ CSS 2 ] [ WAI AA ] [ Bobby AA ]

Page compiled in 0.005 seconds