Scripting Languages Course 1. Diana Trandabăț

Similar documents
The Directory Structure

INSE Lab 1 Introduction to UNIX Fall 2017

UNIX Tutorial Two

Parts of this tutorial has been adapted from M. Stonebank s UNIX Tutorial for Beginners (

A Short Introduction to UNIX Operating System

A Brief Introduction to the Linux Shell for Data Science

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

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Operating Systems. Copyleft 2005, Binnur Kurt

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

Introduction to Unix: Fundamental Commands

Mills HPC Tutorial Series. Linux Basics I

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

Unix Tutorial Haverford Astronomy 2014/2015

5/8/2012. Creating and Changing Directories Chapter 7

Unix/Linux Primer. Taras V. Pogorelov and Mike Hallock School of Chemical Sciences, University of Illinois

Introduction: What is Unix?

UNIX Tutorial One

Linux Tutorial #4. Redirection. Output redirection ( > )

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

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

5/20/2007. Touring Essential Programs

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

Using the Unix system. UNIX Introduction

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Physics REU Unix Tutorial

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.

Chapter-3. Introduction to Unix: Fundamental Commands

Perl and R Scripting for Biologists

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

The Shell. EOAS Software Carpentry Workshop. September 20th, 2016

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

Introduction to Linux

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Practical Session 0 Introduction to Linux

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

Lab Working with Linux Command Line

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

CS246 Spring14 Programming Paradigm Notes on Linux

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

1 Getting Started with Linux.

Shell Programming Overview

CSCI 2132 Software Development. Lecture 4: Files and Directories

Introduction to Linux Workshop 1

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

Unix basics exercise MBV-INFX410

Introduction of Linux

Computer Systems and Architecture

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

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

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Working with Basic Linux. Daniel Balagué

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

Files and Directories

CS197U: A Hands on Introduction to Unix

An Introduction to Unix Power Tools

Introduction to Linux. Fundamentals of Computer Science

System Programming. Introduction to Unix

Introduction to Linux

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Common File System Commands

GNU/Linux Course Lesson 1. Puria Nafisi

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. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Computer Systems and Architecture

Overview of the UNIX File System

The Unix Shell. Pipes and Filters

Overview of the UNIX File System. Navigating and Viewing Directories

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

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Introduction. File System. Note. Achtung!

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

Chapter 4. Unix Tutorial. Unix Shell

CS 460 Linux Tutorial

The Unix Shell & Shell Scripts

Files

Welcome to Linux. Lecture 1.1

Oxford University Computing Services. Getting Started with Unix

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

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

5/8/2012. Specifying Instructions to the Shell Chapter 8

COMS 6100 Class Notes 3

Std: XI CHAPTER-3 LINUX

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Linux Tutorial. Ken-ichi Nomura. 3 rd Magics Materials Software Workshop. Gaithersburg Marriott Washingtonian Center November 11-13, 2018

Linux Command Line Interface. December 27, 2017

Introduction to Linux

Part 1: Basic Commands/U3li3es

CS370 Operating Systems

5/8/2012. Exploring Utilities Chapter 5

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Introduction to Linux

Introduction to Linux. Roman Cheplyaka

Chapter 1 - Introduction. September 8, 2016

UNIX files searching, and other interrogation techniques

CS Fundamentals of Programming II Fall Very Basic UNIX

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Transcription:

Scripting Languages Course 1 Diana Trandabăț Master in Computational Linguistics - 1 st year 2017-2018

Today s lecture Introduction to scripting languages What is a script? What is a scripting language Categories of scripting languages Introduction to Unix Basic commands File management

Today s lecture Introduction to scripting languages What is a script? What is a scripting language Categories of scripting languages Introduction to Unix Basic commands File management

What is a script? A script is what you give the actors. A program is what you give the audience. - Larry Wall The distinctions between programs and scripts are not welldefined. Script may be seen as a colloquial term.

What are scripting languages? A scripting language or script language is a programming language It supports scripts, programs written for a special run-time environment They are interpreted (rather than compiled) They include high-level data types, such as lists, associative arrays etc. Common scripting languages : shell, awk, Perl, Python, PHP, Ruby, etc.

Categories Four main usage areas for scripting languages: Command scripting languages Application scripting languages Markup languages Universal scripting languages

Command scripting languages The oldest class of scripting languages (1960) JCL (Job Control Language), created for IBM OS/360 operating system. Modern examples include shell language and text-processing languages, such as sed and awk. Were one of the first to directly include support for regular expression matching.

Application scripting languages Developed in 1980s One of the prime examples is Microsoft-created Visual Basic language, and it s subset Visual Basic for Applications designed for office applications programming, the universal single language for programming Microsoft Office suite. Javascript language also belongs to this class. a de-facto standard for implementations of the client parts of web-programming projects.

Markup Languages Not real programming languages A set of special command words called tags used to mark up parts of text documents later used by special programs to do transformations to the text, such as displaying it in a browser, or converting it to some other data format. Basic idea: separation of contents and structure, and including formatting commands and interactive objects into the documents. Well-known examples: TeX, HTML, XML

Universal scripting languages The languages that belong to this class are the most wellknown scripting languages. Sometime scripting languages refers only to this category Most of them were originally created for the Unix environment. Examples include: Perl, Python, Tcl, PHP, Ruby

Shell scripting language Interactive command-line interpreter of the Unix OS. Includes programming capabilities (if-then, for, while, variables, functions, etc.) Simple shell programs are easy to write. Shell programs generally rely on running and processing the output of external programs, such as Unix filters (sort, wc, grep and so on) and text processing mini-languages, such as awk and sed. Advantage: available on all Unix variants Disadvantage: shell isn t really suitable for any programming tasks, but the simplest ones.

Perl developed by Larry Wall (late 80s). dynamic arrays and the hash type regular expressions dedicated large community around it CPAN: Database of modules can be found on almost every Unix installation. Perl is also available and quite well documented on Microsoft platforms. harder to get started in Perl than in shell. use of many Perl function makes however the code difficult to read

Today s lecture Introduction to scripting languages What is a script? What is a scripting language Categories of scripting languages Introduction to Unix Basic commands File management

UNIX Introduction UNIX is an operating system Suite of programs which make the computer work Made up of three parts: Kernel Shell Programs

The UNIX operating system Kernel Hub of the operating system Allocates time and memory to programs and handles the file storage and inter-process communication Shell Interface between user and kernel Starts when the user logs in to the system The shell is a command line interpreter (CLI) Shell flavors: Tcsh Csh Bash

Files and processes Everything in UNIX is either a file or a process A process is an executing program identified by a unique PID (process identifier) A file is a collection of data. They are created by users using text editors, running compilers etc. Example of files: documents, directory, etc.

The Directory Structure file-system is arranged in a hierarchical structure, like an inverted tree top of the hierarchy is traditionally called "root After typing each of the following commands, along with their arguments, press the RETURN key to execute the command.

Listing files and directories ls (list) When you first login, your current working directory is your home directory To find out what is in your home directory, type ls The ls command lists the contents of your current working directory To list all files in your home directory including those whose names begin with a dot, type ls -a

Making Directories mkdir (make directory) We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called unixstuff in your current working directory type mkdir unixstuff To see the directory you have just created, type ls

Changing to a different directory cd (change directory) The command cd <directory> means "change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree

The directories. and.. In UNIX, "." means the current directory, so typing ".." means the parent of the current directory Enter the unixstuff directory you created using cd unixstuff Now return to the home directory using cd..

Pathnames pwd (print working directory) Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type pwd

~ (your home directory) Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing ls ~/unixstuff will list the contents of your unixstuff directory, no matter where you currently are in the file system

Recap ls list files and directories ls a list all files and directories mkdir make a directory cd directory change to named directory cd change to home-directory cd ~ change to home-directory cd.. change to parent directory pwd display the path of the current directory

Copying Files cp (copy) cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2

Moving files mv (move) mv file1 file2 moves (or renames) file1 to file2 To move a file from one place to another, use the mv command It can also be used to rename a file, by "moving" the file to the same directory, but giving it a different name

Removing files and directories rm (remove), rmdir (remove directory) To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the science.txt file then delete it

Displaying the contents of a file on clear (clear screen) the screen Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood At the prompt, type clear

Displaying the contents of a file on the screen Create a file in your working directory, which should contain at least 15 lines (don t try to put a whole novel on your file just yet, start with a small example) In the following examples, replace filename.txt with the name of the file, along with the extension. Don t forget the extension! UNIX is case sensitive, that means MyFile.txt is different from myfile.txt and from MYFILE.txt If you have spaces in your file s name, put the filename in quotation marks, i.e. My File.txt, but I recommend to avoid using spaces. You may use _ to separate words.

Displaying the contents of a file on cat (concatenate) the screen The command cat can be used to display the contents of a file on the screen. Type cat filename.txt less The command less writes the contents of a file onto the screen a page at a time. Type less filename.txt Press the [space-bar] if you want to see another page, and type q if you want to quit reading.

Displaying the contents of a file on head the screen The head command writes the first ten lines of a file to the screen. First clear the screen then type head filename.txt Now type head -5 filename.txt What difference do you notice? tail The tail command writes the last ten lines of a file to the screen. Type tail filename.txt How can you view the last 15 lines of a file?

Searching the contents of a file Simple searching using less Using less, you can search though a text file for a keyword (pattern). For example, to search through filename.txt for the word test, type less filename.txt then, still in less (i.e. don't press q to quit), type a slash followed by the word to search (make sure the word appears in your file at least twice) /test Type n to search for the next occurrence of the word and q to quit.

Searching the contents of a file grep (don't ask why it is called grep) grep is one of many standard UNIX utilities. It searches files for specified words or patterns Clear the screen and then type grep test filename.txt As you can see, grep has printed out each line containing the word test. Or has it???? Try typing grep Test filename.txt The grep command is case sensitive; it distinguishes between Test and test. To ignore upper/lower case distinctions, use the -i option grep -i test filename.txt

Searching the contents of a file Some of the other options of grep are: -v display those lines that do NOT match -n precede each maching line with the line number -c print only the total count of matched lines Run the grep command using different parameters, replacing the word test with one word you want to see find in your file (the word should appear at least twice in your file) grep c test filename.txt grep n test filename.txt you can use more than one option at a time. For example, the number of lines without the words test or Test is: grep -ivc test filename.txt

Searching the contents of a file To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type grep -i 'spinning top' filename.txt

Counting the number of words A handy little utility is the wc command, short for word count. To do a word count on filename.txt, type wc -w filename.txt To find out how many lines the file has, type wc -l filename.txt

Redirection Most processes initiated by UNIX commands write to the standard output (terminal screen) Many take their input from the standard input (keyboard) There is also the standard error, where processes write their error messages, by default, to the terminal screen

Redirecting the Output We use the > symbol to redirect the output of a command: cat > list1 Then type in the names of some fruit. Press [Return] after each one pear banana apple ^D (Control D to stop) Now type cat list1 to check the content of your file.

Redirecting the Output (cont.) The form >> appends standard output to a file Type cat >> list1 then type oranges and check again the content of the file with cat list1

Redirecting the Output (cont.) Create a second file, named list2, which contains the following words: orange, plum, mango, grapefruit. Read the content of list2 on the screen. Now join (concatenate) the two files, list1 and list2, into a file named biglist. cat list 1 list 2 > biglist then type cat biglist

Redirecting the Input We use the < symbol to redirect the input of a command The command sort alphabetically or numerically sorts a list. Type sort Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type sort < biglist Now, how about writing the result of the sort command to a file named sorted_biglist?

Pipes To see who is on the system with you, type who One method to get a sorted list of names is to type, who > names.txt sort < names.txt What you really want to do is connect the output of the who command directly to the input of the sort command

Pipes The symbol for a pipe is For example, typing who sort Will give you the same result al before, without writing the output to a file. What if you want to have the output written to a file?

Exercise Using pipes, display all lines of list1 and list2 containing the letter p and sort the result.

Exercise Using pipes, display all lines of list1 and list2 containing the letter p and sort the result. cat list1 list2 grep p sort

Wildcards The characters * and? The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name For example, in your unixstuff directory, type ls list* This will list all files in the current directory starting with list... Try typing ls *list This will list all files in the current directory ending with...list

Wildcards The characters * and? The character? will match exactly one character So?ouse will match files like house and mouse, but not grouse. Try typing ls?list

Filename conventions We should note here that a directory is merely a special type of file Rules and conventions for naming files apply also to directories Characters with special meanings such as /,*,&,% etc., should be avoided Avoid using spaces within filenames Safest way to name a file is to use only alphanumeric characters

Getting Help On-line Manuals There are on-line manuals which gives information about most commands Tells you which options a particular command can take, and how each option modifies the behavior of the command Type man grep and q to quit. Then type whatis grep to see the difference

Recap command > file redirect standard output to a file command >> file append standard output to a file command < file redirect standard input from a file command1 command2 pipe the output of command1 to the input of command2 cat file1 file2 > file0 concatenate file1 and file2 to file0 sort sorts data who list users currently logged in

References UNIX Tutorials for Beginnershttp://www.ee.surrey.ac.uk/Teaching/Unix/unix0.html

Great! See you next time!