2

I have a shell script which gets executed. In the script I have:

if [[ $variable == *something* ]]; then
   do something
fi

My issue here is that running the script returns "[[ not found".

From reading other answers, it looks like not all shells recognize double brackets, however, I am not sure what the solution is, what is the equivalent of that code?

user220755
  • 4,358
  • 16
  • 51
  • 68
  • Are you executing this under bash? Add #!/bin/bash to the top – frankc May 22 '12 at 13:51
  • When I added #!/bin/bash to the top, it goes executing forever. I do not have control over executing the script, I simply put it somewhere (on a webpage) and it gets picked up and executed. – user220755 May 22 '12 at 13:52
  • 1
    take a look at [http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts](http://stackoverflow.com/questions/669452/is-preferable-over-in-bash-scripts) and [http://mywiki.wooledge.org/BashFAQ/031](http://mywiki.wooledge.org/BashFAQ/031) this may help you – dwalter May 22 '12 at 13:55

3 Answers3

3

It sounds like your shell is POSIX sh, which does not have the [[ builtin. Possible solutions include translating it to an expression that works for sh or changing the shebang line to be #!/bin/bash. Changing the shebang is generally the best solution.

You may have a POSIX sh if you're on Ubuntu, they use dash.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
  • When I added #!/bin/bash to the top, it goes executing forever. I do not have control over executing the script, I simply put it somewhere (on a webpage) and it gets picked up and executed. – user220755 May 22 '12 at 13:54
  • 2
    @user220755: Then you have an infinite loop in the script. The shebang line would not by itself make it run infinitely. You should consider opening a new question with the script to find out where the problem lies. – Daenyth May 22 '12 at 13:55
  • 1
    Changing the shebang is generally the *worst* solution. Making the script portable is far more useful. – William Pursell May 22 '12 at 14:58
  • @WilliamPursell: I'd have to disagree. At this point in time, bash is extremely portable for most users. If you're in the small group where that's not enough, you'll know. Most scripts that people write are rendered non-portable by the third party utilities they call, so why make them less readable and harder to maintain so that they can operate on a system they never were intended to be used with? – Daenyth May 22 '12 at 16:03
  • @Daenyth Making things work on systems they were never intended to be used with is an extremely important design goal. A tool is most useful if people use it in ways the designer never intended. – William Pursell May 22 '12 at 16:10
1

I know only single brackets, did you try it with only one bracket each? otherwise you could use test instead.

Herm
  • 2,956
  • 19
  • 32
  • you mean test [ $variable == *something* ]; ? I am not sure what "test" does? – user220755 May 22 '12 at 13:53
  • `test`is the script equivalent to [ ], see http://en.wikipedia.org/wiki/Test_%28Unix%29 for more info and some examples – Herm May 22 '12 at 13:55
  • Glob-style comparisons don't work inside single square brackets. The expression gets expanded to a list of matching filenames. – Dennis Williamson May 22 '12 at 14:24
  • `test "$variable" = something`, rather; the quotes are important, and supporting `==` is a GNU extension and not portable. Also, this won't test for glob matching in the way that the `[[` `]]` version does. – Charles Duffy May 22 '12 at 16:54
1

POSIX-standard solution:

if test echo "${variable}" | grep -q 'something' 
then
   # do something
fi

that will ALWAYS work :)

djhaskin987
  • 9,741
  • 4
  • 50
  • 86
  • 1
    +1, but not sure I would say it will always work. In particular, the '*' is likely to be globbed ;) – William Pursell May 22 '12 at 14:59
  • -1; `test` doesn't support glob matching, so this won't _ever_ work in the same way that the bash equivalent does. – Charles Duffy May 22 '12 at 16:53
  • @CharlesDuffy : It doesn't have to, since `if echo "${variable}" | grep "glob"` works even better since you can use regular expressions. You see, I interpreted `*something*` not to be a glob expression above, but just a placeholder for ``. It is true it can't do globs, but it is POSIX standard, and with the advent of DASH, etc., that's getting more important. – djhaskin987 May 23 '12 at 14:26
  • @djhaskin987 I _do_ interpret `*something*` as a glob -- to interpret part of a question as having something other than its literal meaning in the technical context in which it's given is wishful thinking in the absence of explicit clarification. As for "the advent of dash", that only matters if people use `#!/bin/sh` for their shebangs, which they shouldn't do if they want features not present in POSIX sh. – Charles Duffy May 23 '12 at 15:34
  • Thanks for the comments. I have edited the solution to omit globbing, which will not work with test. This should produce the desired effect asked for by the user, and now it **will** always work. – djhaskin987 Aug 08 '12 at 23:22