There's no way to step through a script to see how it would execute without actually doing that. In your example, there are no if
statements or loops. But in real scripts, there are often lots of conditional statements. Which branch will get taken will often depend on what happened when the shell ran the previous command. If it doesn't run the command, there's no way for the shell to know what output it would have generated or what the return code would have been, which the next conditional branch or assignment statement might depend on.
If the point is that you'd like to scrutinize a script and know what it does before you run it, that's not a bad idea. But realistically, about the best way to do that is by just browsing the file with less
or vi
or something similar.
Added
If you're developing a script and you're wanting to step through it the first time, testing the logic but not actually doing any damage if you've got a bug, the solution I'm often able to use is to modify only the statements that could do any damage by pasting an echo
onto the front.
This often works in typical real-life scripts because (a) generating the lists of items you'll iterate over or the value to which you'll set a variable can often be generated without changing the filesystem and (b) it's usually sufficient to assume that if you did run the command, it would succeed.
This will enable Drag & Drop to any script. You can place one of them in SendTo folder and use it afterwards.
Registry Export:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\ShellFile]
[HKEY_CLASSES_ROOT\ShellFile\Shell]
[HKEY_CLASSES_ROOT\ShellFile\Shell\Open]
[HKEY_CLASSES_ROOT\ShellFile\Shell\Open\Command]
@=hex(2):43,00,3a,00,5c,00,70,00,61,00,74,00,68,00,5f,00,65,00,78,00,74,00,5c,\
00,62,00,61,00,73,00,68,00,2e,00,65,00,78,00,65,00,20,00,2d,00,63,00,20,00,\
22,00,73,00,6f,00,75,00,72,00,63,00,65,00,20,00,24,00,30,00,3b,00,72,00,65,\
00,61,00,64,00,22,00,20,00,25,00,31,00,20,00,25,00,2a,00,00,00
[HKEY_CLASSES_ROOT\ShellFile\ShellEx]
[HKEY_CLASSES_ROOT\ShellFile\ShellEx\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"
The hex part actually is "C:\cygwin\bin\bash.exe -c "source $0;read" %1 %*"
which gets encoded in the export.
You probably will want to remove the read
after testing, so you can write scripts that just perform a task without leaving an open window. If you need this for single scripts, you can always add it add their end.
Use assoc .ext=ShellFile
after importing to link any file extension you want with this functionality. The DropHandler in this example works for Windows XP and Windows 7 (probably others too) and basically means "run the command, with all dropped filenames as arguments".
Use this as a script (echotest.ext
) to test basic functionality:
echo $0 $*;
Best Answer
That style of set command sets or unsets shell options. Confusingly - sets the option and + unsets the option.
option "e" makes the shell script error out whenever a command errors out. It's generally a good idea to have it enabled most of the time.
option "x" makes the shell print out commands after expanding their parameters but before executing them. Useful when debugging but can get overwhelming sometimes.
See https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html for more (that documentation is for bash but many of the features are common across many shells)