Loops
-Last updated on 2023-08-09 | +
Last updated on 2023-11-09 | Edit this page
@@ -398,21 +398,15 @@Follow the Promptecho to print the value that the variable
$filename
currently holds. This is not necessary for the
result, but beneficial for us here to have an easier time to follow
-along. Next, we will assign a file name to the variable
-filename
and run the head
command. The first
-time through the loop, $filename
is
-basilisk.dat
. The interpreter runs the command
-head
on basilisk.dat
and pipes the first two
-lines to the tail
command, which then prints the second
-line of basilisk.dat
. For the second iteration,
-$filename
becomes minotaur.dat
. This time, the
-shell runs head
on minotaur.dat
and pipes the
-first two lines to the tail
command, which then prints the
-second line of minotaur.dat
. For the third iteration,
-$filename
becomes unicorn.dat
, so the shell
-runs the head
command on that file, and tail
-on the output of that. Since the list was only three items, the shell
-exits the for
loop.
+along. Next, we will run the head
command on the file
+currently referred to by \(filename. The first
+time through the loop,
+`\)filenameis
basilisk.dat. The interpreter runs the command
headon
basilisk.datand pipes the first two lines to the
tailcommand, which then prints the second line of
basilisk.dat. For the second iteration,
\(filename` becomes `minotaur.dat`. This time, the
+shell runs `head` on `minotaur.dat` and pipes the first two lines to the
+`tail` command, which then prints the second line of `minotaur.dat`. For
+the third iteration,
+`\)filenamebecomes
unicorn.dat, so the shell runs the
headcommand on that file, and
tailon the output of that. Since the list was only three items, the shell exits the
for`
+loop.
@@ -489,7 +483,7 @@
Show me the solution
-
+
BASH
@@ -562,7 +556,7 @@
Show me the solution
-
+
The first code block gives the same output on each iteration through
the loop. Bash expands the wildcard *.pdb
within the loop
@@ -637,7 +631,7 @@
Show me the solution
-
+
4 is the correct answer. *
matches zero or more
characters, so any file name starting with the letter c, followed by
@@ -680,7 +674,7 @@
Show me the solution
-
+
4 is the correct answer. *
matches zero or more
characters, so a file name with zero or more characters before a letter
@@ -731,7 +725,7 @@
Show me the solution
-
+
- The text from each file in turn gets written to the
alkanes.pdb
file. However, the file gets overwritten on
@@ -785,7 +779,7 @@
Show me the solution
-
+
3 is the correct answer. >>
appends to a file,
rather than overwriting it with the redirected output from a command.
@@ -1194,7 +1188,7 @@
Show me the solution
-
+
The second version is the one we want to run. This prints to screen
everything enclosed in the quote marks, expanding the loop variable name
@@ -1245,7 +1239,7 @@
Show me the solution
-
+
We have a nested loop, i.e. contained within another loop, so for
each species in the outer loop, the inner loop (the nested loop)
@@ -1343,7 +1337,7 @@
Keypoints
Show me the solution
-
+
The -l
option makes ls
use a
long listing format, showing not only the
@@ -944,7 +944,7 @@
Show me the solution
-
+
The most recently changed file is listed last when using
-rt
. This can be very useful for finding your most recent
@@ -1297,7 +1297,7 @@
Show me the solution
-
+
- No:
.
stands for the current directory.
@@ -1345,7 +1345,7 @@
Show me the solution
-
+
- No: there is a directory
backup
in
@@ -1393,7 +1393,7 @@
Show me the solution
-
+
- No:
pwd
is not the name of a directory.
@@ -1914,7 +1914,7 @@
Show me the solution
-
+
The touch
command generates a new file called
@@ -2125,7 +2125,7 @@
Show me the solution
-
+
BASH
@@ -2221,7 +2221,7 @@
Show me the solution
-
+
- No. While this would create a file with the correct name, the
@@ -2291,7 +2291,7 @@
Show me the solution
-
+
We start in the /Users/jamie/data
directory, and create
a new folder called recombined
. The second line moves
@@ -2382,7 +2382,7 @@
Show me the solution
-
+
OUTPUT
@@ -2478,7 +2478,7 @@
Show me the solution
-
+
If given more than one file name followed by a directory name
(i.e. the destination directory must be the last argument),
@@ -2563,7 +2563,7 @@
Show me the solution
-
+
The solution is 3.
1.
shows all files whose names contain zero or more
@@ -2687,7 +2687,7 @@
Show me the solution
-
+
BASH
@@ -2754,7 +2754,7 @@
Show me the solution
-
+
BASH
@@ -2845,7 +2845,7 @@
Show me the solution
-
+
The first two sets of commands achieve this objective. The first set
uses relative paths to create the top-level directory before the
@@ -3167,7 +3167,7 @@
Show me the solution
-
+
The -n
option specifies a numerical rather than an
alphanumerical sort.
@@ -3285,7 +3285,7 @@
Show me the solution
-
+
In the first example with >
, the string ‘hello’ is
written to testfile01.txt
, but the file gets overwritten
@@ -3340,7 +3340,7 @@
Show me the solution
-
+
Option 3 is correct. For option 1 to be correct we would only run the
head
command. For option 2 to be correct we would only run
@@ -3448,7 +3448,7 @@
Show me the solution
-
+
Option 4 is the solution. The pipe character |
is used
to connect the output from one command to the input of another.
@@ -3516,7 +3516,7 @@
Show me the solution
-
+
The head
command extracts the first 5 lines from
animals.csv
. Then, the last 3 lines are extracted from the
@@ -3582,7 +3582,7 @@
Show me the solution
-
+
BASH
@@ -3635,7 +3635,7 @@
Show me the solution
-
+
Option 4. is the correct answer. If you have difficulty understanding
why, try running the commands, or sub-sections of the pipelines (make
@@ -3755,7 +3755,7 @@
Show me the solution
-
+
- This would remove
.txt
files with one-character
@@ -3810,7 +3810,7 @@ KeypointsContent from Loops
- Last updated on 2023-08-09 |
+
Last updated on 2023-11-09 |
Edit this page
Show me the solution
-BASH
@@ -562,7 +556,7 @@
Show me the solution
-
+
The first code block gives the same output on each iteration through
the loop. Bash expands the wildcard *.pdb
within the loop
@@ -637,7 +631,7 @@
Show me the solution
-
+
4 is the correct answer. *
matches zero or more
characters, so any file name starting with the letter c, followed by
@@ -680,7 +674,7 @@
Show me the solution
-
+
4 is the correct answer. *
matches zero or more
characters, so a file name with zero or more characters before a letter
@@ -731,7 +725,7 @@
Show me the solution
-
+
- The text from each file in turn gets written to the
alkanes.pdb
file. However, the file gets overwritten on
@@ -785,7 +779,7 @@
Show me the solution
-
+
3 is the correct answer. >>
appends to a file,
rather than overwriting it with the redirected output from a command.
@@ -1194,7 +1188,7 @@
Show me the solution
-
+
The second version is the one we want to run. This prints to screen
everything enclosed in the quote marks, expanding the loop variable name
@@ -1245,7 +1239,7 @@
Show me the solution
-
+
We have a nested loop, i.e. contained within another loop, so for
each species in the outer loop, the inner loop (the nested loop)
@@ -1343,7 +1337,7 @@
Keypoints
Show me the solution
-
+
The -l
option makes ls
use a
long listing format, showing not only the
@@ -944,7 +944,7 @@
Show me the solution
-
+
The most recently changed file is listed last when using
-rt
. This can be very useful for finding your most recent
@@ -1297,7 +1297,7 @@
Show me the solution
-
+
- No:
.
stands for the current directory.
@@ -1345,7 +1345,7 @@
Show me the solution
-
+
- No: there is a directory
backup
in
@@ -1393,7 +1393,7 @@
Show me the solution
-
+
- No:
pwd
is not the name of a directory.
@@ -1914,7 +1914,7 @@
Show me the solution
-
+
The touch
command generates a new file called
@@ -2125,7 +2125,7 @@
Show me the solution
-
+
BASH
@@ -2221,7 +2221,7 @@
Show me the solution
-
+
- No. While this would create a file with the correct name, the
@@ -2291,7 +2291,7 @@
Show me the solution
-
+
We start in the /Users/jamie/data
directory, and create
a new folder called recombined
. The second line moves
@@ -2382,7 +2382,7 @@
Show me the solution
-
+
OUTPUT
@@ -2478,7 +2478,7 @@
Show me the solution
-
+
If given more than one file name followed by a directory name
(i.e. the destination directory must be the last argument),
@@ -2563,7 +2563,7 @@
Show me the solution
-
+
The solution is 3.
1.
shows all files whose names contain zero or more
@@ -2687,7 +2687,7 @@
Show me the solution
-
+
BASH
@@ -2754,7 +2754,7 @@
Show me the solution
-
+
BASH
@@ -2845,7 +2845,7 @@
Show me the solution
-
+
The first two sets of commands achieve this objective. The first set
uses relative paths to create the top-level directory before the
@@ -3167,7 +3167,7 @@
Show me the solution
-
+
The -n
option specifies a numerical rather than an
alphanumerical sort.
@@ -3285,7 +3285,7 @@
Show me the solution
-
+
In the first example with >
, the string ‘hello’ is
written to testfile01.txt
, but the file gets overwritten
@@ -3340,7 +3340,7 @@
Show me the solution
-
+
Option 3 is correct. For option 1 to be correct we would only run the
head
command. For option 2 to be correct we would only run
@@ -3448,7 +3448,7 @@
Show me the solution
-
+
Option 4 is the solution. The pipe character |
is used
to connect the output from one command to the input of another.
@@ -3516,7 +3516,7 @@
Show me the solution
-
+
The head
command extracts the first 5 lines from
animals.csv
. Then, the last 3 lines are extracted from the
@@ -3582,7 +3582,7 @@
Show me the solution
-
+
BASH
@@ -3635,7 +3635,7 @@
Show me the solution
-
+
Option 4. is the correct answer. If you have difficulty understanding
why, try running the commands, or sub-sections of the pipelines (make
@@ -3755,7 +3755,7 @@
Show me the solution
-
+
- This would remove
.txt
files with one-character
@@ -3810,7 +3810,7 @@ KeypointsContent from Loops
- Last updated on 2023-08-09 |
+
Last updated on 2023-11-09 |
Edit this page
The first code block gives the same output on each iteration through
the loop. Bash expands the wildcard *.pdb
within the loop
@@ -637,7 +631,7 @@
Show me the solution
-4 is the correct answer. *
matches zero or more
characters, so any file name starting with the letter c, followed by
@@ -680,7 +674,7 @@
Show me the solution
-4 is the correct answer. *
matches zero or more
characters, so a file name with zero or more characters before a letter
@@ -731,7 +725,7 @@
Show me the solution
-- The text from each file in turn gets written to the
alkanes.pdb
file. However, the file gets overwritten on @@ -785,7 +779,7 @@Show me the solution
-+3 is the correct answer.
>>
appends to a file, rather than overwriting it with the redirected output from a command. @@ -1194,7 +1188,7 @@Show me the solution
-+The second version is the one we want to run. This prints to screen everything enclosed in the quote marks, expanding the loop variable name @@ -1245,7 +1239,7 @@
Show me the solution
-+We have a nested loop, i.e. contained within another loop, so for each species in the outer loop, the inner loop (the nested loop) @@ -1343,7 +1337,7 @@
Keypoints Show me the solution -
+The
-l
option makesls
use a long listing format, showing not only the @@ -944,7 +944,7 @@Show me the solution
-+The most recently changed file is listed last when using
-rt
. This can be very useful for finding your most recent @@ -1297,7 +1297,7 @@Show me the solution
-+- No:
.
stands for the current directory.
@@ -1345,7 +1345,7 @@ - No: there is a directory
backup
in @@ -1393,7 +1393,7 @@Show me the solution
-+- No:
pwd
is not the name of a directory.
@@ -1914,7 +1914,7 @@ The
touch
command generates a new file called @@ -2125,7 +2125,7 @@Show me the solution
-+BASH @@ -2221,7 +2221,7 @@
Show me the solution
-+- No. While this would create a file with the correct name, the
@@ -2291,7 +2291,7 @@
Show me the solution
-+We start in the
/Users/jamie/data
directory, and create a new folder calledrecombined
. The second line moves @@ -2382,7 +2382,7 @@Show me the solution
-+OUTPUT @@ -2478,7 +2478,7 @@
Show me the solution
-+If given more than one file name followed by a directory name (i.e. the destination directory must be the last argument), @@ -2563,7 +2563,7 @@
Show me the solution
-+The solution is
3.
1.
shows all files whose names contain zero or more @@ -2687,7 +2687,7 @@Show me the solution
-+BASH @@ -2754,7 +2754,7 @@
Show me the solution
-+BASH @@ -2845,7 +2845,7 @@
Show me the solution
-+The first two sets of commands achieve this objective. The first set uses relative paths to create the top-level directory before the @@ -3167,7 +3167,7 @@
Show me the solution
-+The
@@ -3285,7 +3285,7 @@-n
option specifies a numerical rather than an alphanumerical sort.Show me the solution
-+In the first example with
>
, the string ‘hello’ is written totestfile01.txt
, but the file gets overwritten @@ -3340,7 +3340,7 @@Show me the solution
-+Option 3 is correct. For option 1 to be correct we would only run the
head
command. For option 2 to be correct we would only run @@ -3448,7 +3448,7 @@Show me the solution
-+Option 4 is the solution. The pipe character
|
is used to connect the output from one command to the input of another. @@ -3516,7 +3516,7 @@Show me the solution
-+The
head
command extracts the first 5 lines fromanimals.csv
. Then, the last 3 lines are extracted from the @@ -3582,7 +3582,7 @@Show me the solution
-+BASH @@ -3635,7 +3635,7 @@
Show me the solution
-+Option 4. is the correct answer. If you have difficulty understanding why, try running the commands, or sub-sections of the pipelines (make @@ -3755,7 +3755,7 @@
Show me the solution
-+- This would remove
.txt
files with one-character @@ -3810,7 +3810,7 @@Keypoints
Content from Loops
-Last updated on 2023-08-09 | +
Last updated on 2023-11-09 | Edit this page
- This would remove
- No. While this would create a file with the correct name, the
@@ -2291,7 +2291,7 @@
Show me the solution
-+ - No:
Show me the solution
-+ - No: