質問

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
役に立ちましたか?

解決

  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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top