Scripting (Just one of many tools.) P. Bailey
No, not this kind of scripting…. I wanted to find some cool graphics on scripting and I got….
Concept of a Shell Hardware Kernel SHELL vi grepawk ls who another shell df netstat diff
Scripting Languages Plain-text files Interpreted – Not Compiled Tie together tools Usually and imperative language with flow control Form of job control The “glue”: coordinate the calls to applications A scripting or script language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Wikipedia:
UNIX Scripting Common languages: bash, K-Shell, C- Shell, perl, awk Bash, ksh etc have meta-character support Heavy use in /etc (the home for a system administrator Example: /etc/rc0.d
UNIX Scripting Languages Must have both read and execute permission myscript.sh -r-wr-wr-w Should be myscript.sh –r-xr-xr-x (or –rwx-r-xr-x) Why not just execute?
Windows Scripting Languages . BAT files. Originally from MSDOS,very limited. Environment variables identified by percent signs (E.G. %PATH%) … UGH! VB Script. Like VBA and VB. Very flexible (e.g. supports regular expressions) Powershell. Combines the strongest features of other languages..Net support and object based. (*still doesn’t have meta-characters) Has an execution policy.
Interpreted Ajouter un œuf. “Add one egg Add flour” INTERPRETER Ajouter la farine
Compiled Ajouter un œuf. COMPILER Add one egg. Add 1 cup flour
Discussion question…(get a piece of candy) Compiled applications only need the “interpretation” once. Then, they are directly executable. Why not use compiled languages more often instead of scripting?
Use of Environment System level Application level (usually taking precedence) Passed on to applications called in script The environment includes: Current working path Variables (Path, LIBPATH, $SHELL) Current process information (e.g. process Id) Etc.
Process (Applies to Windows and UNIX) A process is an instance of an application running. Each process is uniquely identified by its ID A process can create another process (child process) A process gets a copy of the environment from its parent process
Process implications Placing environment variables in a script is not enough because calling the script only creates a sub-process For the environment variables in a separate script to change the current process, it must be dot-sourced Example:. /path/myenv.sh. /path/myenv.ps1
Handy Tool: Regular Expressions In theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression ) [1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations.theoretical computer scienceformal language theory [1][2]characterspattern matchingstringsstring matching
Basics of RE MCharDefinition ^Start of a string. $End of a string..Any character (except \n newline) |Alternation. {...}Explicit quantifier notation. [...]Explicit set of characters to match. (...)Logical grouping of part of an expression. *0 or more of previous expression. +1 or more of previous expression. ? 0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string. \ Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.
Sample Patterns PatternSample Matches ^abcabc, abcdefg, abc123,... abc$abc, endsinabc, 123abc,... a.cabc, aac, acc, adc, aec,... bill|tedted, bill ab{2}cabbc a[bB]cabc, aBc (abc){2}abcabc ab*cac, abc, abbc, abbbc,... ab+cabc, abbc, abbbc,... ab?cac, abc a\sca c
Problem Write a regular expression that matches any line in a log file that indicates a failed status of some type for an installation such as the first line in the example below: :19:33: e00 COMAPI - Install call complete (succeeded = 1, succeeded with errors = 0, failed = 0, unaccounted = 0) :53:21: ky27 Agent- Install success 'Install.*failed =.+'
Startup Scripts Startup scripts exist to ensure that the “environment” is setup before running an application. The environment can include the directory where the application starts environment variables that the application uses to determine run time features Startup scripts also can log the successful launch or shutdown of an application
Job Control “Job” is a term historically associated with scheduled operations. A Job Control language defines dependencies (e.g. On success of Job A, run Job B, otherwise run Job c) Scripting in UNIX and Windows are the low cost alternative to Job Control
UNIX Scheduling with cron # minute (0 - 59) # | hour (0 - 23) # | | day of month (1 - 31) # | | | month (1 - 12) # | | | | day of week (0 - 6) (Sunday=0 or 7) # | | | | | * * * * * command to be executed # DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.XXXXfoPWZt installed on Thu Nov 15 09:42: ) # (Cron version V $Id: crontab.c,v /01/23 18:56:42 vixie Exp $) #first entry * * * /home/pmb4/IS271/scripting/scripts/nightlyrun.sh EXAMPLE
Windows Scheduling
Lab Preparation Review basic concepts at lt.aspx (if, while and foreach) lt.aspx Read Chapter 18: WMI from above. Review us/library/windows/desktop/aa394595%28v=vs.8 5%29.aspxhttps://msdn.microsoft.com/en- us/library/windows/desktop/aa394595%28v=vs.8 5%29.aspx