Showing posts with label Windows Commands. Show all posts
Showing posts with label Windows Commands. Show all posts

Saturday, 28 June 2014

Getting Acquainted with Variables

Getting Acquainted with Variables

In command-line scripting, what we commonly call variables are more properly called environment variables. Environment variables can come from many sources. Some variables are built into the operating system or derived from the system hardware during startup. These variables, called built-in system variables, are available to all Windows processes regardless of whether anyone is logged on interactively. System variables can also come from the Windows Registry. Other variables are set during logon and are called built-in user variables. The built-in user variables available are the same, no matter who is logged on to the computer. As you might expect, they are valid only during an actual logon session, that is, when a user is logged on.
You can see a listing of all the variables known in the current instance of the command shell by typing set at the prompt. In addition to the normal system and user variables, you can create variables whenever Windows is running, which is exactly what you’ll do when you program in the command shell. You define variables for the current instance of the command shell using the SET command and the following syntax:
set variable_name=variable_value
such as
set working=C:\Work\Data
set value=5
set string="Hello World"
Some variables, including system and user environment variables, have special meaning in the command shell. These variables include path, computername, homedrive, and many other important environment variables. One environment variable that you should learn more about is errorlevel, which tracks the exit code of the most recently used command. If the command executes normally, the error level is zero (0). If an error occurs while executing the command, the error level is set to an appropriate nonzero value. Error values include
  • 1  Indicates a general error
  • 2  Indicates an execution error, meaning the command failed to execute properly
  • 2  Indicates a math error, such as when you create a number that is too large for the command shell to handle
You can work with the errorlevel variable in several ways. You can check for a specific error condition, such as
if "%ERRORLEVEL%"=="2" echo "An error occurred!"
Or, you can use the following special syntax and check for a condition equal to or greater than the specified exit code:
if errorlevel 2 echo "An error occurred!"
Note 
You’ll see more on errorlevel and if statements later in the chapter in the section titled “Using Conditional Statements.”
When you are finished working with variables, it’s good form to dispose of them. You do this to free memory used by the variable and prevent problems or unexpected results if you accidentally refer to the variable in the future. To clear out a variable, you simply set the variable equal to nothing, such as
set working=

Now the variable is cleared out of memory and is no longer available.

Windows Commands for Redirecting Standard Input, Output, and Error

Redirecting Standard Input, Output, and Error

By default, commands take input from the parameters specified when they are called by the command shell and then send their output, including errors, to the standard console window. Sometimes, though, you’ll want to take input from another source or send output to a file or other output device such as a printer. You may also want to redirect errors to a file rather than the console window. You can perform these and other redirection tasks using the techniques introduced in Table 2-2 and discussed in the sections that follow.

Table 2-2: Redirection Techniques for Input, Output, and Errors
Redirection Technique
Description
command1 | command2
Sends the output of the first command to be the input of the second command.
command < [path]filename
Takes command input from the specified file path.
command > [path]filename
Sends output to the named file, creating the file if necessary or overwriting it if it already exists.
command >> [path]filename
Appends output to the named file if it exists or creates the file and then writes to it.
command < [path]filename > [path]filename
Gets command input from the specified file and then sends command output to the named file.
command < [path]filename >> [path]filename
Gets command input from the specified file and then appends command output to the named file.
command 2> [path]filename
Creates the named file and sends any error output to it. If the file exists, it is overwritten.
command 2>&1 filename
Sends error output to the same destination as standard output.

Redirecting Standard Output to Other Commands

Most commands generate output that can be redirected to another command as input. To do this, you use a technique called piping, whereby the output of a command is sent as the input of the next command. Following this, you can see the general syntax for piping is

Command1 | Command2

where the pipe redirects the output of Command1 to the input of Command2. But you can also redirect output more than once, such as

Command1 | Command2 | Command3

The two most common commands that are piped include FIND and MORE. The FIND command searches for strings in files or in text passed to the command as input and then lists the text of matching lines as output. For example, you could obtain a list of all .txt files in the current directory by typing the command

dir | find ".txt "

The MORE command accepts output from other commands as input and then breaks this output into sections which can be viewed one console page at a time. For example, you could page through a log file called Dailylog.txt using the following command:

type c:\working\logs\dailylog.txt | more

Type find /? or more /? at the command line to get a complete list of the syntax for these commands.

Redirecting I/O to and from Files

Another command redirection technique is to get input from a file using the input redirection symbol (<). For example, the following command sorts the contents of the Usernames.txt file and displays the results to the command line:

sort < usernames.txt

Just as you can read input from a file, you can also send output to a file. To do this, you can use > to create or overwrite, or >> to create or append data to a named file. For example, if you want to write the current network status to a file, you could use the command

netstat -a > netstatus.txt

Unfortunately, if there is an existing file in the current directory with the same file name, this command overwrites the file and creates a new one. If you want to append this information to an existing file rather than overwrite an existing file, change the command text to read

netstat -a >> netstatus.txt

