Nic Shulver, [email protected] Scripts and Batch files Scripting in Windows and Linux What is...

13
Nic Shulver, [email protected] Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files Windows Environment Variables Example batch files UNIX/Linux shell scripting Linux Environment Variables Example shell scripts Conclusion

Transcript of Nic Shulver, [email protected] Scripts and Batch files Scripting in Windows and Linux What is...

Page 1: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesScripting in Windows and Linux

What is scripting?What is it for?DOS/Windows batch filesWindows Environment VariablesExample batch filesUNIX/Linux shell scriptingLinux Environment VariablesExample shell scriptsConclusion

Page 2: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesWhat is scripting? What is it for?

Scripting is an extension of the text-based command-line interface

Instead of repetitively typing in a sequence of commands, put them in a file, then just “run” the file with a single command

The basic concept is a bit like macros in a spreadsheet or word processor

Automation of repetitive commands reduces errorsA flexible scripting system becomes a “system programming

language”A good scripting system allows complex tasks to be automated

Page 3: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesDOS and Windows

Early PC’s had DOS (Disc Operating System), a text-only command-line interface

Current systems use GUIsAutomation is simpler in text-based systems – so batch scripts

are still usefulWindows NT (3.5, 2000, XP and so on) have an enhanced

command-line interfaceThe examples that follow give a glimpse into the style of

command shell batch scriptingMany batch files to file or directory manipulation.Often they need contextual information contained in the system’s

“environment variables”

Page 4: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesSome NT Environment Variables

ALLUSERSPROFILE=C:\Documents and Settings\All UsersAPPDATA=C:\Documents and Settings\nas1\Application DataCLASSPATH="C:\WINDOWS\system32\QTJava.zip"CommonProgramFiles=C:\Program Files\Common Files…NUMBER_OF_PROCESSORS=2OS=Windows_NTPATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSHPROCESSOR_ARCHITECTURE=x86PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 9, GenuineIntelPROCESSOR_LEVEL=15PROCESSOR_REVISION=0209…USERPROFILE=C:\Documents and Settings\nas1windir=C:\WINDOWSWINVERSION=XP

Page 5: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesExamples: NT command-line

@echo offrem Checks which machine we're running on first...

if "%USERNAME%"=="nas1" goto MachineOK

echo No! Wrong Machine!!!!goto EndOfScript

:MachineOKecho Yes! This is the right machine.

:EndOfScript

Page 6: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesMore complex batch files

@echo off

echo @echo off > allMyXLS.bat

echo rem Built by robots! >> allMyXLS.bat

for %%x IN (*.xls) DO echo print %%x >> allMyXLS.bat

echo rem All done >> allMyXLS.bat

call allMyXLS.bat@echo off

rem Built by robots!

print attendanceS2_Feb2006.xls

print CPU_Speed_Intel.xls

print CPU_Speed_Intel_v2.xls

print csa marks 2005-6.xls

rem All done

Page 7: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesUseful but convoluted?

@echo off

set theDate=%date%

set theDate2=%theDate:/=-%

md %theDate2%

copy *.xls %theDate2%

set theDate=

set theDate2=

This example creates two environment variables, uses one then destroys both

The first variable holds the date (e.g. 28/07/2009), the second holds a modified form of the first (e.g. 28-07-2009)

The second date is used to create a subdirectory

Files are copied into the subdirectory

This is a simple “daily backup” script

Page 8: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesUNIX/Linux shell scripting

There are a few different command-line systems or “shells” for Linux

The Bourne shell (sh), the original UNIX shell.The C-shell (csh), better for interactive usersThe tc-shell (tcsh), an improved C-shellThe Korn shell (ksh), a commercial (and improved) Bourne shellThe “Bourne again” shell (bash), a free, improved Bourne shell

with csh-like elements - very popular, part of LinuxThe Z shell (zsh), Bourne-like with many extra features

See: http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

Page 9: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesSome Linux Environment Variables

USERNAME HOSTNAME LOGNAME MAIL EDITOR - Specifies the editor to be used by default for some commands such

as edquota. Usually it is set to vi or emacs with a command like "export EDITOR=emacs".

TERM - The type of terminal being used. PATH - The path the system looks in to find commands that the user has

entered. HOME - The current user's home directory SHELL - The current shell program that is being executed USER - The name of the current user. TMPDIR - Allows programs that use the tempnam(3) function call to use the

directory specified by this variable rather than the /tmp directory. SHLVL - Shows how many shells the user has invoked.

Page 10: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesExample shell scripts

#!/bin/Bash

echo Type in a message

read sTextInput

echo `$sTextInput ` is: $sTextInput

#exit 0

Type in a message

This is a simple bash demonstration

$sTextInput is: This is a simple bash demonstration

Page 11: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesExample shell scripts

#!/bin/Bash# be sure the directory /mnt existsif [ ! -d /mnt ] then

mkdir /mntfi for i in /mnt/floppy/*; do

if [ -f $i ]; then# if the file is therefilename=${i#/mnt/floppy/}echo copying $i to /etc/$filenamecp -p $i /etc/$filename

Fidone

Page 12: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesXKCD - Command Line Fu

http://xkcd.com/196/

Page 13: Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Nic Shulver, [email protected]

Scripts and Batch filesConclusion

Scripting is very usefulNT provides many useful tools for automationHowever, Linux Bash scripting is far more like programming than

NT batch scripting In general, Bash is more powerful than the NT shellUNIX is fifteen years more mature than Windows, so you might

expect a differenceUNIX was a text-only minicomputer OS for a long time, so text-

based systems are very well developedBash scripting, online book:

http://www.linux.com/guides/abs-guide/