Scripting

make shortcut command

exercise:

make modification permanent

Each time we modify the environment this modification
is available only for the current terminal.
To turn in permanent just put it in your .bashrc

.bashrc is a file which is executed each time a terminal is created.

exercise

  1. use vim to edit the ~/.profile to add the following lines. this way when you log again, your ~/.bashrc will be taken in account

syntax

# use the bashrc
source ${HOOME}/.bashrc
  1. make a copy of the .bashrc file
  2. use vim to edit the .bashrc file and add the alias.

in order to test your modifications, do the following command source ~/.bashrc

repeat an action

Some times we need to repeat the same action n times with just a different details.
for instance rename all files in a directory.
for this we use a loop:

syntax:

for var in list items
do
   do something
done

to be usefull we need :

example:

remove '.fa' from all files terminating by 'fa.gde'

for f in `ls il2*.fa.gde`
do
   nam=`basename $f .fa.gde`
   mv $f $nam.gde
done

exercise:

  1. copy the Proteique directory from the unix_training projets in central-bio in your local home
  2. reformat all fasta file (.fa file) using squizz in gde format.

scripts

scripts arguments

tests

you will find 2 syntax for the test

expression evaluate in true (0) or false (1)

syntax:

var="hello"
[ $var = "hello" ]
echo $?

==> 0 means true

syntax:

var=3
[ $var -eq 2 ]
echo $?

==> 1 means false

put test at work

syntax:

#! /bin/sh

# check if we got arguments
if [ $# -eq  0 ]
  then
    echo "no argument provided: exit"
    exit 1
fi

# check if file exists
if [ -f $1 ]
  then
    echo "file $1 exists"
    ret=0
  else
    echo "file $1 does not exists"
    ret=1
fi

# exit with significative return value
exit $ret