Om Sao
Om Sao

Reputation: 7673

Shellscript: Conditional operator: What is wrong in following shellscript if statement

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.?

Upvotes: 2

Views: 48

Answers (1)

Inian
Inian

Reputation: 85885

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

Upvotes: 2

Related Questions