0

I am trying to write a bash script that will tell whether two strings are of similar value. I have produced this bash script:

#!/bin/bash
value="java.lang.NullPointerException"
if [[ "java.lang.NullPointerException" = "$value" || "java.lang.NullPointerException" == "$value"* ]]; then
   echo "Match"
fi

Basically what I want to achive, is that if two strings are of equal value or a very similar either side but with matching text in the middle then echo "Match".

I have tried a number of resources but can't get this example to work. I have taken a look at:

  1. In bash, how can I check if a string begins with some value?
  2. How to test that a variable starts with a string in bash?
  3. https://ubuntuforums.org/showthread.php?t=1118003

Please note these values would eventually come from a text file and so the values will be in a form of variables. I have tried different approaches, but don't seem to get it working. I just want to get this if statement working. It works for matching text but not for values either side. Value could be "java.lang.NullPointerException: Unexpected" or "Unexpected java.lang.NullPointerException".

Community
  • 1
  • 1
Rajiv Jain
  • 13
  • 1
  • 5
  • You've got your operands order wrong : `in="test this"; [[ "$in" = "test"* ]] && echo ok` works fine (and don't need an additional `=` test) ; it's the shortest value that must be used as a pattern with a `*` – Aaron Apr 27 '17 at 13:51
  • Thanks. That worked. – Rajiv Jain Apr 27 '17 at 14:30
  • You're welcome ! I suggest you delete your question as it is now solved and was just about an incorrect implementation of the answers to the questions you linked to. – Aaron Apr 27 '17 at 14:51

2 Answers2

0
#!/bin/bash
value="java.lang.NullPointerException" #or java.lang.NullPointerException: Unexpected
if [[ $value == *"java.lang.NullPointerException"* ]]; 
then
   echo "Match"
fi
tso
  • 4,732
  • 2
  • 22
  • 32
  • While this might be the answer to the question, and the one who asked might be contempt with it, it would be nice if you could add some explaination on what you did, so it also helps further readers, that may stumble across this question. – derM - not here for BOT dreams May 03 '17 at 06:57
  • @derM It is a one-liner! It should be quite easy to find the explanation in the [Bash manual](http://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b) on [Stackexchange](http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts) or in the [Bash FAQ](http://mywiki.wooledge.org/BashFAQ/031). – ceving May 03 '17 at 07:38
  • 1
    @der: `s/contempt/content/`! – Toby Speight May 03 '17 at 08:04
  • RTFM? If it would be that obvious, this question is not needed at all. Someone still asked it which basically implies: it is not that obvious. The links might help, but on *StackOverflow* it is appreciated if all necessary details are added in the answer itself. As far as I can see, the OP was unclear about the fact, that you can prepend the search string with a `*`, and that this star accepts `0` characters, too. – derM - not here for BOT dreams May 03 '17 at 08:10
  • @TobySpeight: You are right of course! One letter, major difference. – derM - not here for BOT dreams May 03 '17 at 08:12
0

A simple and portable (POSIX compliant) technique for wildcard matching is to use a case statement rather than if. For your example, this would look something like

#!/bin/sh
value="java.lang.NullPointerException"
case "$value" in
*java.lang.NullPointerException*) echo Match;;
esac
Toby Speight
  • 27,591
  • 48
  • 66
  • 103