From 33775ffb1d2062789da073a75585908bef95a454 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sun, 31 Jan 2021 19:48:56 +0100 Subject: [PATCH] Refactored template creation Fix obsolete escapes in sequencer help output --- sequencer/sequencer.sh | 86 ++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/sequencer/sequencer.sh b/sequencer/sequencer.sh index ee6e45c..665c2c2 100755 --- a/sequencer/sequencer.sh +++ b/sequencer/sequencer.sh @@ -40,7 +40,7 @@ Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS] --help, -h : Display help --helpapi, -ha : Display help about build-in supporting functions (e.g. exe,addconf,echerr,...) - --profile, -p : Sequence configuration profile name (default: \"default\") + --profile, -p : Sequence configuration profile name (default: "default") If supported by seq --quiet, -q : Don't ask for permission to execute steps If called without starting step number, only this help is shown @@ -51,11 +51,11 @@ Usage: ${0##*/} [OPTIONS] [STEP NUMBER(s) or ALIAS] [STEP ARGUMENTS] ( e.g.: exe apt update ) --version : Display version of sequencer and revision of sequence - [STEP NUMBER\"(s)\" 1-${MAX_STEP} or ALIAS] + [STEP NUMBER"(s)" 1-${MAX_STEP} or ALIAS] No STEP or ALIAS : assume 1 as starting point Single STEP or ALIAS : starting point of sequential process Multiple STEPS or ALIAS : execute only given steps - (e.g. $0 \"2 4 12\") + (e.g. $0 "2 4 12") multiple steps need to be given as string [STEP ARGUMENTS] * : Arguments will be passed to selected steps and step infos as: @@ -100,7 +100,7 @@ sequencer.sh build-in functions: Supporting: dry-run (-d): only print command without execution verbose (-v): print command before execution - exep \"[COMMANDLINE]\" + exep "[COMMANDLINE]" See exe, but support for pipes or redirects. Important: - Shell commands cd, read, ... won't work because COMMANDLINE is started in a new shell. @@ -122,7 +122,7 @@ sequencer.sh build-in functions: addConf [SOURCE TYPE] Trying to write or append text or a file () to a destination file. If the CONFIGFILE exists, a backup (name_%Y%m%d-%H%M%S.bck) is saved at the same location. - If -s fails or -m, \"$(realpath "$MISSING_CONF")\" is created with the conflicts + If -s fails or -m, "$(realpath "$MISSING_CONF")" is created with the conflicts to be resolved by the user. -c : create a new file @@ -680,41 +680,47 @@ createTemplate() { if [ -f $TEMPLATE_NAME ] ; then return 1 fi - echo "#!/bin/bash" > $TEMPLATE_NAME - echo >> $TEMPLATE_NAME - echo "toolName=mytool" >> $TEMPLATE_NAME - echo >> $TEMPLATE_NAME - echo "# Get script working directory" >> $TEMPLATE_NAME - echo "# (when called from a different directory)" >> $TEMPLATE_NAME - echo "WDIR=\"\$( cd \"\$( dirname \"\${BASH_SOURCE[0]}\" )\" >>/dev/null 2>&1 && pwd )\"" >> $TEMPLATE_NAME - echo "CONFIG=0" >> $TEMPLATE_NAME - echo "CONFIG_FILE_NAME=\"\${toolName}.cfg\"" >> $TEMPLATE_NAME - echo "CONFIG_FILE_TEMPLATE=\"\$WDIR/\${CONFIG_FILE_NAME}.example\"" >> $TEMPLATE_NAME - echo >> $TEMPLATE_NAME - echo "step_config() {" >> $TEMPLATE_NAME - echo " echo \"Called once before executing steps.\"" >> $TEMPLATE_NAME - echo " ## e.g. to source a config file manually:" >> $TEMPLATE_NAME - echo " #. \"\$CONFIG_FILE\"" >> $TEMPLATE_NAME - echo " ## or to use sequencer api:" >> $TEMPLATE_NAME - echo " #initSeqConfig \"\$CONFIG_FILE_NAME\" \"\$CONFIG_FILE_TEMPLATE\"" >> $TEMPLATE_NAME - echo " #if [ \$? -eq 0 ] ; then" >> $TEMPLATE_NAME - echo " # CONFIG=1" >> $TEMPLATE_NAME - echo " #fi" >> $TEMPLATE_NAME - echo "}" >> $TEMPLATE_NAME - echo >> $TEMPLATE_NAME - echo "step_1_info() { echo \"My custom step\"; }" >> $TEMPLATE_NAME - echo "step_1_alias() { ALIAS=\"begin\"; }" >> $TEMPLATE_NAME - echo "step_1() {" >> $TEMPLATE_NAME - echo " echo \"Doing something for step \$1 ...\"" >> $TEMPLATE_NAME - echo " echo \"Command line arguments starting with argument 2: \$@\"" >> $TEMPLATE_NAME - echo " # Use exe for regular command" >> $TEMPLATE_NAME - echo " # Use exep \"command\" for commands containing pipes or redirects" >> $TEMPLATE_NAME - echo " exe ls" >> $TEMPLATE_NAME - echo " exep \"dmesg | grep usb\"" >> $TEMPLATE_NAME - echo "}" >> $TEMPLATE_NAME - echo >> $TEMPLATE_NAME - echo "VERSION_SEQREV=${VERSION_REV}" >> $TEMPLATE_NAME - echo ". $0" >> $TEMPLATE_NAME + cat > $TEMPLATE_NAME << TEMPLATE_EOF +#!/bin/bash + +toolName=mytool + +# Get script working directory +# (when called from a different directory) +WDIR="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)" +CONFIG=0 +SCRIPT_NAME=\$(basename -- \$0) +SCRIPT_NAME=\${SCRIPT_NAME%%.*} +CONFIG_FILE_NAME="\${SCRIPT_NAME}.cfg" +CONFIG_FILE_TEMPLATE="\$WDIR/\${CONFIG_FILE_NAME}.example" + +step_config() { + echo "Called once before executing steps." + ## e.g. to source a config file manually: + #. "\$CONFIG_FILE" + ## or to use sequencer api with global config file: + #initSeqConfig "\$CONFIG_FILE_NAME" "\$CONFIG_FILE_TEMPLATE" + ## or to use sequencer api with profile config file support: + #initSeqConfig -p "\$SCRIPT_NAME" "\$CONFIG_FILE_TEMPLATE" + #if [ \$? -eq 0 ] ; then + # CONFIG=1 + #fi +} + +step_1_info() { echo "My custom step"; } +step_1_alias() { ALIAS="begin"; } +step_1() { + echo "Doing something for step \$1 ..." + echo "Command line arguments starting with argument 2: \$@" + # Use exe for regular command + # Use exep "command" for commands containing pipes or redirects + exe ls + exep "dmesg | grep usb" +} + +VERSION_SEQREV=${VERSION_REV} +. $0 +TEMPLATE_EOF chmod +x $TEMPLATE_NAME return 0