2

Here is my shellscript if condition

if [[ $RBFilePath =~ \.js$ || $RBFilePath =~ \.json$ && ${#key_array[0]} = "JAVASCRIPT" ]]; then
Blah blah
blah blah

fi

What I want here is, if $RBFilePath ends with .js or .json AND First elemeny of array: key_array is equal to "JAVASCRIPT" then execute the if block.

But it is not executing. Can someone please help telling me issue.?

1 Answer 1

2

The statement is incorrect as it checks the length of the first element of the array

${#key_array[0]} = "JAVASCRIPT"

change it to

${key_array[0]} = "JAVASCRIPT"

which checks the value of the first element in the array.

e.g.

fooArray=('foo' 'bar')
printf "%s\n" "${#fooArray[0]}"
3
printf "%s\n" "${fooArray[0]}"
foo
Sign up to request clarification or add additional context in comments.

5 Comments

No No no.. How did I miss this basic :(
@OmSao: It happens to the best of us! ;)
One thing to confirm, Is using single Equal = for condition is best practice or should we use == ?
@OmSao: I would use == as a best practice
Ok, but both = and == works fine as I tested. Thanks @Inian !

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.