0

I'm not familiar with bash scripting. In bash script, I'm attempting to validate a prompted value against a defined list, so that when I call:

./test.bash [caseyear] [sector]

caseyear and sector will be validated in the following attempted bash script:

Validating Year
#1 Check the value of the caseyear Parameter
if [ "$1" -eq "$1" 2> /dev/null ]; then
  if [ $1 -ge 2009 ]; then
    export caseyear=$1
  else
    echo -e "\n\tSpecify [caseyear] value greater than or equal to 2009.\n"
    exit 4
  fi
else
  echo -e "\n\tSpecify [caseyear] value greater than or equal to 2009.\n"
  exit 3
fi

I'm having trouble validating that the entered value must be in the sector list, so I tried the following script:

Validating Sector

#sector list list_of_sectors="11 40 44_45 52 10_11"

#1 Check if sector value is in the sector list $ function exists_in_list() { LIST=$1 DELIMITER=$2 VALUE=$3 LIST_WHITESPACES=echo $LIST | tr "$DELIMITER" " " for x in $LIST_WHITESPACES; do if [ "$x" = "$VALUE" ]; then return 0 fi done return 1 }

#2 Check if sector is null if [ -z "$2" ]; then echo -e "\n\tSpecify [caseyear] sector value.\n" exit 2 else #export omitflag="$(echo $2 | tr '[a-z]' '[A-Z]')" #Convert to upper case #3 Check if sector value is in the sector list export sector

#------------------->Problem Area #How do I pass the entered $sector value to exists_in_list function that matches with the list, $list_of_sectors?

if [ $(exists_in_list $list_of_sectors) -ne 0 ] ; then echo -e "\n\tSpecify [sector] sector value.\n" exit 1 fi fi

echo -e "\nYou Specified - CaseYear:$caseyear, Sector:$sector"

Thank you!

Kusalananda
  • 333,661
lydias
  • 117
  • 6

1 Answers1

2

This would be easiest to do with a case ... esac statement:

#!/bin/sh

caseyear=$1 sector=$2

if [ "$#" -ne 2 ]; then echo 'expecting two arguments' >&2 exit 1 fi

if [ "$caseyear" -lt 2009 ]; then echo 'caseyear (1st arg) should be 2009 or larger' >&2 exit 1 fi

case $sector in 11|40|44_45|52|10_11) # do nothing ;; *) # error echo 'sector (2nd arg) is not in list' >&2 exit 1 esac

printf 'Got caseyear="%s" and sector="%s"\n' "$caseyear" "$sector"

This script does not use anything that is bash-specific, so I changed the interpreter to /bin/sh.

If the script does not get precisely two arguments, it exits immediately. It validates the first argument by ensuring that it is numerically greater or equal to 2009. It validates the second argument by matching it against the entries in a |-delimited list in the case statement.

The case statement could also be written in less number of lines as

case $sector in
        (11|40|44_45|52|10_11) ;;
        (*) echo 'sector (2nd arg) is not in list' >&2; exit 1 ;;
esac

The left parenthesis of the pattern string in each case is optional. The ;; after the final case is also optional.

The initial assignment of $1 and $2 to our two variables can be made a bit fancier, which introduces an extra check to make sure that both arguments exist and that they are not empty strings:

caseyear=${1:?'1st argument caseyear must be specifiend and not empty'}
sector=${2:?'2nd argument sector must be specified and not empty'}

The expansion ${variable:?string} will cause the string string to be outputted if the variable variable is unset or empty. If that happens, the script will terminate with a non-zero exit status. This would allow us to drop the test on $#.

As far as I can see, there is no need for exporting the two variables. If you are starting some external tool that needs these variables in its environment, you may export them both at once at the end of the code above, before running your external tool, using

export caseyear sector
some-other-utility
Kusalananda
  • 333,661
  • 1
    Using ! [ "$caseyear" -ge 2009 ] instead of [ "$caseyear" -lt 2009 ] may be better in that it would also rule out non-numbers. – Stéphane Chazelas Jan 13 '23 at 07:46
  • Hi, I entered ./testBash1.sh [44_45] [2010], and this is the error message I received : `[: [44_45]: integer expression expected' – lydias Jan 13 '23 at 17:59
  • 2
  • You asked for caseyear and sector but you've provided sector and caseyear. 2. Don't include the square brackets - those are generally considered to be syntax markers for people to understand they present a value to be supplied
  • – Chris Davies Jan 13 '23 at 18:26
  • @roamia , thanks that solved the problem – lydias Jan 13 '23 at 18:44