Operating System - Linux
1752808 Members
6700 Online
108789 Solutions
New Discussion юеВ

Re: Restart a function if it gets aborted during the excution of script

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again) Raghu:

Since the code I presented is destructive to the argument list (the 'shift') we need to preserve the original argument list and then process it, in another pass, if its elements are valid.

The following modification might be useful to you.

# cat ./parse
#!/usr/bin/sh
typeset -i MAX=$#-1
typeset -i N=0

if [ $# -eq 0 ]; then
STEP=0
echo "no arguments; use file"
else
set -A STEPS $@
while (( $# > 0 ))
do
STEP=$1
shift
if [ `expr "${STEP}" : '[0-9]*'` -ne `expr "${STEP}" : '.*'` ]; then
echo "ERROR: STEP must be a number; scanning: ${STEP}"
exit 1
fi
echo "STEP-${STEP} requested"
done
fi

N=0
while (( N <= MAX ))
do
echo "now processing STEP-${STEPS[$N]}"
let N=N+1
done
exit 0

Note that the script's value of 'MAX' is established to be zero-relative, since elements of an array ('STEPS') are referenced with zero as the first element.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

>JRF: set -A STEPS $@

If you want perfection on saving and restoring, you should quote the parms:
set -A STEPS "$@"
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again):

Dennis>: If you want perfection on saving and restoring, you should quote the parms:
set -A STEPS "$@"

Agreed, although in the context I suggested the argument list, there would be no need to supply it in quotes like "4 5 6" 7 8 versus "4 5 6 7 8" versus 4 5 6 7 8 (without any quotes. Your point is noted, though. Thanks.

Regards!

...JRF...