2

I recently upgraded my FreeBSD box and now pythonbrew seems to be broken. It bails out of the .pythonbrew/etc/bashrc file on this line:

PATH_WITHOUT_PYTHONBREW=$(printf "$PATH" | awk -v RS=: -v ORS=: "/${PATH_ROOT//\//\/}/ {next} {print}" | sed -e 's#:$##')

Gives the error:

awk: syntax error at source line 1
   context is
      >>> //home/myusername/. <<< pythonbrew/ {next} {print}
awk: bailing out at source line 1

That PATH_ROOT variable is

/home/myusername/.pythonbrew
CaptainThrowup
  • 919
  • 1
  • 7
  • 15
  • 1
    It seem for me that you try to read variable inside `awk` code. Use `awk` like this `awk -v RS=: -v ORS=: -v path=$path_root '$0==path {next...` – Jotne Mar 31 '14 at 18:15
  • Does `${PATH_ROOT//\//\\\/}` work? Though as @Jotne suggests using `-v` is likely a better plan. Though with `$0 ~ path` if you want the regex match and not equality. – Etan Reisner Mar 31 '14 at 18:25

1 Answers1

4
  1. Don't put variables in your printf formatting argument as that'll fail when your variable contains printf formatting charcters, e.g. '%'. So use printf "%s" "$PATH" instead of printf "$PATH".
  2. It is never a good idea to use double quotes to delimit an awk script as it introduces double quoting hell in the rest of the script. So use '/'"${PATH_ROOT//\//\/}"'/{...}' instead of "/${PATH_ROOT//\//\/}/{...}"
  3. It is almost never a good idea to allow shell variable expansion to provide parts of the body of your awk script dynamically as it introduces the potential for bizarre syntax errors. So use awk -v path_root="${PATH_ROOT//\//\/}" '$0 ~ path_root{...}' instead of '/'"${PATH_ROOT//\//\/}"'/{...}'.
  4. Rather than test for a condition and use next and then use a print if the condition isn't present, you can just test for the negation of the condition.
  5. When setting 2 variables to the same value (e.g. RS and ORS), it's clearest to set them together rather than separately.

So, all together, as a starting point your script would be:

PATH_WITHOUT_PYTHONBREW=$(printf "%s" "$PATH" |
    awk -v path_root="${PATH_ROOT//\//\/}" 'BEGIN{RS=ORS=":"} $0 !~ path_root' |
    sed -e 's#:$##')

assuming your PATH_ROOT manipulation makes sense.

It could be further improved but that should be enough to get rid of your error.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    Awesome, thank you! There is an extra single quote in there in front of $0 that I can't remove as it wants the edits to be at least 6 characters, but once that is removed it works great. I'll see about adding this as a patch to pythonbrew as well. – CaptainThrowup Apr 01 '14 at 14:38
  • You're welcome and I updated my script to remove that single quote. If you'd like to update your question with some samples of what "PATH" can contain and what you'd want "PATH_WITHOUT_PYTHONBREW" to be set to given those PATH settings, I'm sure we could show you how to do it trivially in a single awk command rather than invoking multiple commands and pipes. – Ed Morton Apr 01 '14 at 16:48