0

I have a script that can be provided with [the potential] to have an unknown quantity of known arguments

That is, I know ALL the arguments that can be provided, but as they are all optional, they may not be getting provided all the time.

I have written this code (below), how can I send all the arguments provided to the 'setOptions' function? once they.are there I can iterate through them and process whats needed, but I don't have a clue how to pass them to the function.

Basically the case just checks to see if B or anything else is provided, if B then thats fine, but anything else needs to be iterated through and tested.

at the moment Im able to test if the value is a B or not and from there... well you can see.

  # request statement
  echo  "Enter 'B' to build or the name of test you wish to run."
  read testOptions
  # check the options
  case $testOptions in
    B )
      # tell them the tests are being rebuilt
      echo " The test will be rebuilt"
      COMMAND_LINE=$COMMAND_LINE" build"
      # display the command to be used and execute it.
      echo $COMMAND_LINE
      $COMMAND_LINE
    ;;
    * )
      # we are not rebuilding, take all the options and send to processing
      setOptions $testOptions #<- processes and builds the commandline from the  arguments
      # Execute the command
      executeOptions $testOptions #<- executes the command line
    ;;
  esac
4
  • 1
    Are you just looking for read -a testOptions? Commented Jan 24, 2023 at 15:49
  • I am honestly not sure, I know very little when it comes to shell script, if this was php, C# or JS I would be over and done with, but this is a bit different, and quite enjoyable tbh! Commented Jan 24, 2023 at 15:52
  • The penny may have dropped for me just then, so if I were to use sendOptions $testOptions Then in the function sendOptions, I would use read -a $1 and basically a case var in $1 .... iteration code... easc Commented Jan 24, 2023 at 15:56
  • You don't usually use $ in read: it takes zero or more variable names. Commented Jan 25, 2023 at 8:04

1 Answer 1

0

If you're using bash, read -a testOptions is a simple way to parse the inputted line into an array (as noted by @DavisHerring).

If you're using POSIX shell (/bin/sh), the only type of array it supports is from the command line, $@. While you can use set to populate it, that's not necessary to simply loop through the fields represented by a user-inputted line:

IFS= read -r testOptions
for option in $testOptions; do
  case "$option" in
    # …
  esac
done

Note that the for command intentionally omits quotes around $testOptions (otherwise, you'd only find one option). This also lets you change the delimiter by setting IFS. If you do that, I recommend doing so with a local variable in a function, as other programs will get mangled with nonstandard $IFS values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.