• No results found

Scripting for Life Science Researchers

N/A
N/A
Protected

Academic year: 2021

Share "Scripting for Life Science Researchers"

Copied!
52
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction

Mihai Lefter

(2)

• Simple tasks automation:

• Run a series of programs (commands) successively.

• Scripts = programs in their own right:

• Allow the use of real programming functions. • Pass arguments to commands.

• Reusable and scalable.

• Far more useful functions through command substitution. CAN YOU PASS

THE SALT?

I SAID

-I KNOW! -I'M DEVELOP-ING A SYSTEM TO PASS YOU ARBITRARY CONDIMENTS.

IT'S BEEN 20 MINUTES!

(3)
(4)

Basic Linux Concepts Recap • Terminal, shell, and kernel. • The filesystem:

• Special directories. • Absolute/relative paths.

(5)

Applications

Applications

cat cd mines sol >_ terminal User Operating System Hardware Kernel

Shell A program that helps runother programs. Command interpreter.

(6)

terminal

>_

User

Kernel

(7)

terminal

>_

User

Kernel

Shell IBM 1620 Model 1 console

(Wikipedia)

History:

a piece of equipment through which you could interact with a computer. teletypewriter - "tty".

(8)

terminal >_ User Kernel Shell History:

a piece of equipment through which you could interact with a computer. teletypewriter - "tty".

console - furniture point of view. terminal - electronic point of view:

(9)

terminal

>_

User

Kernel

Shell

Terminal, GNOME Terminal, XTerm, KDE Konsole, ...

History:

a piece of equipment through which you could interact with a computer. teletypewriter - "tty".

console - furniture point of view. terminal - electronic point of view.

Today:

(10)

terminal

>_

User

Kernel

Shell

An ordinary program, you can choose the one that you like. Examples:

sh

bash The Bourne-Again shell: can

be found installed and is the default interactive shell on most Linux systems. The basic shell: the original small program shell with few features, still used on UNIX systems.

csh Its syntax resembles that of the C programming language.

tcsh TENEX C shell: a superset of

the common C shell, enhancing user-friendliness and speed (Turbo C shell).

ksh The Korn shell: a superset of the Bourne shell (a nightmare for beginners).

zsh The Z shell: an extended Bourne shell including some features of ksh, and tcsh.

(11)

Everything is a file

Applications Applications

(12)

Special directories

bin home usr var

bash znew

bin games src

Documents Videos

2to3 zxpdf espdiff mines sol

/

zorro gulliver

The root directory marks the begining of the hierarchical filesystem. Sometimes referred to as the trunk, it is denoted by /.

The current (working) directory is the directory in which a user is working at a given time. Every user is always working within a directory.

file1 fileZ

Important

(13)

Absolute paths

For the root folder - signalizes an absolute path.

bin home var

bash znew

bin src

Documents Videos

2to3 zxpdf espdiff mines sol

/ zorro gulliver file1 fileZ Important start / / stop usr games

The path to mines from root is absolute:

(14)

Relative paths

bin var

bash znew

bin src

Videos

2to3 zxpdf espdiff mines sol

/ zorro gulliver file1 fileZ Important home Documents usr games stop start / /

The path to mines from important is relative:

(15)

Quiz 1 - Q1

bin home usr var

bash znew

bin games src

Documents Videos

2to3 zxpdf espdiff mines sol

/ zorro gulliver file1 fileZ Important stop start

What's the relative path to

(16)

Quiz 1 - Q1

bin home usr var

bash znew

bin games src

Videos

2to3 zxpdf espdiff mines sol

/

zorro gulliver

file1 fileZ

Important The path to ../Documents/Importantimportant from videos is:

(17)

Quiz 1 - Q2

bin home usr var

bash znew

bin games src

Documents Videos

2to3 zxpdf espdiff mines sol

/

zorro gulliver

file1 fileZ

Important

(18)

Quiz 1 - Q2

bin usr var

bash znew

bin games src

Documents Videos

2to3 zxpdf espdiff mines sol

/

zorro gulliver

file1 fileZ

Important The absolute path of /home/gulliver/Documents/file1file1 is:

(19)

Format

command[option]... [argument]...

Most input lines have three basic elements:

• command - executing program (application) name (path). • option(s)- modify what the command may do.

• argument(s)- what the command operates on.

Some notes:

• The commandmay be followed by one or more options.

• Options usually start with one or two dashes.

• Plenty commands have nooptions, noarguments, or neither.

(20)

Basic Commands Overview

Navigate through the filesystem

Prints the absolute path of the current/working directory. ls[option]...[directory]...

