Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Scripting Workshop October 15 2015.

Similar presentations


Presentation on theme: "Introduction to Scripting Workshop October 15 2015."— Presentation transcript:

1 Introduction to Scripting Workshop October 15 2015

2 Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT

3 Introduction First Intro to Scripting Workshop Thanks for agreeing to be guinea pigs! Please Leave Feedback

4 Introduction Will be sent out afterwards: Slides Commands cheat sheet

5 Other Resources Shell and Scripting Tutorial http://linuxcommand.org/

6 Other Resources How Linux Works Available at Safari Books Online

7 Other Resources Advanced Bash-Scripting Guide http://tldp.org/LDP/abs/html/

8 Introduction What is a shell script?

9 Introduction What is a shell script? A file with shell commands.

10 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

11 Access Mac Instructions 1.Run terminal

12 Access Mac (Terminal) $ ssh UNI@didius.cc.columbia.edu Windows (Putty) Host Name: didius.cc.columbia.edu

13 Access Aside System: cunix.columbia.edu User: Your UNI

14 Workshop Setup $ mkdir workshop $ cd workshop $ cp /tmp/workshop/*.

15 Command Line Example Word Count from “Data Science at the Command Line” - Jeroen Janssens

16 wcount $ cat wcount cat alice.txt | tr …

17 wcount $ wcount

18 wcount $ wcount -bash: wcount: command not found

19 wcount $./wcount

20 wcount $ wcount -bash:./wcount:Permission denied

21 wcount $ ls –l wcount

22 wcount $ ls –l wcount -rw-rw---- [ snip ]

23 wcount $ ls –l wcount -rw-rw---- [ snip ] $ chmod +x wcount

24 wcount $ ls –l wcount -rw-rw---- [ snip ] $ chmod +x wcount $ ls –l wcount -rwxrwx--x [ snip ]

25 wcount $./wcount Should work this time.

26 wcount Choose an editor nano  Recommended default vi emacs

27 wcount Choose an editor nano  Recommended default vi emacs

28 nano Nano commands are on back of cheat sheet. ^ means “hold down control”

29 Edit wcount $ nano wcount

30 #! Add “#!” to first line #!/bin/sh cat alice.txt | tr …

31 #! $./wcount Still works.

32 #! Some #! first lines you might see #!/bin/sh #!/usr/bin/perl #!/usr/bin/python

33 Variables $ file=alice.txt $ echo $file alice.txt

34 Variables 1.Add file=alice.txt to wcount. 2.Replace cat alice.txt with the variable.

35 Variables #!/bin/sh file=alice.txt cat “$file” | tr …

36 Variables Why put quotes around $file? cat “$file” | tr …

37 Command Line Parameters Change wcount so any file can be specified from the command line. $./wcount moby.txt

38 Command Line Parameters Change wcount so any file can be specified from the command line. $./wcount moby.txt  $1

39 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

40 Command Line Parameters $ cat param #!/bin/sh echo $1

41 Command Line Parameters $./param $./param alice.txt $./param aaa bbb ccc

42 Command Line Parameters Update wcount to use $1 instead of alice.txt.

43 Command Line Parameters Before: file=alice.txt After: file="$1"

44 Command Line Parameters Update param to print out $# echo $# echo $1 Run it with different numbers of parameters

45 if if [[ condition ]] then do something fi

46 [, [[, (( [ : Standard comparison [[ : Extended comparison (( : Mathematical comparison

47 [, [[, (( [ : Standard comparison [[ : Extended comparison  (( : Mathematical comparison

48 Comparison with [[ if [[ “$count” -eq 100 ]] -eq : equals -ne : not equal -gt : greater than …etc.

49 if if [[ $# -ne 1 ]] then do something fi

50 if if [[ $# -ne 1 ]] then echo “Usage: wcount file” exit 1 fi

51 if $./wcount $./wcount alice.txt $./wcount alice.txt junk

52 if if [[ ! –f “$file” ]] then echo “Error: File $file not found.” exit 1 fi

53 File Tests Common tests -e : File exists -f : File is “regular” -d : File is a directory …many more

54 if if [[ ! –f “$file” ]] then echo “Error: File $file not found.” exit 1 fi

55 Add lines parameter Show the five most common words: $./wcount alice.txt 5

56 while i=7 while (( $i > 4 )) do echo $i i=`expr $i - 1` done

57 for for i in aa bb cc do echo $i done for file in `ls` do ls $file done

58 readyourmind A silly impolite script but it shows a few more things. read : read input case : another way to control flow

59 End of Slides Questions?


Download ppt "Introduction to Scripting Workshop October 15 2015."

Similar presentations


Ads by Google