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

22
Using the Windows shell

Upload: dolien

Post on 03-May-2018

228 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Using the Windows shell

Page 2: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 3: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Windows shell scripting

Page 4: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 5: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

@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

Page 6: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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)

Page 7: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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%

Page 8: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Using command line arguments

• %1 is first arg

• %2 is the second arg

• notice you only use one % in this case

Page 9: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Limiting scope of variable

• use Setlocaland EndLocal

Page 10: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 11: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 12: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

IFReference

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

Page 13: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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)

Page 14: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 15: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Execute code if a condition doesn’texist

Page 16: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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.)

Page 17: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

Simple looping

Page 18: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 19: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

FOR...

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

Page 20: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 21: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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

Page 22: Windows shell scripting - Welcome to gmitWEBgmitweb.gmit.ie/pdunne/opsys/labs/lab2/Intro to Windows...IF CMDEXTVERSION 1 GOTO start_process IF ERRORLEVEL EQU 2 goto sub_problem2 Does

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