OPS102: Windows Scripting
Windows vs. Linux Scripting
- Windows shell scripting is similar to Linux but with different syntax due to different technical heritages.
An Abundance of Shells
-
Linux Shells:
bash: Widely-deployed shell on Unix-like systems.
sh: Original Unix shell (Bourne Shell).
ksh: Korn shell.
csh: C shell with C-like syntax.
fish: Friendly Interactive shell.
zsh: Similar to ksh.
-
Windows Shells:
CMD: Traditional Windows shell with expanded features from DOS.
PowerShell: Combines scripting with object-oriented programming. Pre-installed but disabled by default on non-server systems.
- Focus is on CMD shell scripting in this course.
Cross-Platform Scripting
- Bash/Zsh: Available on Linux and Mac, and as add-ons for Windows.
- PowerShell: Available on Windows by default and on Linux and Mac via third-party options.
Other Interpreted Languages
- Python and Perl: Suitable for cross-platform development.
- Shell scripting is ideal for process control but not for advanced processing algorithms.
Windows "Shell Scripts" vs. Batch Files
- Early scripts known as batch files for non-interactive batch processing.
- Current shell scripts can still use
.bat or .cmd extensions.
Basic Requirements for Shell Scripts
- Create a text file: Use a text editor like Notepad.
- Shell association via filename extension:
- CMD scripts:
.cmd or .bat
- PowerShell scripts:
.ps1
- Appropriate permissions: Default permission to read is usually sufficient.
Command Echos
- Windows displays each command before execution.
- Suppress display with
@ in front or echo off command.
A Basic Script Example
- Example script using
echo and date:
@echo off
echo The current date is:
date /t
Variables
Setting a Variable
- Use
set keyword with variable=value format.
- No spaces around
=.
- Variables are untyped.
Accessing a Variable
Quoting
- Quoting includes the quotes in most cases.
- Not required when assigning a string with spaces.
Carat Symbols
- Use
^ to escape special characters.
Environment Variables
- All variables are environment variables.
- Use
set command to view current variables.
Common Environment Variables
- Examples include
CD, TIME, DATE, ERRORLEVEL, etc.
Reading Variable Values from STDIN
- Use
set /p to prompt user for input.
Arithmetic
- CMD performs integer arithmetic.
- Use
set /A for arithmetic operations.
Conditional Logic: IF/ELSE
- Use
IF to control command execution based on tests.
- Types of tests include file existence, string equality, numeric comparisons, and variable definition.
- Negate tests with
NOT. No AND or OR operators available.
Script Parameters
- Access with
%0, %1, %2, etc.
- Use
shift to manipulate parameter list.
Looping
- Use
FOR to iterate over files or parameters.
EnableDelayedExpansion allows variable updates within loops.
Loop through a List of Files or Parameters
- Iterate over filenames, patterns, strings, or parameters.
Example of Looping
- Example loops through file deletion choice and integer counting.
Examples
- Integer vs String comparisons.
- Coin flip example using random integer.