Shell Scripts¶
What is a shell script?¶
A shell script is a text file that contains a series of commands that can be executed by the shell. The shell is a program that interprets and executes commands. The most common shell on Linux is bash.
How to write a shell script¶
To write a shell script, you can use any text editor. The following is an example of a simple shell script:
This script will print the message "Hello, world!" to the console.
How to run a shell script¶
To run a shell script, you can use the following command:
For example, to run the script above, you would use the following command:
Variables¶
Variables are used to store data in shell scripts. To declare a variable, you use the following syntax:
For example, the following code declares a variable named name and assigns it the value "John Doe":
You can then access the value of the variable by using its name. For example, the following code will print the value of the name
variable:
Conditional statements¶
Conditional statements are used to control the flow of execution in shell scripts. The most common conditional statements are if
, else
, and elif
:
- The
if
statement is used to execute a block of code if a condition is met. - The
else
statement is used to execute a block of code if the condition in theif
statement is not met. - The
elif
statement is used to execute a block of code if a condition is met, but the condition in theif
statement is not met.
For example, the following code will print the message "Hello, world!" if the variable name
is not empty:
Loops¶
Loops are used to execute a block of code repeatedly. The most common loops are for
and while
:
- The
for
loop is used to iterate over a list of items. - The
while
loop is used to execute a block of code as long as a condition is met.
For example, the following code will print the names of all the files in the current directory:
Functions¶
Functions are used to group together a set of commands that can be executed repeatedly. To define a function, you use the following syntax:
For example, the following code defines a function named greet
that prints the message "Hello, world!" to the console:
You can then call the function by using its name. For example, the following code will call the greet
function:
Output redirection¶
Output redirection is used to redirect the output of a command to a file. To redirect output to a file, you use the following syntax:
For example, the following code will redirect the output of the ls
command to the file list.txt
:
Input redirection¶
Input redirection is used to redirect input to a command from a file. To redirect input from a file, you use the following syntax:
For example, the following code will read the contents of the file list.txt
and print them to the console:
Error handling¶
Error handling is used to deal with errors that occur in shell scripts. The most common way to handle errors is to use the try
and catch
statements:
- The
try
statement is used to execute a block of code. - The
catch
statement is used to handle errors that occur in the try block.
For example, the following code will try to create a directory named new_dir
. If the directory already exists.
Examples of Bash in Bioinformatics¶
- Introduction To Bash: Using the Terminal For Bioinformatics. University of Adelaide.
- Process fasta script. Pavlo Hrab.
- Samtools: faidx
References¶
Created: 05/16/2023: Updated: 05/17/2023
Carlos Lizárraga