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) Docsity.com 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 Docsity.com 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. Docsity.com Executable Text Files • When the shell executes a nonbuilt-in command (i.e. a file) it first determines what type of file the command is. – Executable binary files have a “magic number” (a few bytes of code) at the beginning. – If this magic number is not found, the file is an executable text file. • The first few characters of the file tell which shell should process the file - #/bin/csh or #/bin/sh Docsity.com 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) Docsity.com 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 Docsity.com The foreach Command #!/bin/csh foreach x ( * ) echo Changing mode for $x chmod +rx $x end • Consider changing mode only for .exe files. Docsity.com The if Command • Logical branching is provided by the if command: if (expr ) simple-command if ( expr ) then commandlist1 [else commandlist2] endif Docsity.com The switch Command #!/bin/csh ## append $1 to $2 or standard input to $1 switch ( $#argv ) case 1: cat >> $argv[1] breaksw case 2: cat >> $argv[2] < $argv[1] breaksw default: echo 'usage: append [ from ] to' endsw Docsity.com 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 Docsity.com 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/2 @ argv[$j] = $j + 4 @ x += 3 @ i++ Docsity.com 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 Docsity.com Variables – There are four kinds of variables: 1. Positional parameters (e.g. $1 and $argv[2]) 2. Special variables such as noglob and nonomatch 3. Environment variables such as DISPLAY and TERM 4. User-defined variables – Variables are set using the set or @ command: set dir = $cwd set list = (a b c d) @ k = $j + 1 set y = “$list” A multivalued variable Value of k is a string Use doublequotes for a multivalued variable Docsity.com Variables • To select a portion of a multivalued variable, use $var[ selector ]where selector is an integer index, a range (e.g. 2-4) or all of the values (*). • $#var or ${#var} gives the number of strings in a multivalued variable. Docsity.com Special Substitutions • To examine whether a variable is set or not use $?var or ${?var}. The string 1 is substituted is var is set, 0 if it is not. if ( ! $?term ) then set term = „tset - -m dialup:vt100‟ endif • The special variable $$ substitutes the process number of the shell executing the script. Docsity.com Special Substitutions #!/bin/csh ## Reminder service using calendar and mail set tfile = /tmp/remind_$$ ## temporary file calendar > $tfile ## consult calendar file if ( ! -z $tfile ) then ## send msg if necessary cat $tfile | /usr/ucb/mail -s \ "Reminder-calendar" $USER endif rm -f $tfile Docsity.com Input and Output • The command echo words is used to display zero or more words to the standard output. – To prevent a newline after the last word, use echo -n words. • To read user input, the metavariable $< is used. A line from the standard input is read and returned as the value of the metavariable $< without variable- or file-name substitution. Docsity.com The findcmd Script #!/bin/csh ## this procedure finds where given command is on search path ## the pathname for the command ## is displayed as soon as it is found ## otherwise a message to the contrary is displayed ## This script simulates the UNIX command "which" set cmd = $1 foreach dir ( $path ) if ( -e $dir/$cmd ) then echo FOUND: $dir/$cmd exit(0) endif end echo $cmd not on $path Docsity.com The append Script #!/bin/csh ## append $1 to $2 or standard input to $1 switch ( $#argv ) case 1: cat >> $argv[1] breaksw case 2: cat >> $argv[2] < $argv[1] breaksw default: echo 'usage: append [ from ] to' endsw Docsity.com The clean Script ## csh script: clean ## helps to rm unwanted files from a directory if ($#argv != 1) then echo usage: $0 directory; exit(1) endif set dir = $1 if (! -d $dir || ! -w $dir ) then echo $dir not a writable directory echo usage: $0 directory; exit(1) endif chdir $dir set files = * Docsity.com The clean Script eval $< ## in this command the var $file can be used continue case q: ## quit from clean exit(0) default: ## help for user echo "clean commands: followed by RETURN\ y yes delete file\ n no, don't delete file, skip to next file\ m display file with more first\ t display tail of file first\ \! shell escape\ q quit, exit from clean" endsw end ## of while end ## of foreach Docsity.com The ccp Script #!/bin/csh ## csh script : ccp --- conditional copy ## usage: ccp from to [ file ... ] ## where: `from' the source directory ## `to' the destination directory ## [file ... ] an optional list of files to be copied, ## otherwise, all files in `from' will be processed if ($#argv < 2) then echo usage: ccp from to "[ file ... ]"; exit(1) else if (! -d $1 || ! -d $2) then echo usage: ccp from to "[ file ... ]"; exit(1) endif Docsity.com The ccp Script set dir = `pwd`; chdir $2; set to = `pwd` chdir $dir; chdir $1; ## now in from-dir if ($#argv == 2) then set files = * else set files = ( $argv[3-] ) endif foreach file ($files) if ( -d $file ) continue ## skip directories if (! -e $to/$file) then echo $to/$file is a new file cp $file $to; continue endif # if file in $from is more recent then cp find $file -newer $to/$file -exec cp $file $to \; end Docsity.com