A very simple shell scripts bintcsh ls l
A very simple shell scripts #!/bin/tcsh (ls -l | wc) & ls -l ; ls –a set My. LS = `ls` echo Listing is $My. LS `command` means the output of the command
Shell scripts #!/bin/tcsh #----Ask the user for the file extension if none provided if ($#argv < 1) then echo -n "What file type : " set File. Type=$< else first argument set File. Type = $1 endif echo "Looking through $File. Type files" #----Look in each file of that type foreach File (*. $File. Type) opens file of type () echo -n "Searching $File. . . " #----Count the number of lines containing "int" set Int. Count = `grep -c int $File` `` means the output of the command echo "It has $Int. Count lines containing int” end
A more complicated shell script #!/bin/tcsh #---------------------------------------echo -n "What is the limit on your rabbit population: " @ Rabbit. Limit = $< set Fibonaccis = (0 1) @ Epochs = 2 while ($Fibonaccis[$Epochs] < $Rabbit. Limit) @ Epochs++ @ Epochs 1 = $Epochs - 1 @ Epochs 2 = $Epochs - 2 @ Next. Fibonacci = $Fibonaccis[$Epochs 1] + $Fibonaccis[$Epochs 2] set Fibonaccis = ($Fibonaccis $Next. Fibonacci) end echo "After $Epochs epochs there are $Fibonaccis[$Epochs] rabbits" echo -n "Enter the first epoch of interest: " @ First = $< echo -n "Enter the last epoch of interest: " @ Last = $< echo "Rabbit populations were. . . " echo " $Fibonaccis[$First-$Last]" #--------------------------------------- while loop extending the array
Shell scripts on Pegasus 2 • Log into pegasus 2 using ssh <username>@pegasus 2. ccs. miami. edu • To start a job you need a shell script which shell? job name #!/bin/bash file for stderr #BSUB -J HIV file for stdout #BSUB -e %HIV. err #BSUB -o %HIV. out number of cores #BSUB -n 1 queue #BSUB -q general #BSUB -W 72: 00 time allocation # cd ${HOME}/Viruses/HIV/ (. . /Python/distance. Clustering 2. py. . /human_mint_biogrid_hprd_hint. ppi HIV_targets_HPIDB. txt. . /human_essential_genes. txt > HIV_ess) >& HIV_ess. log your script
Shell scripts on Pegasus 2 • submit a job with bsub < script. sh • bjobs returns the status of current jobs • transfer data through • scp <user>@pegasus 2 -gw. ccs. miami. edu: ~/. into your home directory • scp <user>@pegasus 2 -gw. ccs. miami. edu: /scratch/<user> into scratch directory
Python • Python is a very simple language, and has a very straightforward syntax. • It encourages programmers to program without boilerplate (prepared) code. • There are two major Python versions, Python 2 and Python 3. Python 2 and 3 are quite different. Python 2 is more widely used and supported. However, Python 3 is more semantically correct, and supports newer features. • For example, one difference between Python 2 and 3 is the print statement. In Python 2, the "print" statement is not a function, and therefore it is invoked without parentheses. However, in Python 3, it is a function, and must be invoked with parentheses. • Our first Python program: #!/usr/bin/python print “Hello world!”
Intendation Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces. For example: #!/usr/bin/python x=1 if x == 1: # indented four spaces print "x is 1. "
Variables & Types • Numbers - Python supports two types of numbers - integers and floating point numbers. myint = 7 myfloat = 7. 0 myfloat = float(7) coercing an into a float • Strings - Strings are defined either with a single quote or a double quotes. mystring = 'hello’ mystring = “hello” mystring = "Don't worry about apostrophes"
Variables & Types • Simple operators can be executed on numbers and strings: #!/usr/bin/python one = 1 two = 2 three = one + two print three hello = "hello" world = "world" helloworld = hello + " " + world print helloworld sum = str(one) + " " + (two) print sum coercing an into a string
Variables & Types • Assignments can be done on more than one variable "simultaneously" on the same line like this: #!/usr/bin/python a, b = 1, 2 print a, b #!/usr/bin/python one = 1 two = 2 hello = "hello" #would this work? print one + two + hello assigning 1 to a and 2 to b
Lists • Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. #!/usr/bin/python mylist = [] mylist. append(1) mylist. append(2) mylist. append(3) print(mylist[0]) # prints 1 print(mylist[1]) # prints 2 print(mylist[2]) # prints 3 # prints out 1, 2, 3 for x in mylist: print x
Lists • What would happen here? #!/usr/bin/python mylist = [1, 2, 3] print(mylist[10]) • catching errors #!/usr/bin/python mylist = [1, 2, 3] try: print(mylist[10]) except Index. Error: pass expect an error to occur catch the error what to do if the error occurred
Basic operators • Just as any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers. #!/usr/bin/python number = 1 + 2 * 3 / 4. 0 print number remainder = 11 % 3 print remainder squared = 7 ** 2 cubed = 2 ** 3 print squared, cubed does python follow order of operations? modulo operation equivalent to 7*7 equivalent to 2*2*2
Basic operators - numbers • Just as any other programming languages, the addition, subtraction, multiplication, and division operators can be used with numbers. #!/usr/bin/python number = 1 + 2 * 3 / 4. 0 print number remainder = 11 % 3 print remainder squared = 7 ** 2 cubed = 2 ** 3 print squared, cubed does python follow order of operations? modulo operation equivalent to 7*7 equivalent to 2*2*2
- Slides: 14