1 / 13

Scripting in Windows and Linux

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. What is scripting? What is it for?.

owena
Download Presentation

Scripting in Windows and Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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. 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. 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. @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 More complex batch files

  7. @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 Useful but convoluted?

  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 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. 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 filename=${i#/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/

More Related