rbees
rbees

Reputation: 35

Bash test if #- in string but NOT #-# or #-#-#

I have 5 different references to a file and locations in it, as in;

search  | contents
  #-    | some.htm#9-22                             called pat=
 #-#    | some.htm#1-some.htm#3                     called pat2=
#-#-#   | some.htm#21-some.htm#-some.htm#4          called pat3=
   ;    | some.htm#6-some.htm#13;some.htm#22-23     called pat4=
 else   | some.htm#

There are some 550 of these references in a csv data file.

The problem I am having is figuring out how to test the variable that contains the reference to see if it contains the search colum listed above. I do know that it contains special characters. I have not found a reference to searching for them in a test online.

  pat=[#-]
  if [[ $ALIYAH == $pat ]]; then
  SHIR1="$(echo "$ALIYAH" |awk -F \# '{print $1}')"
  START1="$(echo "$ALIYAH" |awk -F \# '{print $2}'|awk -F - '{print $1}')"
  END1="$(echo "$ALIYAH" |awk -F - '{print $2}')"
  return

Thanks

So I made the changes recommended below but have no joy. The test is not matching and so is falling through to the default and not processing as it should. The debug output

+ for ALIYAH in '"${arr[@]:2:11}"'
+ pat='[#-]'
+ [[ some.htm#11-38 == \[\#\-\] ]]
++ echo some.htm#11-38
++ awk -F '#' '{print $1}'
+ SHIR10=some.htm  # this is the fall through, it should fill SHIR1

So after some more searching I tried

if [[ "$ALIYAH" =~ [#-] ]]; then

and that stopped it from falling through

as I feared it is testing positive for patterns 2 and 3 also

Upvotes: 0

Views: 88

Answers (1)

Loedolff
Loedolff

Reputation: 172

Put quotes around the variable name:

if [[ "$ALIYAH" == "$pat" ]]; then
...etc...

Upvotes: 2

Related Questions