Basic Programming in Bash
Basic Programming in Bash
Basic Programming in Bash
Bash programming
• In the previous tutorial you got to know basic Bash commands
• Bash is also a programming (scripting) language
• More sophisticated execution of commands (upon a condition,
several times in a row, etc.) is possible through Bash scripts
Motivation
• Basic programming is useful as it allows you to automate tasks
• MMseqs2 software suite allows creating tailored computational tools
by combining its modules and workflows in Bash scripts
createdb createdb
taxonomy search
filterdb filterdb
search
The script file
• The first line of a Bash script is usually:
#!/bin/bash
• This indicates this file is a Bash script
• Lines that start with ‘#’ are comments
• To print something we use ‘echo’
• A script is just a text file.
• Under your home directory, create a directory called “Bash_scripts”
• We will create Bash scripts there
Creating the Hello_Bash.sh script file
Running a Bash script
• You need to give your script execution permission:
chmod +x ~/Bash_scripts/Hello_Bash.sh
• Then you can run it from the terminal:
Hello_Bash.sh
Create a Hello_Bash.sh script and run it
Bash variables
• A variable stores a value
• There are no variable types in Bash
• Assignment of a value is done with “=“:
#!/bin/bash
NAME="Eli"
NUMBER_OF_EYES=3
echo "Hello $NAME, you have $NUMBER_OF_EYES eyes"
CORRECT_NUMBER_OF_EYES=$((NUMBER_OF_EYES – 1))
echo "Humans usually don't have more than
$CORRECT_NUMBER_OF_EYES eyes"
• Create a Bash script with a variable AGE and assign it your age. Print
the age you will be in one year
Conditionals
• If/else structures allow us to execute commands only in certain cases
AGE=20
if [ "$AGE" -eq 20 ]; then
echo "Wow, you are exactly 20!"
fi Description Numeric String
less than -lt <
greater than -gt >
• Comparison operators:
equal -eq =
not equal -ne !=
less or equal -le
greater or equal -ge
Exercise
• This simple Bash script asks the user for their name and says hi:
#!/bin/bash
echo "Enter your name and press [ENTER]: "
read NAME
echo "Hi $NAME"
• Create a script that asks for the user’s age and serves beer only if
the user is at least 18
What does this code do?
echo "Enter a directory name and press [ENTER]: "
read DIR
if [ -d "$DIR" ]; then
ls "$DIR"
else
mkdir "$DIR"
fi
Repetitive execution of commands
• Often we would like to perform the same thing more than once:
• Say hello to all students in the class (there 22 of you!)
• Make a copy of each file in a directory
• Refine an MMseqs2 clustering…
2. Sum the numbers the user provides you until they provide a
negative number
if [ ! -e "${TMP_PATH}/first" ]; then
"$MMSEQS" search "${INPUT}" "${TARGET}" "${TMP_PATH}/first"
"${TMP_PATH}/tmp_hsp1" ${SEARCH1_PAR} \
|| fail "First search died"
fi
if [ ! -e "${TMP_PATH}/top1" ]; then
"$MMSEQS" filterdb "${TMP_PATH}/first" "${TMP_PATH}/top1" --extract-lines 1 \
|| fail "Filterdb died"
fi
Taxonomy Bash workflow
if [ ! -e "${TMP_PATH}/aligned" ]; then
"$MMSEQS" extractalignedregion "${INPUT}" "${TARGET}" "${TMP_PATH}/top1"
"${TMP_PATH}/aligned" --extract-mode 2 \
|| fail "Extractalignedregion failed"
fi
if [ ! -e "${TMP_PATH}/round2" ]; then
"$MMSEQS" search "${TMP_PATH}/aligned" "${TARGET}" "${TMP_PATH}/round2"
"${TMP_PATH}/tmp_hsp2" ${SEARCH2_PAR} \
|| fail "Second search died"
fi
Taxonomy Bash workflow
# Concat top hit from 1st search with all results from 2nd search
if [ ! -e "${TMP_PATH}/merged" ]; then
"$MMSEQS" mergedbs "${TMP_PATH}/top1" "${TMP_PATH}/merged" "${TMP_PATH}/top1"
"${TMP_PATH}/round2" \
|| fail "Mergedbs died"
fi
# Filter out 2nd search entries that do not reach the evalue of the top 1 hit
if [ ! -e "${TMP_PATH}/2b_ali" ]; then
"$MMSEQS" filterdb "${TMP_PATH}/merged" "${TMP_PATH}/2b_ali" --beats-first --
filter-column 4 --comparison-operator le \
|| fail "First filterdb died"
fi