0

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$

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

6 Comments

in this line=> : "${chartRelease:="default"}" what is the purpose of :?
@Learner, it sets default value of chartRelease,see this link once stackoverflow.com/questions/4437573/bash-assign-default-value it will help you.
Thanks. I get the second colon but not sure why we need the first colon though
Opps sorry just saw the link. Thanks for the nice explanation
@Learner, your welcome, cheers and have a happy learning on SO.
|

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.