Saturday, 5 October 2013

Operator precedence

  Likewise other programming languages, batch program does support operator precedence for performing a valid arithmetic operation to obtain accurate results. The precedence of operations are given in order, *, /, %, +, -. The expression that is enclosed and grouped with the grouping operator ‘()’ gets the high priority in the precedence.

C:\>set /A (10-5)*2+6/2

In the above example, the expression that is enclosed within the ‘()’ operator gets the high priority and thus 10-5 is ‘5’, the next priority moves to the ‘/’ division operator and ‘6/2’ gives ‘3’, then comes the multiplication ‘*’ operator 5*2 gives ‘10’ then it is summed up with ‘3’ to obtain the final result as ‘13’. To redirect the output of one command to other file, the ‘>’ and ‘<’ command is used. For example the below command is used to print the text “hello redirection” to a notepad file named “first.txt”

C:\>echo hello redirection > first.txt C:\>

As we already have seen that the ‘echo’ command is used for printing the given text on the screen, here by using the redirection operator ‘>’ we are redirecting the output of the command to a text file. It will create a new text file even it wasn’t already there. Likewise you can redirect the output of any command to any other files. The below command is used for performing the same operation but the redirection happens to word document,

C:\> echo hello redirection > first.doc

The tilde ‘~’ operator is a unary operator that is used for shortening the long directory names, the following example will brief with the usage of this operator. The tilde operator can be used after 6 consecutive characters of a directory name, for example the “Documents and Settings” is a directory that contains more than 8 characters, instead of typing them all and messing with it, we can use the ‘~’ operator, so that it will automatically recognizes the path and performs the operation mentioned,

C:\>cd C:\DOCUME~1\CYB3RC~1\LOCALS~1\Temp C:\DOCUME~1\CYB3RC~1\LOCALS~1\Temp>

The above command is just a path to the location “C:\Documents and Settings\Cyb3rcr4wl3r\Local Settings\Temp”, where “Cyb3rcr4wl3r’ is the user account on my computer. Note: even though the ‘~’ operator is a unary operator, it can’t be used without the 1 following the operator. The ‘&&’ operator is used to execute multiple commands in a single line, for example, the following command is used to print the text ‘hi’ and ‘hello’ using two different echo commands,

C:\>echo Hi && echo hello Hi Hello

The pipeline operator is used for giving the output of one command as input for another command, C:\>echo Y | del *.txt

In the above example, whenever you delete a file using the del command, it will prompt you with a confirmation message whether to delete the file or not, and only depending upon the user input it will proceed further, here we can make use of the pipeline ‘|’ operator to print ‘Y’ when the ‘del’ command prompt for the user interaction. Whenever the ‘del’ command prompts the user for the confirmation, the output of the echo command (i.e. ‘Y’) will be given as input for the del command, and as a result it deletes all the text files that reside in the specified directory.

No comments:

Post a Comment