Skip to content

Commit c318364

Browse files
authored
Updated variables to {}
1 parent 56cdca1 commit c318364

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

lessons/06_loops_and_automation.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ The structure or the syntax of (*for*) loops in bash is as follows:
2323
```bash
2424
for (variable_name) in (list)
2525
do
26-
(command1 $variable_name)
27-
(command2 $variable_name)
26+
(command1 ${variable_name})
27+
(command2 ${variable_name})
2828
...
2929
....
3030
done
@@ -41,8 +41,8 @@ $ cd ~/unix_lesson/raw_fastq/
4141

4242
$ for x in Mov10_oe_1.subset.fq Mov10_oe_2.subset.fq Mov10_oe_3.subset.fq
4343
do
44-
echo $x
45-
wc -l $x
44+
echo ${x}
45+
wc -l ${x}
4646
done
4747
```
4848

@@ -63,10 +63,10 @@ $ for x in Mov10_oe_1.subset.fq Mov10_oe_2.subset.fq Mov10_oe_3.subset.fq
6363

6464
> **We don't explicitly see this, but the variable has been defined as `x=Mov10_oe_1.subset.fq`.**
6565

66-
2. Next, all of the commands in the body of the loop (between the `do` and `done`) are executed. Usually, the commands placed here will be using the temporary variable as input. **Remember, if you are using the value stored in the variable you need to use $ to reference it!** In the example, we are running two commands:
66+
2. Next, all of the commands in the body of the loop (between the `do` and `done`) are executed. Usually, the commands placed here will be using the temporary variable as input. **Remember, if you are using the value stored in the variable you need to use ${} to reference it!** In the example, we are running two commands:
6767

68-
* `echo $x`: print out the value stored in `x`
69-
* `wc -l $x`: count/report the number of lines in `x`
68+
* `echo ${x}`: print out the value stored in `x`
69+
* `wc -l ${x}`: count/report the number of lines in `x`
7070

7171
3. Once those two commands are complete, the temporary variable is assigned a new value. It now takes the value of the second item in the list.
7272

@@ -102,8 +102,8 @@ Let's rewrite the for loop above using a more meaningful variable name and using
102102
```bash
103103
$ for file in Mov10*.fq
104104
do
105-
echo $file
106-
wc -l $file
105+
echo ${file}
106+
wc -l ${file}
107107
done
108108
```
109109

@@ -178,28 +178,27 @@ For each file that we process we can use `basename` to create a prefix from the
178178
```bash
179179
do
180180
# create a prefix for all output files
181-
samplename=`basename $filename .subset.fq`
181+
samplename=`basename ${filename} .subset.fq`
182182
```
183183
184184
Now we execute the command required to dump the bad reads to file, but first start with an `echo` statement to keep the user informed. We will use `grep` to find all the bad reads (in our case, bad reads are defined as those with 10 consecutive N's), and then extract the four lines associated with each sequence read and write them to a file. Our output file is named using the `samplename` variable we created earlier in the loop. We will also add a path to redirect output to the `badreads` directory.
185185
186186
```bash
187187
# tell us what file we're working on
188-
echo $filename
188+
echo ${filename}
189189
190190
# grab all the bad read records into new file
191-
grep -B1 -A2 NNNNNNNNNN --no-group-separator $filename > ~/unix_lesson/badreads/${samplename}_badreads.fq
191+
grep -B1 -A2 NNNNNNNNNN --no-group-separator ${filename} > ~/unix_lesson/badreads/${samplename}_badreads.fq
192192
```
193193
194-
> #### Why are we using curly brackets with the variable name?
195-
> When we append a variable to some other free text, we need shell to know where our variable name ends. By encapsulating the variable name in curly brackets we are letting shell know that everything inside it is the variable name. This way when we reference it, shell knows to print the variable `$samplename` and not to look for a variable called `$samplename_badreads.fq`.
194+
> Reminder: When we append a variable to some other free text, we need shell to know where our variable name ends. By encapsulating the variable name in curly brackets we are letting shell know that everything inside it is the variable name. This way when we reference it, shell knows to print the variable `samplename` and not to look for a variable called `samplename_badreads.fq`.
196195
197196
198197
We'll also count the number of identified bad reads using the count flag of `grep`, `-c`, which will return the number of matches rather than the actual matching lines. Here, we also use a new `grep` flag `-H`; this will report the filename along with the count value. This is useful because we are writing this information to a running log summary file, so rather than just reporting a count value we also know which file it is associated with.
199198
200199
```bash
201200
# grab the number of bad reads and write it to a summary file
202-
grep -cH NNNNNNNNNN $filename >> ~/unix_lesson/badreads/badreads.count.summary
201+
grep -cH NNNNNNNNNN ${filename} >> ~/unix_lesson/badreads/badreads.count.summary
203202
done
204203
```
205204
@@ -216,16 +215,16 @@ for filename in *.fq
216215
do
217216
218217
# create a prefix for all output files
219-
samplename=`basename $filename .subset.fq`
218+
samplename=`basename ${filename} .subset.fq`
220219
221220
# tell us what file we're working on
222-
echo $filename
221+
echo ${filename}
223222
224223
# grab all the bad read records
225-
grep -B1 -A2 --no-group-separator NNNNNNNNNN $filename > ~/unix_lesson/badreads/${samplename}_badreads.fq
224+
grep -B1 -A2 --no-group-separator NNNNNNNNNN ${filename} > ~/unix_lesson/badreads/${samplename}_badreads.fq
226225
227226
# grab the number of bad reads and write it to a summary file
228-
grep -cH NNNNNNNNNN $filename >> ~/unix_lesson/badreads/badreads.count.summary
227+
grep -cH NNNNNNNNNN ${filename} >> ~/unix_lesson/badreads/badreads.count.summary
229228
done
230229
231230
```

0 commit comments

Comments
 (0)