-1

Currently in Rhel6.5 bash-4.1.2-15 package is used and =~ operator is not supported in this version. This problem is resolved by adding shopt -s compat31 in shell scripts but I don't want to take this commands into all shell scripts.
There are lot of shell scripts are used in my project.

Please provide a solution.

whoan
  • 8,143
  • 4
  • 39
  • 48
yogesh
  • 25
  • 5

1 Answers1

1

=~ is absolutely supported in that version of bash. The behaviour of quoted arguments to the operator just changed.

compat31 makes bash use the old behaviour instead of the new behaviour.

So if you don't want to go stick that into all of your scripts then you get to update your scripts to use the new behaviour of the =~ operator.

See this question and answer for explanation of the change.

Community
  • 1
  • 1
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • There are a lot of shell scripts in which =~ operator is used. Please provide one solution. – yogesh Dec 22 '14 at 04:31
  • 1
    Solution to what? you haven't given an example of the problem. You've just stated that `compat31` "resolved" the problem. Show us the lines in question and we can help you fix the compat issues. But the issue is almost certainly an issue of quoting or unquoting the RHS of the `=~` operator in your script as appropriate. – Etan Reisner Dec 22 '14 at 07:06
  • I am using =~ operator in many shell scripts. for example `if [[ "$3" =~ "^[0-9]+$" ]] ; then abc[$i]=$3 else hsexit "${ERROR_DETECT_INTERNAL_ERROR}"` Execution is goes into else part because compatibility issue in this package. If shopt -s compat31 command is added in this script, it works fine. So problem is that i don't know how many places are using same operator in all shell scripts. If it is possibile to resolve this problem? – yogesh Dec 26 '14 at 05:10
  • 1
    The test `[[ "$3" =~ [[ "$3" =~ "^[0-9]+$" ]] ]]` in older versions of bash considers `[[ "$3" =~ "^[0-9]+$" ]]` to be a regular expression. Newer versions of bash consider that to be a literal string. That is the change in question and what the `compat31` option changes (among other things). So, as I said, you either get to continue using `compat31` or you get to go through your scripts and audit every usage of `=~` in them for quoted RHS regular expressions. – Etan Reisner Dec 26 '14 at 12:54
  • Which approaches is best for this bash incompatibility? 1. Add compat31 in all shell script. 2. Change code according to bash 4. – yogesh May 07 '15 at 12:19
  • Fixing the code for modern bash versions is clearly preferable to forcing backwards compatibility. There is no guarantee how long `compat31` is going to work going forward. That said using `compat31` is clearly simpler and easier as a fix. I would probably add `compat31` if things need to start working again **immediately** and schedule a middle-term todo item to review the scripts and update them to remove the need for `compat31`. If you have time now though I would just fix them. – Etan Reisner May 07 '15 at 13:51