Error Message in function causing failure.....

I have a long busybox ash script that has 3 stages.

  1. Identify and Capture information on variable data sources, output the information to text file on each data source.

  2. Using data from 1 above now actually do data processing on each individual dataset.

  3. Produce report.

So stage 1 detects what data sources are currently live, stage 2 then process's data on each source and stage 3 gives the report on number of sources and each source.

Stage 2 can take literally hours on one data source and the application allows for saving current state to allow it to be stopped and continued later.

Therefore I've written the script to allow the job(s) to run but with a CTR-c function script that allows the user to either kill the whole job, or to kill just the current dataset.

However this function script is falling over and I can't see why.

Here's the function script....

trapcatch ()  { echo "Ctl-c Detected, what do you want to do?"
  echo "Please choose the number of one of the following options"
  echo "1.  Jump past this Set"
  echo "2.  Exit altogether"
  echo "Any other key or no key just continues"

  read -t 5 -p "Choose option 1-3 now : " KEYHIT

  case $KEYHIT in
  1*) Echo "OK, I'll bypass this Set then"
      continue 2
      ;;
  2*) Echo "OK, I'll exit gracefully then"
      # need to add the graceful exit here
      exit 99
      ;;
  *)  Echo "OK I'll just carry on then......"
      ;;
  esac    }

Here's the script section that calls the function

trap trapcatch SIGINT SIGTERM

Here's the output I'm getting when I press CTR-c

^CCtl-c Detected, what do you want to do?
Please choose the number of one of the following options
1.  Jump past this Set
2.  Exit altogether
Any other key or no key just continues
/usr/sbin/sourceslist.ver0.99: line 1: Echo: not found
/usr/sbin/sourceslist.ver0.99: let: line 1: arithmetic syntax error

I'm guessing the issue maybe to do with the continue 2 command in the script (the trap call happens inside a double loop that loops till a condition is met that moves the script onto the next dataset)

All helpful ideas appreciated.....

1) Surely "Echo" should be lower case "echo" ?

2) No idea about the "let" because none of the code posted contains "let".

1 Like

Jees....sometimes you just spend far too long in front of a screen, total brain*art on my part.

Having fixed that though the read statement is not outputting the Choose 1-3 option, any ideas why? The syntax of the read looks OK to me.

Thanks

---------- Post updated at 04:11 PM ---------- Previous update was at 03:23 PM ----------

Actually I'm going to post this new problem in a new thread as the original problem for this thread is showing fixed.

Sorry, I don't have a Shell where that syntax of read is valid.

While tinkering I had to change it to:

echo "Choose option 1-3 now : \c"; read KEYHIT

(My echo is the one which doesn't have the "-n" option).

For echo's that don't have the \c option, the printf builtin ought to work the same in nearly any shell:

printf "%s" "Choose option 1-3 now : "

Not sure if ash supports 'select', but it might ease the menu creation and execution.

It doesn't unfortunately.

But since it supports functions, something similar can be built:

$ cat bb-select.sh

#!/bin/bb

bbselect () {
        local FS="$IFS";        IFS="|" # Split on pipes
        local VAR="$1";         shift   # Get var to save in
        local TITLES="$*"               # TITLES="title1|title2|title3"
        local N=1

        while [ "$#" -gt 0 ]            # Print all titles
        do
                printf "%2d)\t%s\n" "$N" "$1"
                let N=N+1;      shift
        done

        set -- $TITLES                  # $1="title1", $2="title2", ...
        IFS="$FS"                       # Put old splitter back

        read -p "? " REPLY < /dev/tty || return 1

        # Return error when given a non-number
        case "$REPLY" in
        [1-9])  ;;      [0-9][0-9])     ;;
        *)              return 1        ;;
        esac

        # Return error when given an out-of-range number
        [ "$REPLY" -lt 1 ]  && return 1
        [ "$REPLY" -gt $# ] && return 1

        # Look up the correct title text
        let N=REPLY-1
        [ "$N" -gt 0 ] && shift "$N"

        # Put the title text in the variable *named* in VAR
        read $VAR <<EOF
$1
EOF
        # Return success
        return 0
}

while ! bbselect X "Door 1" "Door 2" "Door 3"
do
        echo "Invalid reply $REPLY"
done

echo "Got value '$X' -- number $REPLY"

$ ./bb-select.sh

 1)     Door 1
 2)     Door 2
 3)     Door 3
? 0
Invalid reply 0
 1)     Door 1
 2)     Door 2
 3)     Door 3
? 50
Invalid reply 50
 1)     Door 1
 2)     Door 2
 3)     Door 3
? slartibartfast
Invalid reply slartibartfast
 1)     Door 1
 2)     Door 2
 3)     Door 3
? 3
Got value 'Door 3' -- number 3

$