Scripting (Just one of many tools.) P. Bailey

advertisement
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….
https://www.youtube.com/watch?v=6iVfXj7aojs
Concept of a Shell
who
diff
netstat
Kernel
vi
ls
Hardware
df
grep
awk
SHELL
another shell
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: https://en.wikipedia.org/wiki/Scripting_language
UNIX Scripting
Common languages: bash, K-Shell, CShell, 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
“Add one egg
Add flour”
INTERPRETER
Compiled
COMPILER
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.
https://en.wikipedia.org/wiki/Regular_expression
Basics of RE
MChar
^
$
.
|
{...}
[...]
(...)
*
+
?
\
Definition
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
Pattern
^abc
abc$
a.c
bill|ted
ab{2}c
a[bB]c
(abc){2}
ab*c
ab+c
ab?c
a\sc
Sample Matches
abc, abcdefg, abc123, ...
abc, endsinabc, 123abc, ...
abc, aac, acc, adc, aec, ...
ted, bill
abbc
abc, aBc
abcabc
ac, abc, abbc, abbbc, ...
abc, abbc, abbbc, ...
ac, abc
ac
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:
2016-04-23
19:19:33:932 6204 1e00 COMAPI - Install call complete (succeeded = 1,
succeeded with errors = 0, failed = 0, unaccounted = 0)
2016-04-23
19:53:21:811 6277 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
EXAMPLE
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXfoPWZt installed on Thu Nov 15 09:42:42 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
#first entry
43 10 * * * /home/pmb4/IS271/scripting/scripts/nightlyrun.sh
Windows Scheduling
Lab Preparation
Review basic concepts at
http://powershell.com/cs/blogs/ebookv2/defau
lt.aspx (if, while and foreach)
Read Chapter 18: WMI from above.
Review https://msdn.microsoft.com/enus/library/windows/desktop/aa394595%28v=vs.8
5%29.aspx
Download