Issue
I am building a script (linux bash) where I need to pass a certain number of arguments, it can be 7 or 8 arguments.
If I pass 7 arguments I will do it like this:
my_script.sh
!/bin/bash
arg1=$1
arg2=$2
arg3=$3
arg4=$4
arg5=$5
arg6=$6
arg7=$7
#Do other stuff after this
and I run it like this: ./my_script.sh 1 2 3 4 5 6 7
I want to be able to add an optional 8th parameter into this script. So the idea is to run the script with this 8th parametes sometimes and the rest of the time only with 7 parameters. How can I do this?
Solution
You could do something like this:
if [[ $8 != '' ]]
then
# do something
fi
Also, if you do
arg8=$8
and if there is no eighth arg, arg8 will just remain empty.
Answered By - develc Answer Checked By - Willingham (WPSolving Volunteer)