building a script from functions

7 views
Skip to first unread message

Ken Irving

unread,
Mar 20, 2013, 2:09:21 PM3/20/13
to foss-sc...@googlegroups.com
For larger shell scripts I've found it useful to break the script
into chunks, mostly functions, then build them into the final script.
Here's an abbreviated build.sh script for one project:

#!/bin/bash
path=${0%/*}
order=(
shebang
prelude
warn
error
debug
parse-query
resolve-query
resolve-path
main
call-main
manpage
)
for f in ${order[*]}; do
cat $path/$f
printf "\n"
done

The output can be redirected to a file for testing or to an executable
script name in a bin directory, e.g.,

project/build.sh > myproj && sh myproj
project/build.sh > ~/bin/myproj

During development, the function files can be sourced to run and test
in the current shell.

I probably should've used 'printf "%s\n" "$(<$path/$f)"' in the loop
to avoid the 'useless use of cat' in favor of bash's $(<f) built-in.

Just tossing this out there in part to see if this 'foss shell scripting'
list is still going, if it has persisted through the spam. More likely
it will maybe retrigger the spammer...

Ken

Steve Searle

unread,
Mar 20, 2013, 3:28:27 PM3/20/13
to foss-sc...@googlegroups.com
Around 06:09pm on Wednesday, March 20, 2013 (UK time), Ken Irving scrawled:

> Just tossing this out there in part to see if this 'foss shell scripting'
> list is still going, if it has persisted through the spam. More likely
> it will maybe retrigger the spammer...

I'm still hanging in here - and your mail was much more welcome than one
offering me a job I don't want in a place I don't live.

Cheers

Steve

--

19:27:13 up 6 days, 18:45, 1 user, load average: 0.08, 0.07, 0.02
Reply all
Reply to author
Forward
0 new messages