windows shell scripting - welcome to gmitwebgmitweb.gmit.ie/pdunne/opsys/labs/lab2/intro to...

Post on 03-May-2018

228 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Using the Windows shell

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

Windows shell scripting

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 all the commands its about to execute

ANNOYING

The shell displays all the commands its about to execute

@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

Scripts obtain information from four basic sources:

• Information you write, or hard-code,into the script

• Echo Windows is in the C:\winnt directory.• Information from environment

variables• Echo Windows is in the %SYSTEMROOT% directory.

• Information from arguments• c:\mybatch.bat arg1 arg2• You sent arg1 and arg2 into the program

• Information from executed commands (i.e., command output)

Your own Environment Variables

• Use the set command to assign (string) values to variables

• Use /a switch to assign result of math operation to variable

• Extract value from variable using %var_name%

Using command line arguments

• %1 is first arg

• %2 is the second arg

• notice you only use one % in this case

Limiting scope of variable

• use Setlocaland EndLocal

Program Control• Normally, all commands in the batch file will be executed

in the order in which they appear in the file. – This is called a sequence.

• Sometimes, there are circumstances in which you would like to carry out commands in a different order or carry out a single command repeatedly.

• echo offREM print Paul all over the screen:startecho Paulgoto startREM end of program

label

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

IFReference

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

Some Maths Using 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 equal

to)

IF ExamplesIF EXIST C:\install.log (echo complete) ELSE (echo failed)

IF DEFINED _department ECHO Got the department variable

IF DEFINED _commission SET /A _salary=%_salary% + %_commission%

IF CMDEXTVERSION 1 GOTO start_process

IF ERRORLEVEL EQU 2 goto sub_problem2

Does the batch file have an argument? (i.e. Does %1 exist?)REF: Question 2 exercises

To test for the existence of a command line paramater - use empty brackets like this

IF [%1]==[ ] ECHO Value MissingorIF [%1] EQU [ ] ECHO Value Missing

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

Execute code if a condition doesn’texist

Readability

• You can improve the readability of a batch script by writing a complex IF...ELSE command over several lines using brackets e.g.

IF EXIST filename (del filename)ELSE (echo The file was not found.)

Simple looping

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 string “winnt” in the file

– The | is a pipe and it sends output from first command to 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

FOR...

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

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

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

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

top related