Tool for Shell systax errors in windows? - windows

I am writing my shell scripts using notepad++ editor in windows xp but I can not check for systax error in this editor. Is there any tool which can check the syntax of the shell scripts in windows xp?
Thanks

Try Shel script editor for windows http://www.editrocket.com/features/shellscript_editor.html

If you mean batch files, then it's getting fairly difficult. My syntax highlighter can show me some things, e.g. using %f instead of %%f but not much else I could do wrong. That is mostly because the syntax for batch files isn't really described by a formal grammar but instead parsed in a more ad-hoc manner.
Just learn to write correct code, in that case. No editor will be of much help, especially given the subtleties that can arise with batch files. Or do you see the error in the following code?
#echo off
for %%x in (World,Moon) do (
echo Hello %%x :-) - A good morning to you.
)

Related

Use of Windows undefined environment variable?

Here's a simple but puzzling question.
For an undefined Windows environment variable, abc for example
In the Command Prompt window ECHO [%abc%] results in [%abc%]
But in a .CMD batch file ECHO [%abc%] results in []
Why the difference? I've researched the ECHO command and can't find anything about this. I'm concerned about where else this subtle difference might apply.
Really good question! Confusing huh?
There are actually two distinct parsers used to parse batch scripts and command line commands.
Quote from this excellent answer:
BatchLineParser - The parser inside of batch files, for lines or blocks
CmdLineParser - Like the BatchLineParser, but directly at the command prompt, works different
The key difference is in the first phase of parsing, particularly the extension of %var%:
In BatchLineParser if var does not exists will be replaced with nothing, in CmdLineParser if the var isn't defined, the expression will be unchanged.
So why did someone design it this way? I have absolutely no idea.

Shebang to embed <*your language*> in Windows BAT file

How do you embed scripting language inside BAT file in Windows?
Effectively I am after Unix shebang for Windows: put JS/C#/Python/Java text into a BAT file, have a small header on top then have it compiled/run by just running the container BAT file.
Some scripting engines are guaranteed to exist: VBScript, JS, MSHTA/HTML — are there good shebangs for those?
Others are technically optional, but may be expected: C#, VB.NET, PowerShell, node.js, Python — any clues as to how to wrap those with a shebang?
Finally, there might be a way to smuggle a compiled binary within a BAT file, and shake/goad OS into running it. Any ideas how to do that?
Two useful Javascript shebangs I've come across. Those are crafted for CSCRIPT, MS JScript guaranteed to be bundled with Windows. Can be trivially retargeted for node.js too -- if you're certain node.js would be on the path at runtime.
DARTH shebang, called after the /*-*\ smiley:
title /*-*\&#cscript/nologo /E:jscript %~f0 %*&#goto:EOF&:*/=0
WScript.Echo('OK');
Stealth clubman shebang, called after the scary man with a club emoticon:
0</* :
#cscript/nologo /E:jscript %~f0 %*&#goto:EOF&:*/0
WScript.Echo('OK');
The tradeoff between those two is whether we make it apparent the script is shebanged at runtime or have a more conspicuous header. DARTH would print one line to console, and set the console title. Stealth clubman would take two lines at the JS header.

Cross-platform command line script (e.g. .bat and .sh)