The input and output redirection techniques can be combined as well. You could, for example, obtain command-input from a file and then redirect command-output to another file. In this example, a list of user names is obtained from a file and sorted, and then the sorted name list is written to a new file:

sort < usernames.txt > usernames-alphasort.txt

Redirecting Standard Error

By default, errors from commands are written as output on the command line. If you are running unattended batch scripts or utilities, however, you may want to redirect standard error to a file so that errors are tracked. One way to redirect standard error is to tell the command line that errors should go to the same destination as standard output. To do this, type the 2>&1 redirection symbol as shown in this example:

chkdsk /r > diskerrors.txt 2>&1

Here, you send standard output and standard error to a file called Diskerrors.txt. If you want to see only errors, you can redirect only the standard error. In this example, standard output is displayed at the command line and standard error is sent to the file Diskerrors.txt:

chkdsk /r 2> diskerrors.txt

Managing Command Shell

Managing Command Shell Startup

When you previously worked with the command line, you probably started it by clicking Start, pointing to Programs or All Programs, Accessories, and then choosing Command Prompt. Another way to start a command line is to use the Run dialog box or type cmd in an open command-shell window. These techniques enable you to pass arguments to the command line, including switches that control how the command line works as well as parameters that execute additional commands. For example, you can start the command shell in quiet mode (meaning command echo is turned off) by using the startup command cmd /q and if you wanted the command shell to execute a command and then terminate, you could type cmd /c followed by the command text enclosed in quotation marks. The following example starts a command shell, sends the output of ipconfig to a file, and then exits the command shell:

cmd /c "ipconfig > c:\ipconfig.txt"

Table 2-1 summarizes the key parameters for the Windows command shell (Cmd.exe). Note that several command-line parameters are set by default. Because of this, the command line normally uses standard ANSI character codes for command output, as opposed to Unicode character codes, and enables command extensions that add features to most built-in commands.

Table 2-1: Essential Parameters for the Command Line
Parameter
Description
/C
Executes the command specified and then exits the command shell.
/K
Executes the command specified and then remains in interactive mode.
/A
Command output to files (or pipes) is set to ANSI format (default).
/U
Command output to files (or pipes) is set to Unicode.
/U
Turns on quiet mode, meaning command echo is off. By default, command echo is on.
/T:FG
Sets the foreground and background colors for the console window.
/E:ON
Enables command extensions, which is the default.
/E:OFF
Disables command extensions.


Note

Some parameters cannot be used with other switches. For example, you can’t enable both Unicode and ANSI character codes. If you use both /A and /U, or /E:ON and /E:OFF, the command line applies the last option you passed on the command line.
Sometimes you may want to use different environment settings or parameters for a command line and then go back to your original settings without exiting the console window. To do this, you can use a technique called nesting. With nesting, you start a command line within a command line and the nested command line inherits its environment settings from the current command line. You can then modify the environment as necessary and execute commands and scripts using those settings. When you type exit to end the nested command- line instance, you return to the previous command line and the previous environment settings are restored.


Tip 
As you set out to work with the command shell, keep in mind that some characters have special meanings and that whenever the command shell encounters one of these characters, it attempts to carry out the special procedure associated with that character. Special characters include < > ( ) & | @ ^. If you want to use a special character as a regular character, you must escape the special character for the command shell to look at it literally, without invoking the special procedures with which it is associated. The escape character is the caret (^), which is the character above the 6 key on a standard keyboard, and is placed to immediately precede the special character.

Wednesday, 11 June 2014

General talk about Commands

Command is a common term for all Operating Systems


                              
Windows => Command Prompt,                   Linux  => SHELL Command.


Generally we think about Command, at first we know "Unix,Linux and similar OS".

General Structure of Operating System: 

But we all should know this before start,
This is a common structure of Operating System. So finally we should accept as "by Known or Unknown we bidirectionally connect with Commands". 

In earlier days our computer Operating Systems are only run by the help of Commands. So It only have a black screen and we using keyboard to give a command to our Operating System, then that transfer the machine level instruction to the Computer Hardware, Finally Computer responds our needs.

But, during this period Computer was not get more famous. But the same time it used by research centres very much for quick calculations. The main reason for lack of publicity is "Very Difficult to Understand". Really its not only a major reason, real one is Size. Earlier Computer occupies more space.

After the evaluation, The size of the Computer get reduced, But the Publicity

Operating System statistics 
1.Windows 7
  
50.06%
2.Windows XP
  
25.27%
3.Windows 8/Windows 8.1
  
12.64%
4.OS X
  
7.38%
5.Other 
  
3.36%
6.Windows Vista
  
2.90%
7.Linux
  
1.62%
Desktop OS Market Share as of May 2014 according to Usage of OS.

The reason for the usage, public says "Operating Systems like Windows are having  good GUI interface, So it's very easy to understand and work in full fledged.". It's a acceptable reason but the Powerful Operations are start with commands

let I Start to teach You all a Great PlatformCommander...!