Using the window shell

Embed Size (px)

Citation preview

  • 8/7/2019 Using the window shell

    1/22

    Using the Windows shell

  • 8/7/2019 Using the window shell

    2/22

    Useful commands

    command/? get help on a command

    cd /?

    clear clears screen

    md

    make directory

    copy

    cd cd .. cd /

    Change dir

    change to parent dir

    u:

    change partition

    help

    list commands

  • 8/7/2019 Using the window shell

    3/22

    Windows shell scripting

  • 8/7/2019 Using the window shell

    4/22

    The shell reads,parses and executes the line

    I write a batch file and run it

    executes command and displays result

    The shell displays the line it readsfrom the batch file

    ANNOYING

    The shell displays allthe commands itsabout to execute

    ANNOYING

    The shell displays allthe commands itsabout to execute

  • 8/7/2019 Using the window shell

    5/22

    @Echo off at the start of your script stops+ The @ stops Echo Off being displayed+ Echo off stops

    + the shell displaying REM statements+ the shell displaying the commands its about to execute

    nice

  • 8/7/2019 Using the window shell

    6/22

  • 8/7/2019 Using the window shell

    7/22

    Your own Environment Variables

    Use the setcommand to assign

    (string) values tovariables

    Use /a switch to

    assign result of mathoperation to variable

    Extract value fromvariable using

    %var_name%

  • 8/7/2019 Using the window shell

    8/22

    Using command line arguments

    %1 is first

    arg %2 is the

    second arg notice you

    only use one% in thiscase

  • 8/7/2019 Using the window shell

    9/22

    Limiting scope of variable

    use

    SetlocalandEndLocal

  • 8/7/2019 Using the window shell

    10/22

    Program Control

    Normally, all commands in the batch file will be executedin the order in which they appear in the file.

    This is called a sequence.

    Sometimes, there are circumstances in which you wouldlike to carry out commands in a different order or carry

    out a single command repeatedly. echo off

    REM print Paul all over the screen

    :startecho Paulgoto startREM end of program

    label

  • 8/7/2019 Using the window shell

    11/22

    Command Redirection

    If you run the command Echo Fred was here

    the screen displays the text Fred was here.

    Redirect a command's output to a file with redirection symbols. > (overwrite) and >> (append)

    Echo Fred was here > C:\fredreport.txt

    Creates (or overwrites) fredreport.txt with Fred was here

    Echo Barney was here >> C:\fredreport.txt

    Creates (or appends) fredreport.txt with Barney was here

  • 8/7/2019 Using the window shell

    12/22

    IF

    Reference

    http://www.ss64.com/nt/index.htmlhttp://www.ss64.com/nt/index.html

  • 8/7/2019 Using the window shell

    13/22

    Some MathsUsing SET and IF

    If logical_condition do_this

    Logical conditions

    EQU (equal to)

    NEQ (not equal to)

    LSS (less than)

    LEQ (less than or equal to)

    GTR (greater than)

    GEQ (greater than or equalto)

  • 8/7/2019 Using the window shell

    14/22

  • 8/7/2019 Using the window shell

    15/22

    Execute code if a condition doesnt

    exist

  • 8/7/2019 Using the window shell

    16/22

    Readability

    You can improve the readability of a batch

    script by writing a complex IF...ELSEcommand over several lines usingbrackets e.g.

    IF EXIST filename (del filename)

    ELSE (echo The file was not found.)

  • 8/7/2019 Using the window shell

    17/22

    Simple looping

  • 8/7/2019 Using the window shell

    18/22

    Combining commands

    Dir /ad C:\ | Find /i "winnt" lists directories (only) and send results to Find which

    will perform a case insensitive search for the stringwinnt in the file

    The | is a pipeand it sends output from first commandto second

    Echo Fred & Echo Wilma & Echo Barney & combines commands

    Command A && Command B Do command B if command A is successful

    Command A || Command B Bo command B is A is unsuccessful

  • 8/7/2019 Using the window shell

    19/22

    FOR...

    http://www.ss64.com/nt/index.html

  • 8/7/2019 Using the window shell

    20/22

  • 8/7/2019 Using the window shell

    21/22

    http://www.ss64.com/nt/index.html

  • 8/7/2019 Using the window shell

    22/22

    http://www.ss64.com/nt/index.html