Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Bash scripting course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Junier
Bash scripting course
Commits
cf9b79a6
Commit
cf9b79a6
authored
Oct 4, 2021
by
Thomas Junier
Browse files
Options
Downloads
Patches
Plain Diff
self-test and prerequisites
parent
b58456f8
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Makefile
+5
-1
5 additions, 1 deletion
Makefile
prerequisites.md
+34
-8
34 additions, 8 deletions
prerequisites.md
self-test.md
+166
-0
166 additions, 0 deletions
self-test.md
with
205 additions
and
9 deletions
Makefile
+
5
−
1
View file @
cf9b79a6
.PHONY
:
slides
.PHONY
:
slides
all
:
slides rush
all
:
slides rush
self-test.pdf
slides
:
slides
:
$(
MAKE
)
-C
./slides
$(
MAKE
)
-C
./slides
...
@@ -16,5 +16,9 @@ exam_questions.pdf: exam_questions.md Makefile
...
@@ -16,5 +16,9 @@ exam_questions.pdf: exam_questions.md Makefile
--highlight-style
zenburn
\
--highlight-style
zenburn
\
--resource-path
images
$<
-o
$@
--resource-path
images
$<
-o
$@
self-test.pdf
:
self-test.md
pandoc
--standalone
--metadata-file
~/.mdpdf.yaml
--to
=
latex
\
--filter
pandoc-crossref
--citeproc
--output
$@
$<
mrproper
:
mrproper
:
$(
RM
)
shell-scripting_beamer.pdf exam_questions.pdf
$(
RM
)
shell-scripting_beamer.pdf exam_questions.pdf
This diff is collapsed.
Click to expand it.
prerequisites.md
+
34
−
8
View file @
cf9b79a6
...
@@ -22,8 +22,9 @@ Shells in General
...
@@ -22,8 +22,9 @@ Shells in General
-----------------
-----------------
*
What a shell is
*
What a shell is
*
What kind of work is typically done in shells
*
How to launch a shell on your machine
*
How to launch a shell on your machine
*
How one works in the shell, especially as compared with working in a graphical
environment
Commands
Commands
--------
--------
...
@@ -42,13 +43,38 @@ The Filesystem
...
@@ -42,13 +43,38 @@ The Filesystem
*
How to list files in the current directory (and, for that matter, in any
*
How to list files in the current directory (and, for that matter, in any
directory)
directory)
*
How to move around the filesystem
*
How to move around the filesystem
*
How to rename and move files
Text Files
----------
Participants are expected to know how to use a Unix shell interactively, i.e.,
*
What a text file is, how it differs from, say, a Word document, and how this
moving around the filesystem, understanding pathnames, lauching programs that
relates to shell commands
work on data, redirecting output to files or other programs, etc.
They are also expected to have some familiarity with basic shell
utilities like grep, cut, tr, etc (although no expert skills are required).
Notions of programming (in any language) will be useful, but not essential.
Finally, some proficiency with a text editor is a must.
Redirection and Pipelines
-------------------------
*
How to redirect commands' input and output to/from files
*
How to pass the output of one command to another command's input
*
What a pipeline is
Familiar Shell Tools
====================
You should know the purpose of the following tools, and be able to understand
what they do (though in-depth knowledge is not required):
*
`cut`
*
`find`
*
`grep`
*
`head`
*
`man`
*
`sort`
*
`tail`
*
`tr`
*
`wc`
Text Editor
===========
We'll be writing code, so you need to be comfortable with a text editor/IDE.
This diff is collapsed.
Click to expand it.
self-test.md
0 → 100644
+
166
−
0
View file @
cf9b79a6
---
title
:
Self-Test Questions
subtitle
:
for the SIB Shell Programming Course
author
:
Thomas Junier
date
:
\today{}
classoption
:
-onecolumn
number-sections
:
false
---
**NOTE**
All questions have between 0 and 5 (included) correct answers.
### Question 1
Which part the following command is an _option_?
```
bash
$
ls
-l
```
*
[ ] none
*
[x]
`-l`
*
[ ] Unix commands do not have options
*
[ ]
`ls`
*
[ ] both
### Question 2
How can I obtain help about the
`mv`
command?
*
[ ]
`manual mv`
*
[x]
`man mv`
*
[ ]
`mv help`
*
[ ]
`describe mv`
*
[ ]
`help mv`
### Question 3
Suppose you are logged in as
`myself`
, and currently working in
`/home/myself/project/`
. Which of the following commands will
bring you to your home directory?
*
[ ]
`cd .`
*
[x]
`cd /home/myself`
*
[x]
`cd`
*
[x]
`cd $HOME`
*
[x]
`cd ..`
### Question 4
Which of the following terms best describes any document that is _not_ text in
the sense of a Unix text file?
*
[ ] CSS
*
[ ] graphics
*
[x] binary
*
[ ] HTML
*
[ ] PDF
### Question 5
What operator(s) redirect(s) a command's output into a file (possibly, but not
necessarily, creating or deleting it beforehand)?
*
[ ]
`%`
*
[x]
`>`
*
[ ]
`|`
*
[x]
`>>`
*
[ ]
`&`
### Question 6
Why does this not replace
`myfile`
with a sorted version of itself?
```
bash
$
sort
< myfile
>
myfile
# bad!
```
*
[ ] the file to be
`sort`
ed and the output file must both be passed as arguments
*
[x] redirections occur before execution, so
`myfile`
is empty when
`sort`
runs
*
[ ]
`myfile`
has the wrong permissions
*
[ ] sort always deletes its argument
*
[ ] the file to be
`sort`
ed must be passed as an argument
### Question 7
These two commands work without problems and produce the same output:
```
bash
$
grep
tiffany /etc/passwd
$
grep
tiffany < /etc/passwd
```
So why does this work...
```
bash
$
tr
':'
';'
< /etc/passwd
```
...but not this:
```
bash
$
tr
':'
';'
/etc/passwd
```
*
[ ]
`tr`
is not allowed to read
`/etc/passwd`
for security reasons
*
[ ]
`tr`
expects its input in a file named by its first argument
*
[ ]
`tr`
expects its input in a file named by its third argument
*
[ ]
`tr`
expects its input in a file named by its second argument
*
[x]
`tr`
expects its input on standard input
### Question 8
What would be the most straightforward way of displaying the first ten lines of
file
`myfile`
that contain the string
`GAATTC`
?
*
[ ]
`grep GAATTC > tmp; head < tmp`
*
[ ]
`find GAATTC < myfile -n 10`
*
[ ]
`grep GAATTC > tmp; head < tmp; rm tmp`
*
[x]
`grep GAATTC < myfile | head`
*
[ ]
`head < myfile | grep GAATTC`
### Question 9
What is wrong with this?
```
bash
$
cat
myfile |
grep
'^[a-z]\+'
>>
output.txt
```
*
[ ] The regular expression is wrong
*
[ ] There will be a bottleneck because
`cat`
is much slower than
`grep`
*
[ ] There must be a '
`<`
' before
`myfile`
*
[ ] There is nothing wrong with this command
*
[x]
`cat`
is unnecessary and wastes resources
### Question 10
You are given a file named
`sci_names.txt`
with scientific names, one per line, as follows:
```
Panthera leo
Balaena mysticetus
Panthera tigris
Panthera pardus
Corvus monedula
Corvus corax
Solanum tuberosum
Bacillus subtilis
...
```
Your task is to find the genus with the most species (in the snippet above, that
would be _Panthera_, with three species). The expected output is just the genus
name, nothing else. Which command does the job?
*
[ ]
`cut -d' ' -f1 < sci_names.txt | sort | uniq -c | sort -k1,1rn | head -1 | tr -d ' 0-9' | wc -c`
*
[ ]
`cut -d' ' -f1 < sci_names.txt | uniq -c | sort -k1,1rn | head -1 | tr -d ' 0-9' | wc -c`
*
[ ] A 100-line Python script
*
[ ]
`cut -d ' ' -f1 < sci_names.txt | sort | uniq -c | sort -k1,1rn`
*
[x]
`cut -d' ' -f1 < sci_names.txt | sort | uniq -c | sort -k1,1rn | head -1 | tr -d ' 0-9'`
**Note**
there are plenty of solutions to this problem. The choices above avoid
some of the more powerful shell tools (like
`sed`
and
`awk`
) not because they're
not suitable (they are!) but because they may be less familiar.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment