Created
April 17, 2012 17:38
-
-
Save clinuz/2407696 to your computer and use it in GitHub Desktop.
Example of heredoc and other test types in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This file is part of the xTuple ERP: PostBooks Edition, a free and | |
# open source Enterprise Resource Planning software suite, | |
# Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple. | |
# It is licensed to you under the Common Public Attribution License | |
# version 1.0, the full text of which (including xTuple-specific Exhibits) | |
# is available at www.xtuple.com/CPAL. By using this software, you agree | |
# to be bound by its terms. | |
# | |
# --------------------------------------------------- | |
# script owned properties | |
# --------------------------------------------------- | |
_REVISION="0.0.1" | |
_RELEASED="10/24/11" | |
# ... | |
function followup() | |
{ | |
step | |
if [ -z "$TARG" ]; then | |
read -e -p "Please supply a target binary installer: " -t 20 TARG | |
if [ -z "$TARG" ]; then | |
echo $ERR_TIMEOUT | |
usage | |
elif [ ! -e "$TARG" ]; then | |
echo $ERR_INVALID | |
usage | |
fi | |
fi | |
if [ -z "$VERS" ]; then | |
read -e -p "Please supply a release version: " -t 15 VERS | |
if [ -z "$VERS" ]; then | |
echo $ERR_TIMEOUT | |
usage | |
fi | |
fi | |
if [ -z "$FILE" ]; then | |
read -e -p "Please supply an output filename or press enter for default: " -t 15 FILE | |
if [ -z "$FILE" ]; then | |
default | |
else | |
normalize | |
fi | |
elif [ "$FILE" == "DEFAULT" ]; then | |
default | |
else | |
normalize | |
fi | |
if [ "$SUPR" != 1 ]; then | |
SUPR=0 | |
fi | |
if [ -e "$FILE" ]; then | |
echo $ERR_EXISTS | |
usage | |
fi | |
} | |
# ... | |
function default() | |
{ | |
FILE="xTuple-$VERS-linux-installer" | |
} | |
# ... | |
function normalize() | |
{ | |
FILE=$(echo $FILE | sed 's/[ :?/\*&^$@#%!~()-+={}]/_/g') | |
} | |
# ... | |
function arguments() | |
{ | |
while getopts "t:v:f:uhds" OPTION | |
do | |
case $OPTION in | |
t) | |
TARG=$OPTARG | |
;; | |
v) | |
VERS=$OPTARG | |
;; | |
f) | |
FILE=$OPTARG | |
;; | |
d) | |
FILE="DEFAULT" | |
;; | |
s) | |
SUPR=1 | |
;; | |
h | u | ?) | |
usage | |
exit | |
;; | |
esac | |
done | |
step | |
followup | |
} | |
# ... | |
function usage() | |
{ | |
cat <<- EOT | |
Generate a self executing installer for the Linux platform. Any | |
required fields not specified on the command line will be | |
requested interactively. You supply a target binary and version | |
to this script and it will generate a self-executing installer | |
archive to allow for clickable executable from a Linux GUI. | |
usage: $0 [-t target binary] [-v release version] [-d] [-f filename] [-s] [-u] [-h] | |
-t the target installer binary to wrap up in self-executing script | |
-v the release version to be specified in the default filename | |
-f the filename to generate for the installer | |
-d use the default filename standard format (avoid interactive) | |
-s suppress output from the script | |
-u, -h print this message and exit | |
@author W. Cole Davis | |
@revision $_REVISION - $_RELEASED | |
Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple. | |
EOT | |
exit 0 | |
} | |
# ... | |
function setup() | |
{ | |
step | |
line=$(grep --text --line-number '^==TEMPLATE==$' $0 | awk -v FS=: '{ print $1+1 }') | |
TEMPLATE=$(tail -n +$line $0) | |
TEMPLATE=$(echo "$TEMPLATE" | sed "s:{{REVISION}}:$_REVISION:g") | |
TEMPLATE=$(echo "$TEMPLATE" | sed "s:{{RELEASED}}:$_RELEASED:g") | |
environment | |
contents | |
} | |
# ... | |
function environment() | |
{ | |
return | |
} | |
# ... | |
function contents() | |
{ | |
step | |
step | |
step | |
CONTENTS=$(gzip -cf "$TARG" | base64 -) | |
write | |
} | |
# ... | |
function write() | |
{ | |
step | |
echo "$TEMPLATE" > "$FILE" | |
echo "$CONTENTS" >> "$FILE" | |
step | |
chmod +x "$FILE" | |
step | |
} | |
# --------------------------------------------------- | |
# known error types and messages | |
# --------------------------------------------------- | |
ERR_TIMEOUT="\nERROR: The script has timed out without a response during an interactive session" | |
ERR_INVALID="\nERROR: An invalid file or file-path has been supplied" | |
ERR_EXISTS="\nERROR: The filename $FILE already exists, please use a different filename" | |
# --------------------------------------------------- | |
# output text feed to keep user informed of steps | |
# --------------------------------------------------- | |
STEPS=( | |
"Initializing" | |
"Propagating" | |
"Setting up template" | |
"Fetching binary data" | |
"Compressing binary data" | |
"Encoding data payload" | |
"Writing install script" | |
"Setting permissions" | |
"Done" | |
) | |
_curr=0 | |
function step() | |
{ | |
if [ "$SUPR" = 1 ]; then | |
return | |
fi | |
echo " => ${STEPS[$_curr]}" | |
_curr=$_curr+1 | |
} | |
# ... handle the arguments passed in from the command line ... | |
arguments $@ | |
# ... grab the template script to be generated ... | |
setup $0 | |
echo "\nInstaller generated as $FILE. Exiting." | |
exit 0 | |
==TEMPLATE== | |
#!/bin/bash | |
# This file is part of the xTuple ERP: PostBooks Edition, a free and | |
# open source Enterprise Resource Planning software suite, | |
# Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple. | |
# It is licensed to you under the Common Public Attribution License | |
# version 1.0, the full text of which (including xTuple-specific Exhibits) | |
# is available at www.xtuple.com/CPAL. By using this software, you agree | |
# to be bound by its terms. | |
# | |
# ====================================================================== | |
# This is an auto-generated file by the linux-shar-bundler.sh script | |
# {{REVISION}} - {{RELEASED}} | |
# | |
# @todo Needs trap on err/interrupt to cleanup! | |
# | |
# ====================================================================== | |
# ... | |
function environment() | |
{ | |
SCRIPTNAME="$0" | |
XTERM=0 | |
ENVIRONMENT=$(expr `uname` : '^[Dd]arwin.*') | |
if [ "$ENVIRONMENT" -gt 0 ]; then | |
FLAGS="-D" | |
else | |
FLAGS="-d" | |
fi | |
_null=$(which gksudo) | |
if [ "$?" = 0 ]; then | |
AUTH=gksudo | |
else | |
which kdesu | |
if [ "$?" = 0 ]; then | |
AUTH=kdesu | |
else | |
AUTH= | |
XTERM=1 | |
fi | |
fi | |
} | |
# ... | |
function setup() | |
{ | |
step | |
line=$(grep --text --line-number '^==BEGINENCODED==$' $0 | awk -v FS=: '{ print $1+1 }') | |
INSTALLER=$(tail -n +$line "$SCRIPTNAME") | |
} | |
# ... | |
function unpack() | |
{ | |
step | |
LOCTMP=$(mktemp "./$SCRIPTNAME.XXXXXXX") | |
echo "$INSTALLER" | base64 "$FLAGS" - | gunzip -f - > $LOCTMP | |
chmod +x "$LOCTMP" | |
} | |
# ... | |
function install() | |
{ | |
step | |
if [ "$XTERM" = 0 ]; then | |
`$AUTH $LOCTMP` | |
elif [ "$XTERM" = 1 ]; then | |
`sudo xterm -e $LOCTMP &` | |
fi | |
} | |
# ... | |
function cleanup() | |
{ | |
step | |
if [ -e "$LOCTMP" ]; then | |
`rm -f "$LOCTMP"` | |
fi | |
step | |
} | |
STEPS=( | |
"Setting up environment" | |
"Fetching data" | |
"Unpacking installer" | |
"Attempting to launch installer" | |
"Cleaning up" | |
"Done" | |
) | |
_curr=0 | |
function step() | |
{ | |
echo " => ${STEPS[$_curr]}" | |
_curr=$_curr+1 | |
} | |
# ... need to test the environment to determine a few features ... | |
environment | |
# ... grab the contents of the installer so we can decode and decompress it ... | |
setup | |
# ... unpack the contents of the installer ... | |
unpack | |
# ... fire up the installer ... | |
install | |
exit 0 | |
==BEGINENCODED== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment