Tuesday, June 11, 2013

Windows Basics CMD Shell & Others

1.  Frequently-used Commands

General Notes: The commands are NOT case-sensitive (because the legacy DOS is not case-sensitive). In general, Windows/DOS Operating System is not case-sensitive, but Unixes are. Most of the programming languages such as C/C++/Java, with origin in Unix, are case-sensitive.

1.1  Help

Help: To list all the available commands, for example,

1.2  Directory- and File-related Commands

Other frequently-used directory- and file-related commands include:
  • del filenames: Deletes files.
  • ren current-filename new-filename: Renames a file.
  • mkdir directory-name: Creates (or make) a sub-directory under the current working directory.
  • rmdir directory-name: Removes or deletes the sub-directory.
  • copy filename new-filename: Copies file.
  • xcopy|robocopy: Copies (or Robust copies) files and directory trees.

1.3  Environment and Local Variables

Environment variables are global system variables accessible by all the processes running under the Operating System. Environment variables are useful to store system-wide values such as the directories to search for the executable programs, the OS version, and the location of Windows binaries. (Windows registry was later created to store the ever-growing data needed for the applications in a more efficient manner.)
A process could access all the environment variables, it could also maintain its own local variables, which is available to itself only.

1.6  Redirection Operators and Filtering Commands

By default, the output of a command goes to the screen (called STDOUT), and the input of a command comes from the keyboard (called STDIN). You can use a redirection operatorto redirect input and output from/to a file or another command:
  • > (output redirection): Writes the output to a file (or a device such as printer), instead of the screen (STDOUT).
  • >> (output append redirection): Appends the output to a file, instead of the screen.
  • < (input redirection): Reads the input from a file or a device, instead of the keyboard (STDIN).
  • | (pipe): Pipes the output of one command into the input of another command.
An output redirector '>' involves a program and a sink. An input redirector '<' involves a program and a source. A pipe '|' involves two programs.
These filtering commands work with redirection operators for specifying the input and output:
  • find string: Searches for the specified string.
  • sort: Sort the lines alphabetically.
  • more: Displays one screen at a time and wait for a key-press to display the next screen. (It is useful in the old days when screen output is not buffered, but I find it annoying nowadays.)
For examples,
Prompt> more < abc.txt
Prompt> find "Hello" < abc.txt
Prompt> find "Hello" < abc.txt > out.txt
Prompt> find "Hello" < abc.txt | more
Prompt> sort < xyz.txt
Prompt> find "John" < xyz.txt | sort > out.txt
Display the given file one screen at a time.
Find the specified string from the given file.
Find the specified string and write the results to another file.
Display the result one screen at a time.
Sort the lines alphabetically, output to screen.
Find the string from file, sort the output, write result to file.
Notes: Pipe '|' is used to pipe into another command (or process); output redirection '>' is used to send the output to a file (or device such as printer) instead of STDOUT.

2.2  Symbolic Link (SymLink)

In Windows Vista/7/8, you can create a symlink to a file or a directory (similar to Unix), via command "mklink". Symlink could provide great convenience in many situations.
You need the "Create Symbolic Link" privilege to run "mklink" command, which only the administrators possessed by default. Start a CMD shell with administrator right ("run as administrator") to run the "mklink" command..
> help mklink
// Make a directory symlink called mysql to mysql-5.0.28-win32
> mklink /D mysql mysql-5.0.28-win32
symbolic link created for mysql <<===>> mysql-5.0.28-win32
> dir
10/14/2012  12:45 AM    <SYMLINKD>     mysql [mysql-5.0.28-win32]
Symlink is different from shortcut, which was available in the earlier Windows.
See here for Complete Guide to Symbolic Link in Windows
Symbolic links are basically advanced shortcuts. You can create symbolic links to individual files or folders, and then these will appear like they are stored in the folder with the symbolic link even though the symbolic link only points to their real location.
There are two types of symbolic links: hard and soft. Soft symbolic links work essentially the same as a standard shortcut.  When you open a soft link, you will be redirected to the folder where the files are stored.  However, a hard link makes it appear as though the file or folder actually exists at the location of the symbolic link, and your applications won’t know any different. Thus, hard links are of the most interest in this article. (duplicate PCB)

3.  CMD Shell Language and Batch Files

Batch files, which employ a legacy scripting language, possesses very simple, primitive and clumsy syntax . For examples,
  • The names and identifiers are not case-sensitive.
  • All variables belong to the type string. There is no numeric type, and no arithmetic operations.
  • Only simple and primitive flow control constructs (if-elsegoto, and call) are supported. Non-structured goto's are used extensively, which make the script hard to read and understand. (A for construct is available to process each file from a set of files.)
The primary purpose of batch files is to automate a series of commands, instead of general programming. 

3.1  Batch Commands

The frequently-used commands: 
  • rem message: comment 
  • pause: Suspends execution of the batch program and displays the message "Press any key to continue . . .";
  • echo message: Displays the specified message. To display a blank line, use "echo.".
  • echo on|off: When echo is on, all batch commands are echoed on the screen. When echo is off, only echo commands are displayed@echo off disables display for this echo command and subsequent batch commands. Echo is usually turned on during program testing and development, and turned off in production.
  • echo %variable-name%: Displays the value of an environment variable.
  • ctrl+c: to stop the batch file.
  • ctrl+s: to suspend the batch file.

3.2  Variables

All the variables belong to the type string. The names and commands are not case-sensitive.
In a batch file, you can reference an environment variable using the syntax %variable-name%. For example, echo %USERNAME%.
The command-line arguments can be referenced via variables %0 to %9 from right-to-left, where %0 is the name of the batch file. For example,
Prompt> process -o abc.out abc.in
The above command invoke a batch file Process.bat with command-line arguments. The command-line arguments can be referenced inside the batch file as follows:%0=Process%1=-o%2=abc.out, and %3=abc.in. To reference more argument, you need to use the command shift, which shifts the value of %1 into %0%2 into %1, ..., %9 into %8, and the next argument into %9.
  • setlocal
  • endlocal

setlocal and endlocal: The command setlocal begins localization of environment changes in a batch file. That is, changes to environment variables made after setlocal are local to this batch file. The command endlocal restores the previous environment settings. An endlocal is issued implicitly at the end of a batch file.

3.3  Program Control Constructs

if [not] exist filename command
if [not] "string1"=="string2" command
if [not] errorlevel n command
if [not] ... (command1) else (command2)
Execute command if [if not] error-level is equal or greater than n
(Error code is often set to 0 if no error occurs, 1 and higher otherwise.)
goto label-name
Jump to the command at the specified label-name
Define a label as target for goto
for variable in (set) do command
for %f in (*.doc *.txt) do type %f
Runs a specified command for each file in a set of files
type out all .doc and .txt
call filename arguments
start program-name
prompt message
Call another batch file or program with arguments, and returns to this batch file
Start a separate cmd session to run a specified program or batch file.
Terminate this batch file, and start the specified program (or batch file)
Put up the prompting message.
The command call can be used to call another batch file or program. The control returns to the next statement following the call command, after the called program is completed (similar to calling a subroutine in programming languages). For example, suppose "process.bat" is to be used in a batch file. "call process" executes the"process.bat", and returns control to the next statement. On the other hand, "process" alone (without call) terminates the current batch file and executes the "process.bat".

3.4  Examples

Below is a sample batch file extracted from "startup.bat" for starting the Tomcat server. The script checks if environment variable CATALINA_HOME is defined, else set it to the current directory or the parent directory, which contains "bin\catalina.bat". It then packs all the command-line arguments into variable CMD_LINE_ARGS, and call "catalina.bat start CMD_LINE_ARGS".
@echo off
if "%OS%" == "Windows_NT" setlocal
rem Start script for the CATALINA Server
rem Guess CATALINA_HOME if not defined
set CURRENT_DIR=%cd%
if not "%CATALINA_HOME%" == "" goto gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo CATALINA_HOME environment variable is not defined correctly
goto end
set EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat
if exist "%EXECUTABLE%" goto okExec
echo Cannot find %EXECUTABLE%
goto end
rem Get remaining unshifted command line arguments
if ""%1""=="""" goto doneSetArgs
goto setArgs
turn off message display
localized environment
%cd% gives the current directory
if CATALINA_HOME defined, goto gotHome
else set to current directory
if startup batch file found, goto okHome
else set CATALINA_HOME to the parent directory
"catalina.bat" found
append next argument behind
ready to start the server
"catalina start cmd_line_args" 


No comments:

Post a Comment