... | @@ -69,8 +69,10 @@ The previous script example is useful to execute multiple programs with a single |
... | @@ -69,8 +69,10 @@ The previous script example is useful to execute multiple programs with a single |
|
|
|
|
|
```bash
|
|
```bash
|
|
#!/bin/bash
|
|
#!/bin/bash
|
|
# This script runs a partition function, followed by the prediction of
|
|
# This script runs a partition function, followed by the prediction of a
|
|
# a maximum expected accuracy secondary structure
|
|
# maximum expected accuracy secondary structure. In this script, the
|
|
|
|
# input and output files are given as arguments instead of being defined
|
|
|
|
# in the script itself.
|
|
|
|
|
|
# Define a path to a sequence file for the input
|
|
# Define a path to a sequence file for the input
|
|
seq_file=$1
|
|
seq_file=$1
|
... | @@ -89,5 +91,132 @@ rm foo.pfs |
... | @@ -89,5 +91,132 @@ rm foo.pfs |
|
```
|
|
```
|
|
Now, the input sequence file and output structure file can be specified when you execute the script.
|
|
Now, the input sequence file and output structure file can be specified when you execute the script.
|
|
|
|
|
|
|
|
## String Manipulations
|
|
|
|
One common operation is to manipulate strings. Strings can be combined into a new string and substrings can extracted from a larger string. One common context for these operations are manipulating file paths. Two useful commands when dealing with paths is `dirname` and `basename`. The command `basename` returns the filename from a path by stripping way the directory names. For example:
|
|
|
|
|
|
|
|
```
|
|
|
|
[user@host ~]$ basename /home/user/foo.txt
|
|
|
|
foo.txt
|
|
|
|
```
|
|
|
|
|
|
|
|
Similarly, `dirname` returns the directory name information from a given path. For example:
|
|
|
|
```
|
|
|
|
[user@host ~]$ dirname /home/user/foo.txt
|
|
|
|
/home/user
|
|
|
|
```
|
|
|
|
Substrings can be extracted using `${string_var:start:length}` construct. For example:
|
|
|
|
```
|
|
|
|
[user@host ~]$ foo="HELLO WORLD"
|
|
|
|
[user@host ~]$ echo ${foo:0:5}
|
|
|
|
HELLO
|
|
|
|
```
|
|
|
|
Note that the start index begins with 0. This operation is useful for removing a file extension from a filename to extract a sequence name. For example:
|
|
|
|
|
|
|
|
```
|
|
|
|
[user@host ~]$ filename=/home/user/sequence.seq
|
|
|
|
[user@host ~]$ dir_name=`dirname $filename`
|
|
|
|
[user@host ~]$ echo ${filename:${#dir_name}+1:${#filename}-${#dir_name}-5}
|
|
|
|
sequence
|
|
|
|
```
|
|
|
|
|
|
|
|
A more concise way of performing the same string manipulations would be to use pattern matching using the `%` and `#` operators. Using the construct `${string_var%pattern}` trims the shortest pattern match from the end of the string. For example:
|
|
|
|
```
|
|
|
|
[user@host ~]$ filename=home/user/sequence.seq
|
|
|
|
[user@host ~]$ echo ${filename%/*}
|
|
|
|
home/user
|
|
|
|
```
|
|
|
|
|
|
|
|
The construct `${string_var%%pattern}` trims the longest pattern match from the end of the string. For example:
|
|
|
|
```
|
|
|
|
[user@host ~]$ filename=home/user/sequence.seq
|
|
|
|
[user@host ~]$ echo ${filename%&/*}
|
|
|
|
home
|
|
|
|
```
|
|
|
|
|
|
|
|
The `#` and `##` perform similar functions, except they trim from the front of the string. For example:
|
|
|
|
```
|
|
|
|
[user@host ~]$ filename=home/user/sequence.tar.gz
|
|
|
|
[user@host ~]$ echo ${filename#*.}
|
|
|
|
tar.gz
|
|
|
|
[user@host ~]$ echo ${filename##*.}
|
|
|
|
gz
|
|
|
|
[user@host ~]$ base_name=${filename##*/}
|
|
|
|
[user@host ~]$ echo ${base_name%.*}
|
|
|
|
sequence.tar
|
|
|
|
[user@host ~]$ echo ${base_name%%.*}
|
|
|
|
sequence
|
|
|
|
```
|
|
|
|
|
|
## Program Flow
|
|
## Program Flow
|
|
|
|
|
|
|
|
Often, it is useful to be able to alter execution of a script depending on variable values.
|
|
|
|
|
|
|
|
### if...else
|
|
|
|
|
|
|
|
One of the most basic methods to control the flow a script (or any other program) is by using conditional statements.
|
|
|
|
```bash
|
|
|
|
if [ condition ]; then
|
|
|
|
commands when condition is true
|
|
|
|
fi
|
|
|
|
```
|
|
|
|
|
|
|
|
```bash
|
|
|
|
if [ condition ]; then
|
|
|
|
commands when condition is true
|
|
|
|
else
|
|
|
|
commands when condition is false
|
|
|
|
fi
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Comparisons
|
|
|
|
Bash Comparison | Math Equivalent
|
|
|
|
:----:|:----:
|
|
|
|
-lt| <
|
|
|
|
-gt| >
|
|
|
|
-le| <=
|
|
|
|
-ge| >=
|
|
|
|
-eq| ==
|
|
|
|
-ne| !=
|
|
|
|
|
|
|
|
## Loops
|
|
|
|
### For loops
|
|
|
|
For loops will execute a section of the script a set number of multiple times.
|
|
|
|
|
|
|
|
The general syntax for a For loop in bash is:
|
|
|
|
```bash
|
|
|
|
#!/bin/bash
|
|
|
|
for i in 1 2 3 4 5
|
|
|
|
do
|
|
|
|
echo "Hello $i"
|
|
|
|
done
|
|
|
|
```
|
|
|
|
|
|
|
|
The output of this is:
|
|
|
|
```
|
|
|
|
Hello 1
|
|
|
|
Hello 2
|
|
|
|
Hello 3
|
|
|
|
Hello 4
|
|
|
|
Hello 5
|
|
|
|
```
|
|
|
|
|
|
|
|
An equivalent loop is:
|
|
|
|
```bash
|
|
|
|
#!/bin/bash
|
|
|
|
for i in {1..5}
|
|
|
|
do
|
|
|
|
echo "Hello $i"
|
|
|
|
done
|
|
|
|
```
|
|
|
|
|
|
|
|
You can also use a for loop to run a set of commands for all files in a folder
|
|
|
|
```bash
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
input_dir=/home/user/Input_Files
|
|
|
|
|
|
|
|
for filename in $input_dir/*; do
|
|
|
|
echo $filename
|
|
|
|
done
|
|
|
|
```
|
|
|
|
Here, the `*` character represents a wildcard. If the for statement was instead replaced with `for filename in $input_dir/*.txt; do`, the loop would print the filename of every file whose path began with `/home/user/Input_Files` and ended with `.txt`. |
|
|
|
\ No newline at end of file |