Basic Programming in Bash PDF
Basic Programming in Bash PDF
Basic Programming in Bash PDF
Linux environment
• UNIX is an operating system originally developed in AT&T’s Bell labs
in the 1970s
• AT&T had to provide the source code to anyone who asked
• GNU is a UNIX-based open source project started in the 1980s
• Linux was first released in 1991 and is considered (by some) to be a
part of the GNU project
Bash
• Bash is a command language interpreter
• It is a Shell, a user interface (command-line interface)
• Sophisticated execution of commands is possible through Bash scripts
• In bash, everything is a file
• It can have Read (r), Write (w) and/or Execute (x) permissions
Simple Bash commands
• cd - change directory • less - show file content
• ls - list directory • pwd - show current directory
• cat - concatenate and print file
• head - print beginning of the file
• tail - print end of the file
• wc - word count
• rm - remove
• mkdir - make directory
• man - show manual of a command (quit by pressing 'q')
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:
#!/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 <
#!/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 are 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
Flags:
• -f: indicates columns to print (e.g.: 1,4-9,12-)
• -d: specifies column separator character (e.g.: ",")
comma separated
Redirect operator
> and >> redirects the Standard Output (stdout) to a file or elsewhere
• '>' creates and/or overwrites the file
• '>>' appends to the end of the file
Use the man command to find out what those flags mean
man sort
man uniq
man wc
grep
grep <pattern> <file> - extracts and prints all the lines that
match a specific pattern or string in the files
-c: counts occurrences of the pattern
-v: print only the lines that DO NOT contain the pattern
-i: case insensitive flag
Exercises:
1. Count number of students from 'India'
2. Count number of students that are not from 'Germany'
3. How many people contain the the word 'an' in their names?
grep
-E: let's you use 'regular expressions'