Docsity

Shell Scripts - Bioinformatics - Lecture Slides, Slides of Bioinformatics

Toggle sidebar
bg1
Shell Scripts
•A shell usually interprets a single line of input,
but we can also create a file containing a
number of lines of commands to be
interpreted
–This file is a program known as a shell script
–The program can also contain control structures
(if-then, loops)
–Shell scripts allow a sequence of commands to be
executed automatically (e.g. installation of a
program - see /user_client/sybase/install)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Shell Scripts - Bioinformatics - Lecture Slides and more Slides Bioinformatics in PDF only on Docsity!

Shell Scripts

• A shell usually interprets a single line of input,

but we can also create a file containing a

number of lines of commands to be

interpreted

  • This file is a program known as a shell script
  • The program can also contain control structures

(if-then, loops)

  • Shell scripts allow a sequence of commands to be

executed automatically (e.g. installation of a

program - see /user_client/sybase/install)

CSH Input Processing

• CSH transforms each command before it is

executed by applying the following steps:

  • History substitutions
  • Alias substitutions
  • Variable substitutions
  • Command substitutions
  • File name expansions

A Simple Example

#this script consults an on-line telephone database grep -i $1 $HOME/phonenumbers

  • To make this script executable, chmod +rx
  • Now it can be run from the command line

A comment A positional parameter An environment variable

Shell Script Basics

• A shell script consists of a sequence of built-in

and nonbuilt-in commands separated by ; or

NEWLINE

• Comments begin with a

• Script execution is aborted if a built-in

command fails, goes to next command if a

nonbuilt-in command fails.

  • Remember - built-in commands (e.g. alias) are part of the shell, nonbuilt-in commands (e.g. ls) are separate executable programs.

Positional Parameters

• Parameters can be passed to the script from

the command line.

  • Inside the script, these parameters may be

referenced by using the positional parameters $0,

$1, $2, etc.

  • $0 refers to the command name (the name of the

shell script)

CSH Scripts

• The CSH scripting language is based on the C

language.

  • The positional parameters can alternatively be

referred to as $argv[1], $argv[2], etc.

  • $argv[*] is a list of all of the arguments given.
  • $#argv is the number of arguments given.
  • $argv[0] is undefined. Why?

Control Flow in CSH Scripts

• The foreach command is used to execute a list

of commands for each component of a list of

words:

foreach var ( wordlist ) commandlist end

  • Each time through the loop the variable var is

assigned a word from wordlist, and the command

list is executed

The foreach Command

#!/bin/csh

foreach x ( * )

echo Changing mode for $x chmod +rx $x

end

  • Consider changing mode only for .exe files.

The if Command

##check and set parameters

if ( $#argv > 2 || $#argv < 1 ) then

echo usage: “$0 [ from-file ] to-file” exit(1);

else

if ( $#argv == 2 ) then set from = $argv[1] set to = $argv[2] else set to = $argv[1] endif

endir

The switch Command

  • A multiway alternative is provided by the switch

command:

switch ( str ) case pattern1 : commandlist breaksw case pattern2 : commandlist breaksw … default: commandlist endsw

The while Command

• The while command executes commands until

an expression evaluates to zero:

while ( expr ) commandlist end

• Example

set i = $#argv while ($i) echo $argv[$i] @ i-- end

Numerical Computations

  • Notice the ‘@’ in front of the i-- this allows a

variable to take on a numeric value (otherwise

script variables are string-valued)

@ var = expr @ var[n] = expr

  • Examples:

@ x = $#argv/ @ argv[$j] = $j + 4 @ x += 3 @ i++

Expressions

  • Expressions in csh are similar to those in C:
    • Logical operators are the same (!, ||, &&)
    • Relational operators are the same with the addition of =~ for string match and !~ for string mismatch
    • Binary arithmetic operators are the same
    • Bitwise logical operators are the same
    • Parentheses are used to affect the order of evaluation
    • Logical constants (0 for false, 1 for true) are the same

Expressions

  • CSH adds file queries to test the status of a file
    • -r file (Is readable by the user)
    • -x file (Is executable by the user)
    • -o file (Is owned by the user)
    • -f file (Is an ordinary file)
    • -w file (Is writable by the user)
    • -e file (Exists)
    • -z file (Is of zero size)
    • -d file (Is a directory)
  • We can test whether a command has succeeded in a

logical expression by enclosing the command in braces ({ and }): if ({ command1 } && { command2 } || { command3 } then