3

I have a shell script which take input parameters from command line. Below are only two options:

./test.sh 20180415 20180416 20180417 20180418 2
./test.sh 20180418 2

The first option takes 5 input parameters, out of which first four are in date format "YYYYMMDD" and I want to store those dates in array variable: sets_date=("$@") and the last parameter which is 2 I will store it in the different variable.

The second option takes 2 input parameters, in which the first one is in same date format and I still want to store that date in array variable and last parameter in the different variable. These are the only two options I will have.

#!/bin/bash

sets_date=("$@")
# store number in variable

Is this possible to do in a shell script? So either my shell script takes 5 input parameters or only 2 input parameters and there won't be any other combinations.

4
  • Can the last parameter be the first? It will save some writing. Commented Apr 22, 2018 at 8:54
  • yes it can be.. there is no restriction on that Commented Apr 22, 2018 at 8:56
  • Can the "2" be in the middle? Commented Apr 22, 2018 at 11:26
  • @glennjackman no it cant be in the middle but if it is in the middle I would like to exit and log an error message. Commented Apr 22, 2018 at 17:28

2 Answers 2

4

You could use array expansion technique to do this. First you just need to store elements from first the one before the last. The syntax for the first technique is simple, ${@:startpos:endpos}. We set the starting position as 1, since 0 would represent the name of the actual script under use. The end position is derived by reducing one from $# which represents the count of total positional arguments passed.

exceptLastElem=( "${@:1:$#-1}" )

and for the last element

lastElement="${@:$#}"

Putting this on a dummy function

ap() { echo "except last ${@:1:$#-1}" ; echo "last ${@:$#}" ; }

now calling the function with a number of arguments

ap 1 2 3 4
except last 1 2 3
last 4
Sign up to request clarification or add additional context in comments.

3 Comments

Couldn't have said it better myself. (That is, I was typing the same answer when you posted this. :) )
@Inian thanks for your explanation. Also let's say if number comes in the middle for whatever mistake done by someone else then is there any way to detect that and exit out of script?
@user1950349 : How do you expect to identify that? Something like a single digit argument in list of args, but not in the last? Exit in that case?
2

Read some, and it doesn't matter if the extra parameter is first after all. One option - read everything to the list as you did, then pop the last element

sets_date=($@)
extra=${sets_date[((${#sets_date[*]}-1))]}
unset 'sets_date[${#sets_date[@]}-1]'

Deleting last element was taken from Remove the last element from an array. Then to validate:

if [ $# != 2 ] && [ $# != 5 ]; then
    echo -E- wrong number of arguments
    exit 1
fi

for argument in "${sets_date[@]}"; do
    if ! [[ "$argument" =~ ^[0-9]{6}$ ]]; then
        echo "-${argument}- is not valid date"
        exit 1
    fi
done

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.