You are on page 1of 9

Aliases, functions, wrappers, pipelines

Virginie Orgogozo
April 6 2012

Alias
shortcut for a commonly used shell command
~/.bash_profile or ~/.bashrc file:
()

In the shell:

alias cdp='cd ~/pcfb/sandbox'

$cdp

alias cx='chmod u+x'


alias ag='agrep -B -y -d > '

$cxscript2.py
$agCYGexamples/FPexcerpt.fta
$myjobs

alias myjobs=ps -ax | grep orgogozo

$hgpcfb

alias hg='history | grep'

$alias

(list your defined aliases)

For the changes in your .bashrc file to be used in the shell:


- open a new shell window
or: - $source.bashrc

Functions
For a more complex set of shell commands
In the shell:
$pdftkGenomeBiolPaper.pdfcat2endoutputDoe2012.pdf
$firstpageGenomeBiolPaper.pdfDoe2012.pdf
~/.bash_profile or ~/.bashrc file:
()
function firstpage() { pdftk $1 cat 2-end output $2 ;}
$listall

listall() {
ls -la
echo Above are all the directories of the following
folder:
pwd
date
}

You can also write a function in the shell only


virginie@Darwin:~$repeater(){
>echo"$1iswhatyousaidfirst"
>echo"$@iseverythingyousaid"
>}

Defines
the
function

virginie@Darwin:~$repeaterhelloeverybody!Howare
you?
helloiswhatyousaidfirst
helloeverybody!Howareyou?iseverythingyousaid
virginie@Darwin:$

Usage of
the
function

Use a function when you want to use the same command


multiple times with different filenames in the middle
virginie@Darwin:~$phymlsequencesA.fta1i1100WAG
08eBIONJyy
virginie@Darwin:~$phymlsequencesB.fta1i1100WAG
08eBIONJyy
virginie@Darwin:~$phymlCG5679.fta1i1100WAG08
eBIONJyy
virginie@Darwin:~$phymlASCRE.fta1i1100WAG08
eBIONJyy
(...)
virginie@Darwin:~$myphyml(){
>phyml$11i1100WAG08eBIONJyy
>}
virginie@Darwin:~$myphymlsequencesA.fta
virginie@Darwin:~$myphymlsequencesB.fta&&myphyml
CG5679.fta&&myphymlASCRE.fta

Defines
the
function
Usage of
the
function

Use a function to rename multiple files


file1.txt
file2.txt
file3.txt
...

u_file1.dat
u_file2.dat
u_file3.dat
...

renamer(){
EXT="dat"
PRE="u_"
(if the number of arguments is ..., then...)
if[$#lt1]
then
echo"Renameafile.txtlistas$PREfile.
$EXT"
else
forFILENAMEin"$@"
do
ROOTNAME="${FILENAME%.*}"

cp"$FILENAME""$PRE$ROOTNAME.$EXT"
echo"Copying$FILENAMEto
$PRE$ROOTNAME.$EXT"
done
fi
}
Copy and paste this text from pcfb/scripts/shellfunctions.sh in your shell

virginie@Darwin:~$cd~/pcfb/examplesspectra/
virginie@Darwin:~/pcfb/examples/spectra$ls
LEDBlue.txtLEDGreen.txtLEDRed.txtLEDYellow.txt
virginie@Darwin:~/pcfb/examples/spectra$renamer*.txt
CopyingLEDBlue.txttou_LEDBlue.dat
CopyingLEDGreen.txttou_LEDGreen.dat
CopyingLEDRed.txttou_LEDRed.dat
CopyingLEDYellow.txttou_LEDYellow.dat
virginie@Darwin:~/pcfb/examples/spectra$ls
LEDBlue.txtLEDRed.txtu_LEDBlue.datu_LEDRed.dat
LEDGreen.txtLEDYellow.txtu_LEDGreen.datu_LEDYellow.dat
virginie@Darwin:~/Documents/WWW/BioInfoCourses/pcfb/examples/spectr
a$

Use a loop to repeat operations


virginie@Darwin:~$forkin{1..10}
>do
>echo$k
>done
1
2
3
4
5
6
7
8
9
10
virginie@Darwin:~$
virginie@Darwin:~$foriin{A..Z};domkdir$i
authors;mv$i*.pdf$iauthors;done

Wrappers
Program that controls and expands the functionality of another program

Ex: python script that calls MATLAB or another program

Pipelines
Automated workflow
Very important for efficiency, consistency and record

You might also like