Basics. proper programmers write commands

Similar documents
Introduction to Functional Programming and Scripting (Exam Revision) John Woodward

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Introduction: What is Unix?

You should see something like this, called the prompt :

Linux Shell Script. J. K. Mandal

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Advanced Linux Commands & Shell Scripting

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Scripting Languages Course 1. Diana Trandabăț

Introduction to Unix: Fundamental Commands

Chapter 4. Unix Tutorial. Unix Shell

Linux Command Line Interface. December 27, 2017

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Lab Working with Linux Command Line

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Introduction to Linux. Fundamentals of Computer Science

LING 408/508: Computational Techniques for Linguists. Lecture 5

Chapter-3. Introduction to Unix: Fundamental Commands

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Shell Programming Overview

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

A Brief Introduction to the Linux Shell for Data Science

Introduction to Supercomputing

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

Week 2 Lecture 3. Unix

Guided Tour (Version 3.3) By Steven Castellucci as Modified by Brandon Haworth

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Introduction to Linux

Part 1: Basic Commands/U3li3es

Lab 2: Linux/Unix shell

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

5/20/2007. Touring Essential Programs

Introduction to Linux

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

Chap2: Operating-System Structures

CSE 390a Lecture 5. Intro to shell scripting

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

Operating Systems. Copyleft 2005, Binnur Kurt

Unix Introduction to UNIX

Introduction to Linux

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Shell Programming Systems Skills in C and Unix

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

CENG 334 Computer Networks. Laboratory I Linux Tutorial

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

The Unix Shell & Shell Scripts

UNIX Essentials Featuring Solaris 10 Op System

Unix. 1 tblgrant projects 0 Jun 17 15:40 doc1.txt. documents]$ touch doc2.txt documents]$ ls -l total 0

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

Linux File System and Basic Commands

Introduction to Unix and Linux. Workshop 1: Directories and Files

Introduction to Linux Basics

Prof. Navrati Saxena TA: R. Sachan

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Basic Linux (Bash) Commands

Perl and R Scripting for Biologists

A Brief Introduction to Unix

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Unix Basics. Benjamin S. Skrainka University College London. July 17, 2010

CISC 220 fall 2011, set 1: Linux basics

5/8/2012. Exploring Utilities Chapter 5

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

CS Unix Tools & Scripting

CS370 Operating Systems

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

Linux Command Line Primer. By: Scott Marshall

Introduction to Linux Workshop 1

Introduction to the UNIX command line

Linux & Shell Programming 2014

Unix basics exercise MBV-INFX410

UNIX Shell Programming

Introduction To. Barry Grant

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Introduction of Linux

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

CSE 391 Lecture 5. Intro to shell scripting

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Introduction to Linux

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Mills HPC Tutorial Series. Linux Basics I

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

Lesson 04: Our First Java Program (W01D4

Working with Basic Linux. Daniel Balagué

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Transcription:

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