Lists information about thedirectoryin alphanumeric order.

If nodirectoryis specified theworking directoryis assumed.

cd[directory]

(21)

Navigate through the filesystem pwd[option]...

Prints the absolute path of the current/working directory. ls[option]...[directory]...

Lists information about thedirectoryin alphanumeric order.

If nodirectoryis specified theworking directoryis assumed.

cd[directory]

(22)

Basic Commands Overview

Manipulate files and directories

Create newdirectories.

cp[option]...[source]... [destination]

Copies thesourceto the destination.

mv[option]...[source]... [destination]

Move or renamesource todestination.

rm[options]...[file/directory]...

Remove specifiedfiles or directories.

(23)

Manipulate files and directories mkdir[option]..[directory]...

Create newdirectories.

cp[option]...[source]... [destination]

Copies thesourceto the destination.

mv[option]...[source]... [destination]

Move or renamesource todestination.

rm[options]...[file/directory]...

Remove specifiedfiles or directories.

(24)

Basic Commands Overview

Inspect files content

View the entire content of the inputfiles.

more[option]...[file]...

View thefile content one page at a time.

less[option]...[file]...

View thefile content with scrolling.

head[option]...[file]...

Print the first 10 lines of eachfile.

tail[option]...[file]...

Print the last 10 lines of eachfile.

head/tail-number[file]...

(25)

Inspect files content cat[option]...[file]...

View the entire content of the inputfiles.

more[option]...[file]...

View thefile content one page at a time.

less[option]...[file]...

View thefile content with scrolling.

head[option]...[file]...

Print the first 10 lines of eachfile.

tail[option]...[file]...

Print the last 10 lines of eachfile.

head/tail-number[file]...

(26)

Basic Commands Overview

Search and count

Outputs every path that contains the providedpattern.

find[path]...[option]...[expression]

Search for files and directories based on the givenexpression.

grep[option]...[pattern] [file/directory]...

Search text files for the pattern occurrence and output any line

containing a match. wc[option]...[file]...

(27)

Search and count

locate[option]...[pattern]

Outputs every path that contains the providedpattern.

find[path]...[option]...[expression]

Search for files and directories based on the givenexpression.

grep[option]...[pattern] [file/directory]...

Search text files for the pattern occurrence and output any line

containing a match. wc[option]...[file]...

(28)

Basic Commands Overview

Other useful commands

Summarize disk usage of the set offiles/directories.

echo[option]...[text]

Printtext to screen.

ssh[option]...[user@]hostname

Connect asuserto the hostname machine.

wget[option]...[url]...

Download file located aturl.

history

See the previous executed commands.

man[command]

(29)

Other useful commands

du[option]...[file/directory]...

Summarize disk usage of the set offiles/directories.

echo[option]...[text]

Printtext to screen.

ssh[option]...[user@]hostname

Connect asuserto the hostname machine.

wget[option]...[url]...

Download file located aturl.

history

See the previous executed commands.

man[command]

(30)

Quiz 2

(31)

Quiz 2

(32)

Quiz 2

What is the output of thegrep command from below?

$cat wines_list.txt

Chardonnay Sauvignon Blanc

Pinot Gris & Pinot Blanc Riesling & Gewrztraminer Cabernet sauvignon Zinfandel

(33)

Quiz 2

What is the output of thegrep command from below?

$cat wines_list.txt

Chardonnay Sauvignon Blanc

Pinot Gris & Pinot Blanc Riesling & Gewrztraminer Cabernet sauvignon Zinfandel

$grep ’Sauvignon’ wines_list.txt

(34)

• CLI:

• Nano: an easy to use text editor for both new and advanced

Linux users.

• Vi/Vim: one of the most popular among System

Administrators and programmers.

• Emacs: a highly extensible and customizable text editor.

• ...

• GUI:

• Gedit: a general purpose text editor installed by default on

Gnome.

• Kate: a feature rich and highly pluggable text editor that comes with KDE.

(35)

Filename expansion

$ # List all files $ls

DC_000100.jpgsssDocumentssssssssDownloadssssssssflower.jpg humansssssssssssgenessssssssssssMusicsssssssssssprograms ...

projectsssssssssTemplatessssssssVideosssssssssssZagreb.jpg

$ # List all JPEG files $ls *.jpg

(36)

Wildcards (meta-characters)

Characters that the shell will expand to match existing pathnames in the filesystem.

* Match zero or more characters.

? Match exactly one character.

[chars] Match any character listed in the brackets.

[!chars] Match any character that does not appear within the brackets.

[a-z] Match all characters between a and z, in a case-sensitive manner, based on the characters’ value in the ASCII character set.

