I am looking for a bash alternative to write scripts. I came to know that Nim can do this.
https://github.com/nim-lang/Nim/issues/3301 says:
#!/usr/bin/env nim should work now.
I went through Nimscript page, and also some documentations. I could not find how to do it.
It would be helpful to have a complete guide on how to write and use the script.
Aside from putting that shebang line at the top of the file you want to run as a script, the file has to be saved as a NimScript file, namely use a .nims file ending.
someScript.nims
#!/usr/bin/env nim
echo "Hello from NimScript!"
echo defined(NimScript)
and then in your terminal:
chmod +x someScript.nims
./someScript.nims
and it should run just fine.
I use Linux. So, will the file extension be a factor here?
I have few more questions:
How do we take arguments?
How do we read stdin?
How do we write to stdout?
How do we pipe?
How do we use Linux commands like pwd / ls / cd / sed / grep etc inside the script?
Will '#!/usr/bin/env nim' take care of the compilation of the script?
I have a bit involved NimScript: https://github.com/kaushalmodi/nim_config/blob/master/config.nims
See if going through it and referring to docs helps you.
I use Linux. So, will the file extension be a factor here?
Yes, NimScript requires a .nims extension. Personally, I think nim whatever.nims should require the extension and nim e whatever.nims should support any extension, but the current behavior is to require .nims in both cases.
The nimscript module has procs that do what you want. You can use paramStr and paramCount to handle parameters. readLineFromStdin or readAllFromStdin for reading from stdin. exec to execute system commands. echo will write to stdout.
Will '#!/usr/bin/env nim' take care of the compilation of the script?
NimScript isn't compiled, it's interpreted. Nim includes a VM to support compile time evaluation (required for macros and such), and that VM is also used for NimScript. Not all Nim features are available from NimScript. See https://nim-lang.org/docs/nims.html for more details.