#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)convert-number 3.2.1 13.10.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Convert Number String e.g. 2,3,5-7,9 into a sequential number string e.g. 002 003 005 006 007 009 #@ #@Usage: convert-number numberstr #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -p, --padding : Numbers are sorted and left padded with 0 (default padding is 3). #@ -x, --max : Maximal - useful if one calls script with e.g. "5-". #@ NumberStr: Can be a numer or a string e.g. "2,3,5-7,9" #@ #@Examples: #@ convert-number --max 8 2,5- #@ Displays "002 005 006 007 008" # # Exit codes: # 01: Unknown or wrong parameter. # 02: Invalid number # 03: Minimum is 1. # 04: Number is greater than max number. # 99: User interrupt. # # See also: # **install-scripts**(1) # # Update history: # # V 2.0.0 05.07.2019 New version # V 3.0.0 02.05.2020 Minor changes # V 3.0.1 11.06.2020 Using library # V 3.1.0 05.06.2023 New copyright # V 3.1.1 28.07.2024 Trapping ctrl-c # V 3.2.0 12.08.2024 New minor version # V 3.2.1 13.10.2024 Converted from tcsh to bash # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Preset numberstr="" maxnumber="" padding="3" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; -p | --padding) shift if [ "$1" != "" ]; then padding=${1} result=`echo "$padding" | grep '^[0123456789]*$'` if [ "$result" = "" ]; then errstr="Not a number: '$padding'." fi shift else errstr="Please specify a number after parameter '$pname'." fi ;; -x | --max) shift if [ "$1" != "" ]; then maxnumber=${1} result=`echo "$maxnumber" | grep '^[0123456789]*$'` if [ "$result" = "" ]; then errstr="Not a number: '$maxnumber'." fi shift else errstr="Please specify a number after parameter '$pname'." fi ;; *) shift paramck=`echo "$pname" | grep '^-'` # Keys don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else if [ "$errstr" = "" ]; then if [ "$numberstr" = "" ]; then numberstr=`echo "$pname" | tr -d ' '` else errstr="Number string was already specified '$numberstr'. Unknown additional parameter: '$pname'." fi fi fi esac done # Plausibility checks if [ "$errstr" = "" -a "$numberstr" = "" ]; then errstr="Please give a number string like '2,3,5-7,9'." fi # Display help or error message DisplayHelp ### Main # Create empty scratchfile printf "" > $scratchfile # Create string with blank sparated numbers numbers=`echo "$numberstr" | sed 's|,| |g'` # Iterate over each number for num in $numbers; do range=`echo "$num" | grep '^[0123456789]*-[0123456789]*$'` if [ "$range" != "" ]; then rstr1=`echo "$range" | cut -d'-' -f1` rstr2=`echo "$range" | cut -d'-' -f2` if [ "$rstr2" != "" ]; then if [ "$rstr1" = "" ]; then rstr1=1 fi seq -f "%0${padding}g" $rstr1 1 $rstr2 >> $scratchfile else if [ "$rstr1" != "" -a "$maxnumber" != "" ]; then seq -f "%0${padding}g" $rstr1 1 $maxnumber >> $scratchfile else exitcode=2 errormsg $exitcode "($progstr) String '$num' is not a valid number or expression." Cleanup exit $exitcode fi fi else numstr=`echo "$num" | grep '^[0123456789]*$'` if [ "$numstr" != "" ]; then printf "%0${padding}g\n" "$numstr" >> $scratchfile else exitcode=2 errormsg $exitcode "($progstr) String '$num' is not a valid number or expression." Cleanup exit $exitcode fi fi done filemin=`cat $scratchfile | sort -u | head -n 1` if [ $filemin -lt 1 ]; then exitcode=3 errormsg $exitcode "($progstr) Selection is less then min '1'." fi if [ $exitcode -eq 0 ]; then if [ "$maxnumber" != "" ]; then filemax=`cat $scratchfile | sort -u | taillastline` if [ $filemax -gt $maxnumber ]; then exitcode=4 errormsg $exitcode "($progstr) Selection is greater then max '$maxnumber'." fi fi # Display result if [ $exitcode -eq 0 ]; then cat $scratchfile | sort -u fi fi # Cleanup and exit Cleanup exit $exitcode