(37)

Examples

$ # List all JPEG files with one character names $ls ?.jpg

$ # List all files starting with ’file’ and ending in a number. $ls file*[0-9]

(38)

How to escape wildcards

• Use the non-quoted backslash\.

• Use quotes - more on this in the next lecture.

$ls

flower.jpgsss*.jpgssssssssParis.jpgssssZagreb.jpg

$rm \*.jpg

$ls

(39)

Quiz 3

List all files starting with a small letter and ending in a number.

$

List all files that do not start with c, have r or L as second character, and end in a number.

$

List all files that have exactly 3 characters.

$

List all files that contain the * character.

(40)

Quiz 3

List all files starting with a small letter and ending in a number.

$ls [a-z]*[0-9]

List all files that do not start with c, have r or L as second character, and end in a number.

$ls [!c][rL]*[0-9]

List all files that have exactly 3 characters.

$ls ???

List all files that contain the * character.

(41)

• Makes it possible to control:

• where the output of a command goes to. • where the input of a command comes from.

(42)

Pipelining

• Redirect a command output as input to another command.

terminal

>_

command 1

output

(43)

Operators command> file

Redirectcommand output to the file.

Thefile is created or overwritten if already existing.

command>> file

Redirect and appendcommand output to the file.

Thefile is created if not existing.

command< file

Redirect input tocommand from fileinstead of stdin.

command 1|command 2

(44)

Example I

$ls

DocumentssssDownloads

$ls >list.txt;catlist.txt

Documents Downloads

$ls >>list.txt;catlist.txt

(45)

Example II

$ls

abba_poster.jpgsssdancing_queen.FLACssslist.txt ...

sos.mp3ssssssssssswaterloo.mp3 $ls |less

$ls |grep ’mp3’ >my_music_collection.txt

(46)

Going deeper - file descriptors command terminal >_ stdin stdout stderr File descriptors (FD) Handles used to access a file or

other input/output resource.

FD 0

FD 1

FD 2

command[file_description_number]redirection_operator file

• File description numbers implicit for stdin and stdout.

$cat file11> file2 # same as below

(47)

Standard error output

• Very popular Linux feature.

• Many commands provide great amounts of errors.

• Avoid messages cluttering up the normal program output while executing scripts.

$mv

mv: missing file operand

Try ’mv --help’for more information. $mv 2>error.log

$cat error.log

mv: missing file operand

(48)

Redirect both stdout and stderr to same file

$grep something somewhere> file_out 2>&1

In the above:

• stdout is redirected to file_out.

• stderr is redirected to stdout. Alternatives:

$grep something somewhere&> file_out

(49)

/dev/null- the black hole

• Everything written to it disappears.

• Attempts to read/output to/from it result in nothing.

$cat file> /dev/null

Contents of thefilewill not be listed to stdout.

$cat /dev/null>file

Deleting file contents, but preserving the file itself with all its

(50)

Quiz 4

Write the text “Hello world!” to file “hello.txt”:

$

Append the contents of “file1” and “file2” into “file3”:

$

Print the previous executed “ssh” commands:

(51)

Quiz 4

Write the text “Hello world!” to file “hello.txt”:

$echo ’Hello world!’> hello.txt

Append the contents of “file1” and “file2” into “file3”:

$cat file1 file2>> file3

Print the previous executed “ssh” commands:

(52)

Jeroen Laros Magnus Palmblad Jonathan Vis Mihai Lefter

References

Related documents

• 0-19 points for students who show no active participation, who do not participate in group discussions, are

Bluetooth data transfer protocol „rSAP” can be paired to the operating and display unit of the MIB (with „Premium” mobile telephone interface).. All mobile radio connections of

Experiments conducted to study the effect of Pseudomonas fluorescens (IISR-6) and Tricho- derma harzinaum (P-26) on growth of black pepper ( Piper nigrum ) rooted cuttings in

•Use vnode numbers as file descriptors and add a file pointer to the

Descriptor 1 (stdout stdout) points to terminal, and ) points to terminal, and descriptor 4 points to open disk file. descriptor 4 points to open

To make programs really useful we have to be able to input and output data in large machine- readable amounts, in particular we have to be able to read and write to files.. Since

interpreter_do extract_feature world interpreter (status) (status) (C++ (C++ DMIS input file DMIS output file command interface driver terminal or file dummy world model

Percent Respondents 0 10 20 30 40 50 60 70 80 90 100 &gt;6 months 2-6 months 1-2 months &lt;1 m Nonexclusive License Informal Material Transfer Agreement Exclusive