Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nic Shulver, Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files.

Similar presentations


Presentation on theme: "Nic Shulver, Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files."— Presentation transcript:

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 Windows Environment Variables Example batch files UNIX/Linux shell scripting Linux Environment Variables Example shell scripts Conclusion

2 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files What 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 errors A flexible scripting system becomes a “system programming language” A good scripting system allows complex tasks to be automated

3 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files DOS and Windows Early PC’s had DOS (Disc Operating System), a text-only command-line interface Current systems use GUIs Automation is simpler in text-based systems – so batch scripts are still useful Windows NT (3.5, 2000, XP and so on) have an enhanced command-line interface The examples that follow give a glimpse into the style of command shell batch scripting Many batch files to file or directory manipulation. Often they need contextual information contained in the system’s “environment variables”

4 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Some NT Environment Variables ALLUSERSPROFILE=C:\Documents and Settings\All Users APPDATA=C:\Documents and Settings\nas1\Application Data CLASSPATH="C:\WINDOWS\system32\QTJava.zip" CommonProgramFiles=C:\Program Files\Common Files … NUMBER_OF_PROCESSORS=2 OS=Windows_NT PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH PROCESSOR_ARCHITECTURE=x86 PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 9, GenuineIntel PROCESSOR_LEVEL=15 PROCESSOR_REVISION=0209 … USERPROFILE=C:\Documents and Settings\nas1 windir=C:\WINDOWS WINVERSION=XP

5 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Examples: NT command-line @echo off rem Checks which machine we're running on first... if "%USERNAME%"=="nas1" goto MachineOK echo No! Wrong Machine!!!! goto EndOfScript :MachineOK echo Yes! This is the right machine. :EndOfScript

6 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files More 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

7 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Useful 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

8 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files UNIX/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 users The tc-shell (tcsh), an improved C-shell The Korn shell (ksh), a commercial (and improved) Bourne shell The “Bourne again” shell (bash), a free, improved Bourne shell with csh-like elements - very popular, part of Linux The Z shell (zsh), Bourne-like with many extra features See: http://www.faqs.org/faqs/unix-faq/shell/shell-differences/http://www.faqs.org/faqs/unix-faq/shell/shell-differences/

9 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Some 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.

10 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Example 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

11 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Example shell scripts #!/bin/Bash # be sure the directory /mnt exists if [ ! -d /mnt ] then mkdir /mnt fi for i in /mnt/floppy/*; do if [ -f $i ]; then # if the file is there filename=${i#/mnt/floppy/} echo copying $i to /etc/$filename cp -p $i /etc/$filename Fi done

12 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files XKCD - Command Line Fu http://xkcd.com/196/

13 Nic Shulver, N.A.Shulver@staffs.ac.uk Scripts and Batch files Conclusion Scripting is very useful NT provides many useful tools for automation However, Linux Bash scripting is far more like programming than NT batch scripting In general, Bash is more powerful than the NT shell UNIX is fifteen years more mature than Windows, so you might expect a difference UNIX was a text-only minicomputer OS for a long time, so text- based systems are very well developed Bash scripting, online book: http://www.linux.com/guides/abs- guide/http://www.linux.com/guides/abs- guide/


Download ppt "Nic Shulver, Scripts and Batch files Scripting in Windows and Linux What is scripting? What is it for? DOS/Windows batch files."

Similar presentations


Ads by Google