Introduction to Scripting Workshop October
Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT
Introduction First Intro to Scripting Workshop Thanks for agreeing to be guinea pigs! Please Leave Feedback
Introduction Will be sent out afterwards: Slides Commands cheat sheet
Other Resources Shell and Scripting Tutorial
Other Resources How Linux Works Available at Safari Books Online
Other Resources Advanced Bash-Scripting Guide
Introduction What is a shell script?
Introduction What is a shell script? A file with shell commands.
Access Windows Instructions 1.Search for putty on Columbia home page 2.Select first result 3.Follow link to Putty download page 4.Download putty.exe 5.Run putty.exe
Access Mac Instructions 1.Run terminal
Access Mac (Terminal) $ ssh Windows (Putty) Host Name: didius.cc.columbia.edu
Access Aside System: cunix.columbia.edu User: Your UNI
Workshop Setup $ mkdir workshop $ cd workshop $ cp /tmp/workshop/*.
Command Line Example Word Count from “Data Science at the Command Line” - Jeroen Janssens
wcount $ cat wcount cat alice.txt | tr …
wcount $ wcount
wcount $ wcount -bash: wcount: command not found
wcount $./wcount
wcount $ wcount -bash:./wcount:Permission denied
wcount $ ls –l wcount
wcount $ ls –l wcount -rw-rw---- [ snip ]
wcount $ ls –l wcount -rw-rw---- [ snip ] $ chmod +x wcount
wcount $ ls –l wcount -rw-rw---- [ snip ] $ chmod +x wcount $ ls –l wcount -rwxrwx--x [ snip ]
wcount $./wcount Should work this time.
wcount Choose an editor nano Recommended default vi emacs
wcount Choose an editor nano Recommended default vi emacs
nano Nano commands are on back of cheat sheet. ^ means “hold down control”
Edit wcount $ nano wcount
#! Add “#!” to first line #!/bin/sh cat alice.txt | tr …
#! $./wcount Still works.
#! Some #! first lines you might see #!/bin/sh #!/usr/bin/perl #!/usr/bin/python
Variables $ file=alice.txt $ echo $file alice.txt
Variables 1.Add file=alice.txt to wcount. 2.Replace cat alice.txt with the variable.
Variables #!/bin/sh file=alice.txt cat “$file” | tr …
Variables Why put quotes around $file? cat “$file” | tr …
Command Line Parameters Change wcount so any file can be specified from the command line. $./wcount moby.txt
Command Line Parameters Change wcount so any file can be specified from the command line. $./wcount moby.txt $1
Command Line Parameters 1.Create a new file named “param” 2.Put the #! directive on the first line 3.One bash line: “echo $1” 4.Save and make executable 5.Run it
Command Line Parameters $ cat param #!/bin/sh echo $1
Command Line Parameters $./param $./param alice.txt $./param aaa bbb ccc
Command Line Parameters Update wcount to use $1 instead of alice.txt.
Command Line Parameters Before: file=alice.txt After: file="$1"
Command Line Parameters Update param to print out $# echo $# echo $1 Run it with different numbers of parameters
if if [[ condition ]] then do something fi
[, [[, (( [ : Standard comparison [[ : Extended comparison (( : Mathematical comparison
[, [[, (( [ : Standard comparison [[ : Extended comparison (( : Mathematical comparison
Comparison with [[ if [[ “$count” -eq 100 ]] -eq : equals -ne : not equal -gt : greater than …etc.
if if [[ $# -ne 1 ]] then do something fi
if if [[ $# -ne 1 ]] then echo “Usage: wcount file” exit 1 fi
if $./wcount $./wcount alice.txt $./wcount alice.txt junk
if if [[ ! –f “$file” ]] then echo “Error: File $file not found.” exit 1 fi
File Tests Common tests -e : File exists -f : File is “regular” -d : File is a directory …many more
if if [[ ! –f “$file” ]] then echo “Error: File $file not found.” exit 1 fi
Add lines parameter Show the five most common words: $./wcount alice.txt 5
while i=7 while (( $i > 4 )) do echo $i i=`expr $i - 1` done
for for i in aa bb cc do echo $i done for file in `ls` do ls $file done
readyourmind A silly impolite script but it shows a few more things. read : read input case : another way to control flow
End of Slides Questions?