/
Introduction to Scripting Workshop Introduction to Scripting Workshop

Introduction to Scripting Workshop - PowerPoint Presentation

karlyn-bohler
karlyn-bohler . @karlyn-bohler
Follow
440 views
Uploaded On 2016-12-22

Introduction to Scripting Workshop - PPT Presentation

October 15 2015 Introduction Rob Lane amp The HPC Support Team Research Computing Services CUIT Introduction First Intro to Scripting Workshop Thanks for agreeing to be guinea pigs Please Leave ID: 504676

file wcount line txt wcount file txt line command alice parameters echo cat comparison bin param workshop introduction file

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Introduction to Scripting Workshop" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Introduction to Scripting Workshop

October 15 2015Slide2

Introduction

Rob Lane

&

The HPC Support Team

Research Computing Services

CUITSlide3

Introduction

First Intro to Scripting Workshop

Thanks for agreeing to be guinea pigs!

Please Leave

F

eedbackSlide4

Introduction

Will be sent out afterwards:

Slides

Commands cheat

s

heetSlide5

Other Resources

Shell and Scripting Tutorial

http://linuxcommand.org/Slide6

Other Resources

How

Linux W

orks

Available at Safari Books OnlineSlide7

Other Resources

Advanced

Bash-Scripting Guide

http://tldp.org/LDP/abs/html/Slide8

Introduction

What is a shell script?Slide9

Introduction

What is a shell script?

A file with shell commands.Slide10

Access

Windows Instructions

Search for putty on Columbia home page

Select first result

Follow link to Putty download page

Download putty.exe

Run putty.exeSlide11

Access

Mac Instructions

Run terminalSlide12

Access

Mac (Terminal)

$

ssh

UNI@didius.cc.columbia.edu

Windows (Putty)

Host Name: didius.cc.columbia.eduSlide13

Access

Aside

System: cunix.columbia.edu

User: Your UNISlide14

Workshop Setup

$

mkdir

workshop

$ cd workshop

$

cp

/

tmp

/workshop/* .Slide15

Command Line Example

Word Count

from

“Data

Science at the Command

Line”

-

Jeroen

JanssensSlide16

wcount

$ cat

wcount

cat

alice.txt |

tr

…Slide17

wcount

$

wcountSlide18

wcount

$

wcount

-bash:

wcount

: command not foundSlide19

wcount

$

./

wcountSlide20

wcount

$

wcount

-bash

:./

wcount:Permission

deniedSlide21

wcount

$

ls –l

wcountSlide22

wcount

$

ls –l

wcount

-

rw

-

rw

-

--- [ snip ]Slide23

wcount

$

ls –l

wcount

-

rw

-

rw

-

--- [ snip ]

$

chmod

+x

wcountSlide24

wcount

$

ls –l

wcount

-

rw

-

rw

-

--- [ snip ]

$

chmod

+x

wcount

$ ls –l

wcount

-

rwxrwx

-

-x [ snip ]Slide25

wcount

$

./

wcount

Should work this time.Slide26

wcount

Choose an editor

nano

 Recommended default

v

i

emacsSlide27

wcount

Choose an editor

nano

 Recommended default

v

i

emacsSlide28

nano

Nano commands are on back of cheat sheet.

^ means “hold down control”Slide29

Edit wcount

$

nano

wcountSlide30

#!

Add “#!” to first line

#!/bin/

sh

cat alice.txt |

tr

…Slide31

#!

$

./

wcount

Still works.Slide32

#!

Some #!

f

irst lines you might see

#!/

bin/

sh

#!/

usr

/bin/

perl

#!/

usr

/bin/pythonSlide33

Variables

$ file=alice.txt

$ echo $file

a

lice.txtSlide34

Variables

Add

file=alice.txt

to

wcount

.

Replace

cat alice.txt

with the variable.Slide35

Variables

#!/

bin/

sh

f

ile=alice.txt

cat “$file”

|

tr

…Slide36

Variables

Why put quotes around $file?

cat “$file”

|

tr

…Slide37

Command Line Parameters

Change

wcount

so any file can be specified from the command line.

$ ./

wcount

moby.txtSlide38

Command Line Parameters

Change

wcount

so any file can be specified from the command line.

$ ./

wcount

moby.txt

$1Slide39

Command Line Parameters

Create a new file named “

param

Put the #! directive on the first line

One bash line: “echo $1”

Save and make executable

Run itSlide40

Command Line Parameters

$ cat

param

#!/bin/

sh

e

cho $1Slide41

Command Line Parameters

$ ./

param

$ ./

param

alice.txt

$ ./

param

aaa

bbb

cccSlide42

Command Line Parameters

U

pdate

wcount

to use $1 instead of alice.txt.Slide43

Command Line Parameters

Before:

file=alice.txt

After:

file="$1"Slide44

Command Line Parameters

Update

param

to print out $#

e

cho $#

echo $1

Run it with different numbers of parametersSlide45

if

if [[ condition ]]

t

hen

do something

fiSlide46

[, [[, ((

[ : Standard comparison

[[ : Extended comparison

(( : Mathematical comparisonSlide47

[, [[, ((

[ : Standard comparison

[[ : Extended comparison

(( : Mathematical comparisonSlide48

Comparison with [[

i

f [[ “$count” -

eq

100

]]

-

eq

: equals

-ne : not equal

-

gt

: greater than

…etc.Slide49

if

if [[ $# -ne 1 ]]

t

hen

do something

fiSlide50

if

if [[ $# -ne 1 ]]

t

hen

echo “Usage:

wcount

file”

exit 1

fiSlide51

if

$ ./

wcount

$ ./

wcount

alice.txt

$ ./

wcount

alice.txt junkSlide52

if

if [[ ! –f “$file” ]]

t

hen

echo “Error: File $file not found.”

exit 1

fiSlide53

File Tests

Common tests

-e : File exists

-f : File is “regular”

-d : File is a directory

…many moreSlide54

if

if [[ ! –f “$file” ]]

t

hen

echo “Error: File $file not found.”

exit 1

fiSlide55

Add lines parameter

Show the five most common words:

$ ./

wcount

alice.txt 5Slide56

while

i

=7

while (( $

i

> 4 ))

do

echo $

i

i

=`expr $

i

- 1`

doneSlide57

for

for

i

in aa bb

cc

do

echo

$

i

done

for file in `

ls`

do

ls $file

doneSlide58

readyourmind

A silly impolite script but it shows a few more things.

read : read input

c

ase : another way to control flowSlide59

End of Slides

Questions?