Lecture 5 scripting i
This presentation is the property of its rightful owner.
Sponsored Links
1 / 81

Lecture 5 – Scripting I PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on
  • Presentation posted in: General

Lecture 5 – Scripting I. BASH Powershell. Intro. We’ve run shell commands …one at a time …by hand What if we wanted to do the same thing many times? Aliases (more of a shortcut) Script (for bigger jobs). Scripting. Allow user to invoke programs, move/edit files, etc

Download Presentation

Lecture 5 – Scripting I

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 5 scripting i

Lecture 5 – Scripting I

  • BASH

  • Powershell


Intro

Intro

  • We’ve run shell commands

    • …one at a time

    • …by hand

  • What if we wanted to do the same thing many times?

    • Aliases (more of a shortcut)

    • Script (for bigger jobs)


Scripting

Scripting

  • Allow user to invoke programs, move/edit files, etc

    • We already have this manually, so why do we care?

      • Make the invocation easier

  • It’s like a programming language, but a higher level

    • Permissions, files, etc


Scripts

Scripts

  • Interactive mode

    • “Real time”

    • We type stuff by hand

    • This is what we’ve been doing

      • Almost everything that we’ll mention for “file mode” scripting works here as well

  • File mode

    • We put stuff in a file

    • Windows: .ps1

    • Linux: .sh


Assumptions

Assumptions

  • We will not run in interactive mode

  • We will not have spaces in filenames

  • We will not use “special” characters


Tokens reviewed

Tokens (reviewed)

  • A sequence of characters delineated by another character sequence

    • For us, delineator is white space

  • The delineator divides character sequence into words


Quoting

Quoting

  • Single quotes

    • Every character quoted literally

  • Double quotes

    • Mostly the same as single

    • Some chars interpreted ($var)

  • Back quotes (left of ‘1’)

    • “Delayed expansion”

    • Interpreted as command, replaced by results


I o redirection

I/O Redirection

  • stdin

    • Our terminal via keyboard

  • stdout

    • Our terminal

  • stderr

    • Our terminal


I o redirection1

I/O Redirection

  • Those are the default behaviors

  • Sometimes it is desirable to change this

    • Get input from file

    • Append output to file

    • Redirect output to another command’s input


Redirect stdout

Redirect stdout

  • Command > FILE

    • Write to FILE

    • e.g., ls –l > myListing.txt

  • Problems with this?


Redirect stdout1

Redirect stdout

  • Command > FILE

    • Write to FILE

    • e.g., ls –l > myListing.txt

  • Problems with this?

    • It’s overwrites FILE, even if it already exists!


Redirect stdout2

Redirect stdout

  • Command >> FILE

    • Append to FILE

    • e.g., echo “Second Part” >> myFile.txt

  • Just like “> FILE” if FILE doesn’t exist


Redirect stdin

Redirect stdin

  • Command < FILE

    • Read from FILE

    • e.g., myProgram < config.xml


Lecture 5 scripting i

> Or <

  • If you know C++

    • It’s backwards

  • If you don’t know C++

    • Just remember it


Piping

Piping

  • C1 | C2

    • Output of C1 as input into C2

    • e.g., ps –aux | grep vim

  • Indefinite piping

    • C1 | C2 | C3 | … | Cn


Bash variables

BASH Variables

  • Declared 2 ways

    • ‘set’ command

    • Giving undeclared variable a value

      • Type is not required

  • Example

    testStr=“meh”

    echo $testStr

    • Note: no whitespace around ‘=‘


Bash variables1

BASH Variables

  • Can concatenate strings

    bigStr=MyFile.$testStr

  • String manipulation

    testStr=meh

    echo ${testStr#m} // eh

    echo ${testStr%h} // me

  • Note that variable didn’t change (wasn’t assignment)


More examples

More examples

file=myFile.txt

echo ${file%txt}pdf // myFile.pdf


More examples1

More examples

file=myFile.txt

echo ${file%txt}pdf // myFile.pdf

tmp=test

echo ${tmp#e} // test why?


More examples2

More examples

file=myFile.txt

echo ${file%txt}pdf // myFile.pdf

tmp=test

echo ${tmp#e} // test why?

Start and end…not middle!


Even more examples

Even more examples

file=myFile

files=junk

echo $file.txt// myFile.txt


Even more examples1

Even more examples

file=myFile

files=junk

echo $file.txt// myFile.txt

echo $files.txt // junk.txt


Even more examples2

Even more examples

file=myFile

files=junk

echo $file.txt// myFile.txt

echo $files.txt // junk.txt

echo ${file}s.txt //myFiles.txt


Control structures

Control Structures

  • if-else-then

  • for

  • while

  • until

  • case


Syntax if

Syntax: if

if CONDITION; then CMDS; fi

-------------------OR---------------------

if CONDITION

then

cmds

elif CONDITION

then

cmds

fi


Example if

Example: if

if [ -e /dev/sda1 ]

then

echo “Found it!”

else

echo “It’s missing!”

fi


Syntax for

Syntax: for

for VAR in LIST; do CMD; done

------------OR-----------

for VARIABLE in LIST

do

COMMANDS

done


Example for 1

Example: for #1

for number in 1 2 3 4 5

do

echo $number

done


Example for 2

Example: for #2

for ((i=0; i<=10; i++)) do

echo $i

done


Conditions

Conditions

[ EXPRESSION ]: true/false

[ -e FILENAME ]: T if file exists

[ $a –eq 7 ]: T if vara is number 7

[ $a = “five” ]: T if vara is string “five”


Conditions1

Conditions

[ ! –x FILENAME ]: T if FILENAME is not executable

[ -e FN1 –o –d FN2 ]: T if FN1 exists or if FN2 is a directory

[ -n $var –a $FN1 –ot $FN2 ]: T if var contains a string AND file name in FN1 is older than FN2’s file


Example 1

Example #1

for name in *

do

echo nm $name

echo nm.txt ${name%.txt}

echo nm.* ${name%.*}

done

What does this do?


Example 2

Example #2

for (( i=23; i < 250; i+=12 ))

do

echo i is $i

done

What does this do?


Example 3

Example #3

for i in *

do

mv $i `echo $i | sed –r ‘s/[0-9 ]+//’`

done

This is more complicated, and we

haven’t learned all the pieces yet

  • Delayed expansion, sed, regex


Bash scripts

BASH scripts

  • We’re going to avoid interactive mode

  • Means we’ll be putting scripts in files


Shebang

Shebang

  • Every script file must start with shebang

  • Tells OS which shell to use

  • For us  #!/bin/bash

    • Must be at top of file


Procedures

Procedures

  • BASH procedures or shell functions

    • Same idea behind “regular” program functions

  • Must be declared before use


Procedures1

Procedures

procedure_name () {

BODY

}

  • Where are the params?


Procedures2

Procedures

procedure_name () {

BODY

}

  • Where are the params?

    • Hiding: $1, $2, etc


Procedures3

Procedures

procedure_name () {

BODY

}

  • Where are the params?

    • Hiding: $1, $2, etc

  • Where is return type?


Procedures4

Procedures

procedure_name () {

BODY

}

  • Where are the params?

    • Hiding: $1, $2, etc

  • Where is return type?

    • $? returns status code of last command

    • Look at RVH’s example script for other return values


Procedures example

Procedures Example

dotO () {

for f in *

do

echo ${f%.o}.out

done

}

echo Starting program

dotO

echo Ending program


Executing scripts

Executing scripts

  • ./myScript.sh1

  • . myScript.sh2

  • source myScript.sh2

  • File must be executable

  • Forces script to run in current process


Quoting again

Quoting (again)

  • Single quotes

    • Every character quoted literally

  • Double quotes

    • Mostly the same as single

    • Some chars interpreted ($var)

  • Back quotes (left of ‘1’)

    • “Delayed expansion”

    • Interpreted as command, replaced by results


Script conventions

Script conventions

  • Sometimes it’s easier to move directories than have a long relative path

  • When script is done, we should put everything back just like we found it!


Script conventions1

Script conventions

  • How do we know where to go back to?


Script conventions2

Script conventions

  • How do we know where to go back to?

  • pushd

    • Push a path onto a stack and go there

  • popd

    • Pull the top path off the stack and go to the new top item


Powershell

Powershell

  • BASH is nifty, but sometimes we need Windows.

    • We used to do CMD

    • Now we do Powershell

      • Much more similar to BASH


Invoking ps

Invoking PS

  • Powershell

    • CLI

  • Powershell ISE

    • GUI


Powershell help

Powershell – Help

  • Powershell is somewhat similar to BASH

  • You will still get somewhat lost

    • This is expected

    • It is also expected that you will work out the small details in lab


Lecture 5 scripting i

Help

  • man – shows you how to ask for help

  • help – same thing

    • essentially like doing “man man”

  • man *

    • Summary of available commands


Ps thingies

PS Thingies

  • Aliases

  • cmdlets

  • Functions

  • HelpFile

  • Provider


Ps thingies1

PS Thingies

  • Aliases – we’ve seen them

  • cmdlets – built-in short scripts

  • Functions

  • HelpFile

  • Provider

  • Most Linux cmds work, but arguments are different!

    • e.g., ls -l will throw error


Io redirection

IO Redirection

  • It has pipes

  • It has output redirection

  • Does NOT have input redirection


Regular expressions

Regular Expressions

  • PS supports REGEX

  • REGEX is a lecture all by itself


Syntax

Syntax

  • We still have tokens delineated by whitespace

    • ‘ \ ‘:more text on the next line

    • ‘ # ‘: this line is a comment


Variables

Variables

  • Case insensitive

    • This is different from BASH

    • Let us pretend otherwise for labs

  • All vars must begin with $

    • $myString=“meh” // $mS = “meh”

      • No type declared

      • Spaces around = are kosher


Variables1

Variables

  • Some pre-defined

    • $HOME

    • $PROFILE

    • $env:UserName


Printing stuff

Printing stuff

  • $myString

  • echo $myString

  • write-output $myString

  • write-host $myString

    • Makes things pretty

    • Not meant to be re-directed


Arrays

Arrays

  • $myArray=1, 2, 3, 4

  • $newArray=$myArray, 5, 6

  • $ext = ‘*.txt’, ‘*.pdf’, ‘*.txt”

    • The * is just a character here

  • $newArray = @(1,2,3,4)

    • Different syntax, same result


If statements

if Statements

if ( condition ) {CODE}

elseif ( newCondition ) {CODE}

else ( optionalCond ) {CODE}

  • Much closer to C/C++/Java

  • Braces can be omitted in certain cases

    • For 233, always put them in!


Comparison operators

Comparison Operators

  • -eq: ==

  • -lt: <

  • -gt: >

  • -ge: >=

  • -le: <=

  • -ne: !=

  • -i & -c: force case sensitivity


Logic operators

Logic Operators

  • -not

  • !

    • Same as “-not”

  • -and

  • -or


Examples

Examples

  • 7 –eq 7

  • 7 –ne 7

  • 7 –lt 10

  • 7 –gt 10

  • “Tom” –eq “tOm”

  • “Tom” –ceq “tOm”


Examples1

Examples

  • 7 –eq 7 TRUE

  • 7 –ne 7 FALSE

  • 7 –lt 10 TRUE

  • 7 –gt 10 FALSE

  • “Tom” –eq “tOm”

  • “Tom” –ceq “tOm”


Examples2

Examples

  • 7 –eq 7 TRUE

  • 7 –ne 7 FALSE

  • 7 –lt 10 TRUE

  • 7 –gt 10 FALSE

  • “Tom” –eq “tOm” TRUE

  • “Tom” –ceq “tOm” FALSE


For statements

for Statements

for ( init; condition; repeat )

{CODE}

  • Init

    • separated by commas

  • Condition

    • Comparison/conditional ops

  • Repeat

    • Separated by commas


Example

Example

$a = @(2,3,5,7,11,13,17)

for ($i = 0; $i –lt $a.Length; $i++)

{

Write-Host $a[$i]

}


While statement

while Statement

while ( condition )

{

CODE

}


Example fibonachi s

Example – Fibonachi #s

$c = $p = 1

$c # Print the first number

while ( $c –lt 100 )

{

$c; #print current number

$c,$p = ($c+$p),$c

}


Foreach statement

foreach Statement

foreach ( $<item> in $<collection> )

{

CODE

}

  • (For VBScript folks, no NEXT)

    • It advances automatically


Example 11

Example #1

$a = @(2,3,5,7,11,13,17)

foreach ($i in $a )

{

Write-Host $i

}


Example 21

Example #2

foreach ($f in dir *.*) {

$sz = $f.length

$ex = $f.extension

$fn = $f.name #unused

echo “File $f has $sz bytes \

and extension $ex”

}


Example 2 re visited

Example #2 re-visited

$list = Get-ChildItem –path “C:\User”

foreach ($f in $list) {

$sz = $f.length

$ex = $f.extension

$fn = $f.name #unused

echo “File $f has $sz bytes \

and extension $ex”

}


Functions

Functions

function fnc_name ( $p1, $p2, …)

{

CODE

}


Example1

Example

function printSize( $fn ) { $fn.length }

printSize “C:\boost\bjam.exe”

  • File is 244 KB. What do we expect to see?


Example2

Example

function printSize( $fn ) { $fn.length }

printSize “C:\boost\bjam.exe”

  • File is 244 KB. What do we expect to see? 249856

  • What do we get?


Example3

Example

function printSize( $fn ) { $fn.length }

printSize “C:\boost\bjam.exe”

  • File is 244 KB. What do we expect to see? 249856

  • What do we get? 17

  • Escape characters are odd

    • The backtick ( ` ) is escape char!


Lecture 5 scripting i

Misc

  • Where’s the shebang?

    • Don’t need one!

  • How do I actually run scripts?

    • . scriptName

    • Should have extension of PS1


Execution policy

Execution Policy

  • Windows is overly-motherly by default

  • Set-ExecutionPolicyremotesigned

    • Downloaded scripts must be signed by a remote publisher

  • Set-ExecutionPolicy unrestricted


More examples3

More Examples

  • We still learn best by seeing these things in action

  • http://www.cs.wright.edu/~pmateti/Courses/233/Labs/Scripting/scriptingWithPowerShell.html, Section 6


  • Login