Scripting in Windows and Linux - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Scripting in Windows and Linux

Description:

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 – PowerPoint PPT presentation

Number of Views:131
Avg rating:3.0/5.0
Slides: 14
Provided by: NAS1150
Category:

less

Transcript and Presenter's Notes

Title: Scripting in Windows and Linux


1
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
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
DOS and Windows
  • Early PCs 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 systems environment variables

4
Some NT Environment Variables
  • ALLUSERSPROFILEC\Documents and Settings\All
    Users
  • APPDATAC\Documents and Settings\nas1\Application
    Data
  • CLASSPATH"C\WINDOWS\system32\QTJava.zip"
  • CommonProgramFilesC\Program Files\Common Files
  • NUMBER_OF_PROCESSORS2
  • OSWindows_NT
  • PATHEXT.COM.EXE.BAT.CMD.VBS.VBE.JS.JSE.WS
    F.WSH
  • PROCESSOR_ARCHITECTUREx86
  • PROCESSOR_IDENTIFIERx86 Family 15 Model 2
    Stepping 9, GenuineIntel
  • PROCESSOR_LEVEL15
  • PROCESSOR_REVISION0209
  • USERPROFILEC\Documents and Settings\nas1
  • windirC\WINDOWS
  • WINVERSIONXP

5
Examples NT command-line
  • _at_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
More complex batch files
  • _at_echo off
  • echo _at_echo off gt allMyXLS.bat
  • echo rem Built by robots! gtgt allMyXLS.bat
  • for x IN (.xls) DO echo print x gtgt
    allMyXLS.bat
  • echo rem All done gtgt allMyXLS.bat
  • call allMyXLS.bat
  • _at_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
Useful but convoluted?
  • _at_echo off
  • set theDatedate
  • set theDate2theDate/-
  • 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
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/

9
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 EDITORemacs".
  • 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
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
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
  • filenamei/mnt/floppy/
  • echo copying i to /etc/filename
  • cp -p i /etc/filename
  • Fi
  • done

12
XKCD - Command Line Fu
http//xkcd.com/196/
13
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/
Write a Comment
User Comments (0)
About PowerShow.com