bq tool, bat file | escaping characters not workin

2019-06-28 02:38发布

问题:

Using bq tool I have no problem escaping the > operator with a caret ^:

bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event^>1"

However, when I CALL the exact same command through a bat file whe whole thing breaks down.

call bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event^>1"

I understand call is the problem here. I can't remove it, since I need to run additional commands after it (bq extract and gsutil cp). I've tried adaptations of what is shown on Escape user input in windows batch file, to no avail.

What's wrong here? Thanks in advance.

回答1:

I suppose bq.cmd itself contains something like this

set param1=%1
set SQL=%~2
python bigQuery.py --%param1% "%SQL%"

So the line set SQL=%1 requires escaping the special characters.

But when you use CALL, the batch parser has an additional phase of escaping but before it also has a phase of doubling all carets!

So your string in call bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event^>1" is converted to "SELECT x FROM [presentation_dim.dim_events] WHERE event^^>1"

I think there is no solution with only carets to solve this problem.

But you can avoid this by simply defining a variable containing one caret

set "caret=^"
call bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event%%CARET%%>1"


回答2:

Excuse me. I think there is a confusion here.

When a parameter is enclosed in quotes, it may include special Batch characters with no need to escape they; the only problematic case is when Delayed Expansion is enabled and the parameter include an exclamation-mark or caret. For example, this line works correctly:

bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event>1"

This way, the same line with a CALL command also works correctly:

call bq query "SELECT x FROM [presentation_dim.dim_events] WHERE event>1"

If you escape the greater-than sign this way ^> and don't use CALL, the character passed is the same ^> that is processed as a single > in bq.bat. However, if you use CALL, then the escape is duplicated and ^^> is passed.

Conclusion: Don't escape the > character.