What is the reason for the error on running grep.exe (Unix version compiled for Windows) from a batch script?

Ratnesh Chandna

I tried to run grep.exe (Unix version compiled for Windows) from a batch script and got this error:

      0 [main] grep 7760 C:\grep.exe: *** fatal error - add_item ("\??", "/", ...) failed, errno 22
Stack trace:
Frame        Function    Args
000FFFFABB6  0018007215E (00180267E4A, 00180218E59, 00600010000, 000FFFF8B30)
000FFFFABB6  00180046E52 (000FFFF9B98, 000FFFFABB6, 00000000000, 00000000000)
000FFFFABB6  00180046E92 (000FFFF9BB0, 00000000016, 00600010000, 000003F3F5C)
000FFFFABB6  001800DEEFD (000FFFFCBB0, 000FFFFCE00, 001800CF158, 00000000000)
000FFFFCC00  00180128545 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFCCC0  001800474B5 (00000000000, 00000000000, 00000000000, 00000000000)
00000000000  001800460AC (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFFFF0  00180046144 (00000000000, 00000000000, 00000000000, 00000000000)
End of stack trace

This above error message is shown after running the following line in the batch script:

for /f "usebackq delims=" %%a in (`reg query "hklm\system\currentcontrolset\control\session manager\environment" /v Path ^| %~dp0grep -i directory_being_searched_for `) do set pathExists=true

This line of code might look complicated at first, but the grep command near the end is what's being executed eventually, which fails at some point.

What seems to be causing this failure?

If there's no easy solution, I don't mind workarounds.

Mofi

%~dp0grep should be enclosed in double quotes in case of path to grep.exe (= batch file path) contains a space or another syntax critical character from this list &()[]{}^=;!'+,`~ output by command interpreter itself on running in a command prompt window cmd /? on last page.

And the file extension .exe should be also appended to avoid the need to search for an executable by Windows command interpreter in batch file directory.

But the main problem causing the fatal error is most likely the string replaced in question by

directory_being_searched_for
  1. Is the string enclosed in double quotes?
  2. Does it contain regular expression characters like ? or * or .?
  3. And is the directory with path and contains therefore also 1 or more backslashes?

grep is ported from Unix to Windows. Therefore grep interprets a backslash as an escape character like C/C++/C#/JavaScript and many other programming and scripting languages and not as directory separator like Windows.

I don't have installed grep (which version downloaded from where?) and therefore can't reproduce this issue and find out the reason for the fatal error.

But perhaps the solution is using grep option -F to let grep interpret the search string pattern as fixed string instead of regular expression.

for /F "usebackq delims=" %%a in (`%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| "%~dp0grep.exe" -i -F "directory_being_searched_for"`) do set "pathExists=true"

But there are easier solutions as Windows has the console applications find and findstr for simple fixed string searches and simple regular expression searches.

Example with find:

for /F "usebackq delims=" %%a in (`%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| "%SystemRoot%\System32\find.exe" "directory_being_searched_for"`) do echo set "pathExists=true"

Operator | redirects the output of reg to handle STDOUT to the input handle STDIN of find. | must be escaped here with ^ to avoid being interpreted for command for which would result in an exit of batch processing because of a syntax error. Please see Microsoft article about Using command redirection operators for more details.

But this simple command line does not verify if the complete directory path is really included in environment variable PATH as defined in Windows registry. A better verification would be for example:

@echo off
setlocal EnableDelayedExpansion
set "PathExists=false"
for /F "tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path') do (
    if /I "%%A"=="Path" (
        set "SystemPath=;%%C;"
        set "SystemPath=!SystemPath:"=!"
        if not "!SystemPath:;directory_being_searched_for;=!" == "!SystemPath!" set "PathExists=true"
    )
)
echo Path exists: %PathExists%
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • find /?
  • for /?
  • if /?
  • reg /?
  • reg query /?
  • set /?
  • setlocal /?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Running MsTest.exe from Batch script

From Dev

Running MsTest.exe from Batch script

From Dev

Error in grep command to exit from a Unix Script

From Dev

Echo text with Unix line endings from a Windows batch (.bat) script

From Dev

Running MySql script from Cmd.exe in MS Windows 7

From Dev

Running jar file with argument from batch script windows 8

From Dev

Visual Studio build error "This version of NuGet.exe is not compatible with the version of Windows you're running."

From Dev

OpenVMS - batch to check if running latest version of script

From Dev

Convert a Unix ImageMagick script to a Windows batch file

From Dev

Stop command prompt from opening when running compiled .exe

From Dev

Error launching SDK manager: "find_java.exe is not compatible with the version of Windows you're running"

From Dev

Error launching SDK manager: "find_java.exe is not compatible with the version of Windows you're running"

From Dev

Read config.py file from a compiled python .exe script

From Dev

Error Running PowerShell Script From Windows form application

From Dev

This version of query.exe is not compatible with the version of Windows you're running

From Dev

Running a batch file (with blanks) from powershell script

From Dev

Running a batch file (with blanks) from powershell script

From Dev

running NodeJs script from WebStorm gives "spawn cmd.exe ENOENT" error

From Dev

running NodeJs script from WebStorm gives "spawn cmd.exe ENOENT" error

From Dev

Error Running Python Script from Batch File FileNotFoundError: [Errno 2] No such file or directory: '[]'

From Dev

.exe Generated by Cygwin not running directly from windows

From Dev

Running multiple cmd.exe processes from a batch file in Java

From Dev

Passing array as command line arguments when running exe from batch

From Dev

Executing parameterized .exe files via batch script (Windows command line)

From Dev

What might be the reason for the error "this feature is not enabled or not available in this version of quickbooks"

From Dev

Running an .exe script from a VB script by passing arguments during runtime

From Dev

Error handling using ERRORLEVEL in Windows Batch Script

From Dev

Extract version from XML with Windows Batch

From Dev

Grep / Find oracle table name from UNIX Shell / SQL Script

Related Related

  1. 1

    Running MsTest.exe from Batch script

  2. 2

    Running MsTest.exe from Batch script

  3. 3

    Error in grep command to exit from a Unix Script

  4. 4

    Echo text with Unix line endings from a Windows batch (.bat) script

  5. 5

    Running MySql script from Cmd.exe in MS Windows 7

  6. 6

    Running jar file with argument from batch script windows 8

  7. 7

    Visual Studio build error "This version of NuGet.exe is not compatible with the version of Windows you're running."

  8. 8

    OpenVMS - batch to check if running latest version of script

  9. 9

    Convert a Unix ImageMagick script to a Windows batch file

  10. 10

    Stop command prompt from opening when running compiled .exe

  11. 11

    Error launching SDK manager: "find_java.exe is not compatible with the version of Windows you're running"

  12. 12

    Error launching SDK manager: "find_java.exe is not compatible with the version of Windows you're running"

  13. 13

    Read config.py file from a compiled python .exe script

  14. 14

    Error Running PowerShell Script From Windows form application

  15. 15

    This version of query.exe is not compatible with the version of Windows you're running

  16. 16

    Running a batch file (with blanks) from powershell script

  17. 17

    Running a batch file (with blanks) from powershell script

  18. 18

    running NodeJs script from WebStorm gives "spawn cmd.exe ENOENT" error

  19. 19

    running NodeJs script from WebStorm gives "spawn cmd.exe ENOENT" error

  20. 20

    Error Running Python Script from Batch File FileNotFoundError: [Errno 2] No such file or directory: '[]'

  21. 21

    .exe Generated by Cygwin not running directly from windows

  22. 22

    Running multiple cmd.exe processes from a batch file in Java

  23. 23

    Passing array as command line arguments when running exe from batch

  24. 24

    Executing parameterized .exe files via batch script (Windows command line)

  25. 25

    What might be the reason for the error "this feature is not enabled or not available in this version of quickbooks"

  26. 26

    Running an .exe script from a VB script by passing arguments during runtime

  27. 27

    Error handling using ERRORLEVEL in Windows Batch Script

  28. 28

    Extract version from XML with Windows Batch

  29. 29

    Grep / Find oracle table name from UNIX Shell / SQL Script

HotTag

Archive