Scripting Languages
Basics proper programmers write commands E.g. mkdir john1 rather than clicking on icons If you write a (set of) command more that once you can put them in a file and you do not need to write them again. Two of more, use a for (Dijkstra) Interpreted not compiled (like java) Automate the execution of tasks Glue or high level languages.
Scripting language 1 Scripting languages are designed to automate frequently used tasks that usually involve calling or passing commands to external programs. Those that are interpretive are often called scripting languages. In the following slides (py means python and sh means bash)
Scripting language 2 Recently, many applications have built-in traditional scripting languages, such as Perl or Visual Basic, but there are quite a few "native" scripting languages still in use. Many scripting languages are compiled to bytecode and then this (usually) platform independent bytecode is run through a virtual machine (compare to Java).
Examples of Scripting Langauges Bash (unix/linux) Cygwin (windows freely downloadable) Python (adopted by BIG DATA communities) perl "script" often used for small programs (up to a few thousand lines of code) text-processing languages sed and AWK Nothing special about scripting languages
Cygwin Command line (not icons) free!!! Like unix for windows Programming language/operating system. Shell (like bash) Text utilities: grep, sed, diff, patch, awk (alternative is mingw- Minimalist GNU) http://en.wikipedia.org/wiki/cygwin
Cygwin Prompt The command prompt or just prompt is where you type your command Note in these slides $ is a prompt, so you do not need to type it as part of the command. E.g. $ ls mean just type ls You prompt might be different e.g. > Also the prompt on your system might change (typically it could be the current working directory)
What you see
Working with Files cp <filename> <new filename> copy - Make a copy of a file cp -R <directory> <new directory> Make a copy of a directory mv <filename> <new filename> move - Move or rename a file rm <filename> remove - Delete a file 9
Some Bash/cygwin Commands 1 Comm Usage Comment and pwd pwd Print your current directory (folder) location ls ls List the files in the current folder mkdir mkdir Blast Make a new folder. Example Blast cd cd /Blast Change to another folder like Blast cd cd c:/ Change to C drive
Some Bash/cygwin Commands 2 echo "hi" # prints hi to screen echo "hi" > file.txt #writes hi to file.txt (overwrite) echo "bye" >> file.txt #writes bye to file.txt (appends) read usermessage #asks user for message head -2 file.txt #prints first two lines of file.txt tail -2 file.txt #prints last two lines of file.txt
Spaces and $ (values of) (sh) todo jrw@tambala ~ $ x= 8 -bash: 8: command not found jrw@tambala ~ $ x=8 jrw@tambala ~ $ echo $x 8 jrw@tambala ~ $ echo x x jrw@tambala ~ $
Running a Script Type some commands into a file. Save the file (you can choose the extension.sh e.g. myscript.sh) The easiest way to run the script is to type bash myscript.sh, sh myscript.sh or./myscript.sh You can put scripts in a central directory (\scripts) and add to your path and change permissions.
White space - dos2unix jrw@tambala ~/scripts $ od -c makedir.sh 0000000 f o r ( ( i = 1 ; i < = 0000020 9 ; i + + ) ) \r \n d o \r \n 0000040 m k d i r $ i \r \n d o n e \r 0000060 \n 0000061 jrw@tambala ~/scripts $ dos2unix.exe makedir.sh dos2unix: converting file makedir.sh.txt to Unix format... jrw@tambala ~/scripts $ bash makedir.sh
Kill and suspend (^ is the control key ^c means hit both control and c key together press control, and then c at the same time. ) ^C kills a command ^Z suspends the command.
Advice use the tab key to auto-complete commands and arguments. semi-colon ; can connect two commands. pipe send output of one command to input of next command. man (manual) command to find out about commands. Windows confirms almost everything, Unix confirms almost nothing - so be careful (unless you use interactive mode).
Input/Output (py) userinput = raw_input( Type in a any input') print You typed "+userinput Gives output Type in a any input: this is my input You typed: this is my input #cf. java
Exec command (py, sh) Many scripting languages treat the programs as text strings. Therefore you can construct a string and execute it. mycmd = "print 'hi'" exec mycmd
Eval (py) userequation= raw_input('type in a simple maths equation in one variable x e.g. x+1') print "you typed "+userequation x = raw_input('type in an integer value for x') print "x is "+ x x = int(x) y = eval(userequation) print y
Improved (py) userequation= raw_input('type in a simple maths equation in one variable x e.g. x+1') print "you typed "+userequation x = raw_input('type in a positive integer value for x: ') print "x is "+ str(x) if (x.isdigit()):#chech is it a positive integer x = int(x) y = eval(userequation) print y else: print "you did not enter a number"
Example output type in a simple maths equation in one variable x e.g. x+1: x**2+1 you typed x**2+1 type in a positive integer value for x: 3 x is 3 10
Small Project Write a program which takes two inputs 1. a string to be matched 2. a regular expression Type in a any string to match: The cat s@t on the mat You typed: The cat s@t on the mat type in a regular expression e.g. ^...$: (t ).*(t ) the pattern you typed: (t ).*(t ) you regex command is if re.search(r"(t ).*(t )", userstring ):print "matched" matched
The Program import re userstring = raw_input('type in a any string to match: ') print "You typed: "+userstring userregexp = raw_input('type in a regular expression e.g. ^...$: ') print "the pattern you typed: "+userregexp regexpcmd = 'if re.search(r"'+userregexp+'", userstring ):print "matched"' print "your regex command is "+regexpcmd exec regexpcmd
Example output Type in a any string to match: "cat" You typed: "cat" type in a regular expression e.g. ^...$: \"cat\" the pattern you typed: \"cat\" your regex command is if re.search(r"\"cat\"", userstring ):print "matched" matched
Question How easy is that to do in java? Could you do it? Can the program be improved? E.g. how do we know the user types in a regular expression??? What is dangerous about the exec command?
Exec and eval (py) Exec executes a command Eval also executes a command but stored the value y = eval(strfunbrackets) You could do this with exec? how
Eval def inc( intx ): return intx+1 strfun = "inc" strarg = "55" strfunbrackets = strfun +"("+ strarg +")" print "strfunbrackets "+strfunbrackets y = eval(strfunbrackets) print y Output is strfunbrackets inc(55) 56
Exec can replace eval (py) def inc( intx ): return intx+1 strfun = "inc" strarg = "55" strfunarg = "output = "+ strfun +"("+ strarg +")" print strfunarg exec strfunarg print "value of output " + str(output)
More Scripting A program (java, python) is just a text file. We can manipulate text files easily (with python, sh) Therefore we can manipulate programs themselves. This is my research areas ask me for details
A simple python program (py) print "hi - this is a python script" for x in range(0, 5): print "x is " + str(x)
Build a python program (sh) 1 #a script to manipulate a python program. #writes two lines to a file mypython.py echo "for x in range(0, 5):" > mypython.py echo " print \"x is \" + str(x)" >> mypython.py #executes the python file mypython.py python mypython.py
Build a python program (sh) 2 #a script to manipulate a python program. #asks for a message echo "enter message" read usermessage #asks for number of times to print message echo "enter number times you want message printed" read usertimes #writes the message to a file mypython.py echo "message=\""$usermessage"\""> mypython.py #writes a for-loop to file mypython.py echo "for x in range(0, "$usertimes" ):" >> mypython.py echo " print str(message)" >> mypython.py #executes python mypython.py python mypython.py
A simple java program (java) class HelloWorld { public static void main(string[] args) { System.out.println("HelloWorld!"); } }
compexecprintprog.sh #compiles the file printprog.java javac printprog.java #executes the file printprog.class java printprog
compexecprintprogarg.sh #run as #sh compexecprintprogarg.sh HelloWorld # HelloWorld is stored in the variable $1 #complile code javac $1.java #runs code java $1
buildjavaprog.sh 0 #asks for a string echo "enter a string to print in java" #reads user input and stores in variable userinput read userinput #writes first two lines of printprog.java to newprog.java head -2 printprog.java > newprog.java #writes the following line to newprog.java echo "System.out.println(\"" $userinput "!\");" >> newprog.java #writes last two lines of printprog.java to newprog.java tail -2 printprog.java >> newprog.java #replaces newprog.java with printprog.java mv newprog.java printprog.java
buildjavaprog.sh 1 #asks for a string echo "enter a string to print in java" #reads user input and stores in variable userinput read userinput #writes first two lines of printprog.java to newprog.java head -2 printprog.java > newprog.java
buildjavaprog.sh 2 #writes the following line to newprog.java echo "System.out.println(\"" $userinput "!\");" >> newprog.java #writes last two lines of printprog.java to newprog.java tail -2 printprog.java >> newprog.java #replaces newprog.java with printprog.java mv newprog.java printprog.java
We can glue it all together 0 #executes the script sh buildjavaprog.sh sh buildjavaprog.sh #captures the output of sh compexecprintprog.sh #and stores output in outputjava outputjava=$(sh compexecprintprog.sh) #prints the output of outputjava echo "output from java program: "$outputjava #asks for number of times to print message echo "enter number of times" #read in user input #and stores in variable usertimes read usertimes #writes value of usertimes to py3input.txt echo $usertimes > py3input.txt #py3input.txt provides input to py3.sh sh py3.sh < py3input.txt #executes the python file mypython.py python mypython.py
We can glue it all together 1 #executes the script sh buildjavaprog.sh sh buildjavaprog.sh #captures the output of sh compexecprintprog.sh #and stores output in outputjava outputjava=$(sh compexecprintprog.sh) #prints the output of outputjava echo "output from java program: "$outputjava #asks for number of times to print message
We can glue it all together 2 echo "enter number of times" #read in user input #and stores in variable usertimes read usertimes #writes value of usertimes to py3input.txt echo $usertimes > py3input.txt #py3input.txt provides input to py3.sh sh py3.sh < py3input.txt #executes the python file mypython.py python mypython.py
Summary Scripts are interpreted. Good for repeated tasks. We covered some basic commands. You can use them to glue other programs together