0

I have 2 strings "planning1" and "planning2" , I have to check if both the strings are present (whole and complete) in the same array ("active","planning","delete")

expected output: if both the strings present -> echo "true" even if one of the string is not present in the list then -> echo "false"

I have previously tried , but it does not work as expected

#!/bin/bash
declare -a test_array
test_array=( "active" "teardown" "planning" )
[[ " $test_array " =~ " active " ]] && [[ " $test_array " =~ " planning " ]] && echo 'yes' || echo 'no'
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Where are those arrays coming from? Are they some kind of text content that can be read using `grep` or `awk` or so? – Dominique Aug 25 '21 at 14:18
  • Your title does not match your description. – Paul Hodges Aug 25 '21 at 14:26
  • @Dominique it is a static array, I can define it.but the two strings come from awk eg: export data_1=($(git diff-tree -p $CI_COMMIT_SHA | grep state | awk -F ": " '{print $2}')) stiring1= data_1[0] , string2=data_1[1] – 1DA16CS151_V.J.Harshita Aug 25 '21 at 14:28

1 Answers1

0

This is a case where interpolation of the entire array to a single string might be handy.
You can do that with * as the subscript.

edit

Added delimiting spaces around the array and the pattern.
You could make this more precise, and modifying $IFS may change the bahviour - unless quoted, Bash splits input to words based on $IFS, which defaults to whitespace but you can modify it.

$: ray=( "active" "teardown" "planning" )
$: [[ " ${ray[*]} " =~ ' active ' && " ${ray[*]} " =~ ' passive'  ]] && echo found || echo no
no
$: [[ " ${ray[*]} " =~ ' active ' && " ${ray[*]} " =~ ' planning ' ]] && echo found || echo no
found
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36