1

Following the tutorial https://www.gnu.org/software/gawk/manual/gawk.html i created a cmd file on windows that have a simple awk script

 #! /bin/awk -f

BEGIN { print "Don't Panic!" }

However windows doesn't accept the Shebang notation #! and throws me an error:

'#!' is not recognized as an internal or external command, operable program or batch file.

How can i make the file executable on windows cmd?

1 Answer 1

2

The "shebang" (sharp+bang) notation #! is very specifically a Unix/Linux kernel thing; when you use the exec(2) system call to execute a file, there's logic that will look to see if the file is a script and, if so, invoke the specified interpreter on it, instead of just expecting the file itself to be an executable binary. I don't think there's any analogous way to turn a script with an arbitrary interpreter into a directly-executable command on Windows using CMD.

What you can do is pick a file suffix (like .awk), associate that suffix with the awk executable in Explorer, and then use start (or double-clicking) to run awk scripts instead of just typing the bare filename.

But you want to turn an awk script into a command that you can run at the CMD prompt, the most reliable way would be to make a batch (.bat) file that runs awk on the file manually, which looks something like this (assuming the awk script file is in the same folder as the batch file):

@echo off
awk -f "%~dp0filename.awk"

Here's a working example built from your script:

C:\>echo %PATH%
c:\users\mjreed\bin;C:\windows\system32;C:\windows;...

C:\>type \users\mjreed\bin\hello.bat
@echo off
awk -f "%~dp0hello.awk"

C:\>type \users\mjreed\bin\hello.awk
BEGIN { print "Don't Panic!" }

C:\>hello
Don't Panic!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.