Learner
Learner

Reputation: 1968

Cannot understand Shell script with a regular expression

I am new to shell scripting. I have the following snippets and it is really complex. The more I look at it the more I get confused.

while test $# -gt 0; do
        [[ $1 =~ ^-c|--chartrelease$ ]] && { chartRelease="$2"; shift 2; continue; };
        echo "Parameter not recognized: $1, ignored"
        shift
done
: "${chartRelease:="default"}" 

Can any provide a simple explanation to clear this mistery? For example what is the purpose of $1 =~ ^-c|--chartrelease$

Upvotes: 0

Views: 56

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133528

Could you please go through following once, this is only for explanation purposes.

while test $# -gt 0; do                          ##Starting a while loop which traverse through all arguments provided to script.
[[ $1 =~ ^-c|--chartrelease$ ]] &&               ## Checking condition if  first argument is either starting with letter c OR ending with --chartrelease then only DO next action in {}.
{ chartRelease="$2"; shift 2; continue; };       ##Setting variable chartRelease to 2nd argument  then using shift will remove first 2 arguments and make 3rd argument as new $1, continue will go to next cycle of  while loop skipping all next statements from here.
echo "Parameter not recognized: $1, ignored"     ##Echo is printing that $1 is NOT recognized this will execute when above if condition is NOT TRUE.
        shift  ##Shift will make $2 as new $1
done                                             ##Closing while loop here.
: "${chartRelease:="default"}"                   ##In case variable ChartRelease is null then set its value as string default.

Upvotes: 2

Related Questions