I noticed that Windows 7 enables to execute .sh files as if they were .bat files. That got me wondering whether it is possible to write a .sh file such that it can be executed in Windows and Linux (let's say bash).
The first thing that comes to my mind is to fabricate an if-statement such that Windows and Ubuntu can deal with it and jump into the according block to execute plattform-specific commands. How could this be done?
Note: I know this is not good practice. I also know that scripting languages like Python are far better suited to solve this problem than a mixed-syntax command line script would be. I'm just curious...
You could use this:
rem(){ :;};rem '
#goto b
';echo sh;exit
:b
#echo batch
It's valid shell script and batch, and will execute different blocks depending on how it's run.
Modify the echo and #echo lines to do what you want.
AFAIK, you can't directly run .sh files from Windows' cmd.exe. For that you'll need a *nix emulation layer to run the shell. Check out Cygwin or Msys/MinGW

shell script - is it okay as a .bat file? or cygwin more suited?

I found the following code on google for shell script. If I get rid of the bin/bash part, can I use it on Windows as .bat file? Or should I install cygwin?
#!/bin/bash
VIDEOS_DIR=/PATH/TO/JOOMLA/hwdvideos/uploads
YAMDI=/usr/local/bin/yamdi
for i in $VIDEOS_DIR/*.flv
do
$YAMDI -w -i $i -o ${i}.tmp
done
No. That is using specific Bash syntax that isn't supported by windows batch files. Your best bet is to using cygwin or convert this to the appropriate batch file syntax.
No; batch files use different syntax.
You can convert it to Windows command-line syntax or run it in bash.
You'll need Cygwin. This is strictly bash script content. You could rewrite it to work as a batch file, but yamdi wouldn't work from it. (I don't know what yamdi is, but it's most likely a Linux application.)
No, as other commenters have said.
But, just for fun, here's the NT Command Script equivalent.
#echo off
set VIDEOS_DIR=c:\path\to\joomla\hwdvideos\upload
set YAMDI=c:\usr\local\bin\yamdi.exe
for /r %VIDEOS_DIR% %%i in (*.flv) do (
%YAMDI% -w -i %%i -o %%i.tmp
)
Significantly different syntax, as you can see.

Preprocessor to add functionality to Windows's CMD?

I need to do a fair bit of scripting in my job as a SQL Server DBA. Sometimes, I need to deploy a fix script to a very restricted environment, where the only option for scripting may be DOS Batch. In one such environment, even VBScript/WSH isn't a possibility, let alone PowerShell. Anyone who has written enough batch files on DOS and Windows knows that it's very limited and a huge PIA when you need to do anything too complicated. This is especially true for folks who have worked with Unix shell scripting, Perl, Tcl, Python, Ruby, etc.
A possible solution to this would be a CMD preprocessor that would add some of the useful functionality from more capable scripting languages. I've tried to find such a utility, but so far I've had no luck.
Which finally leads to my question: is anyone aware of a such a CMD preprocessor? If not, what functionality would you like to see in one?
Addendum:
If you're unfamiliar with the idea of a preprocessor see this Wikipedia entry.
To clarify, I'm thinking of a tool that would add features like:
Functions
Backtick (`) ala Unix shell
...and possibly others. Those are two features I've wished CMD had and can think of a way to implement them with a CMD preprocessor. Functions could be implemented with env vars and GOTO/labels; backticks by piping to a temp file and using set /p =< to read in the result to an env var.
You can already achieve these same ends, but it gets to be very tedious and verbose- which is how I came to the idea of having a preprocessor handle the boilerplate for features like those.
Example
Using the example of backticks, here is an example of unprocessed code from my hypothetical Batch++ and processed vanilla batch script, ready to be run by CMD.exe:
Batch++ Source (test.batpp)
copy `dir /b /s c:\ | find "CADR-README.htm"` \\srv01\users
Run it through the preprocessor
bpp test.batpp > post_test.bat
Resulting CMD/BAT code (post_test.bat)
dir /b /s c:\ | find "CADR-README.htm" > _bt001.tmp
set /p _BT001 =< _bt001.tmp
copy %_BT001% \\srv01\users
set _BT001=
del _bt001.tmp
I am not sure to interpret correctly your question. If you run in a controlled environment that doesn't allow you to run any scripting extension, how are you going to access such a preprocessor?
However, and in regard of the two features you request, you are OK with .BATs. Both features are supported by BAT processing in current Windows versions.
Functions: you have the extended CALL syntax, that supports parameter passing thru argument references %1 .. %9, and enhanced with expansion substitution using %~ syntax. Read HELP CALL.
Backtick: not sure what you want but, in the FOR /F command you may pass a backticked string to be run and its output captured. Read HELP FOR.

Resources