...
 
Commits (4)
......@@ -45,4 +45,7 @@ tac_() {
echo "Last username in /etc/passwd: "
tac_ < /etc/passwd | cut_d_f__head_n : 1 1
echo "Last 2 lines of /etc/passwd: "
tac_ < /etc/passwd | head_n 2 | tac_
# vim: ai et sts=4 ts=4 sw=4
#!/bin/sh
# Bash-completion provided as static list of words.
# To test commandline completion for this script, run the following command:
# . ./1-wordlist.inc.bash
......
#!/bin/sh
# Bash-completion dynamically provided by a function.
# To test commandline completion for this script, run the following command:
# . ./2-function.inc.bash
......
......@@ -4,64 +4,114 @@ Shellscripting Examples
Table of Contents
-----------------
* 1 [Basics](./01-basics/)
### 1 [Basics](./01-basics/)
* 1.1 [Indentation](./01-basics/1-indentation.sh)
Examples of indenting code blocks.
* 1.2 [Continuation](./01-basics/2-continuation.sh)
Example of line continuations.
* 1.3 [Arithmetics](./01-basics/3-arithmetics.bash)
Example of performing arithmetics.
* 1.4 [Assignments](./01-basics/4-assignments.sh)
Example for assignment of values.
* 1.5 [Quoting](./01-basics/5-quoting.sh)
Example for here-documents using shell quoting mechanisms.
* 1.6 [Shell Options](./01-basics/6-shell-options.sh)
Example of setting shell options using shebang commandline or set command.
* 1.7 [Bash-only Options](./01-basics/7-bash--only-options.bash)
* 1.8 [Exercises](./01-basics/8-exercises.md)
* 2 [Sourcing](./02-sourcing/)
### 2 [Sourcing](./02-sourcing/)
* 2.1 [Dynamic Sourcing](./02-sourcing/1-dynamic-sourcing.sh)
* 3 [Functions](./03-functions/)
Example of sourcing a file.
### 3 [Functions](./03-functions/)
* 3.1 [Subprogram](./03-functions/1-subprogram.sh)
Function performing a simple subprogram.
* 3.2 [Printing Output](./03-functions/2-printing-output.sh)
Perform a calculation and print the result.
* 3.3 [Filter](./03-functions/3-filter.sh)
Filter stdin to stdout, report on stderr.
* 3.4 [Return Value](./03-functions/4-return-value.sh)
Example of using a function's return value.
* 3.5 [Last Foreground Pipeline](./03-functions/5-last-foreground-pipeline.sh)
Example of a function providing the last foreground status as return value.
* 3.6 [Arguments](./03-functions/6-arguments.sh)
Iterate function arguments.
* 3.7 [Exercises](./03-functions/7-exercises.md)
* 4 [Variables](./04-variables/)
### 4 [Variables](./04-variables/)
* 4.1 [Assignments](./04-variables/1-assignments.sh)
Example of variable assignments and their visibility.
* 4.2 [Local Assignments](./04-variables/2-local-assignments.bash)
Example of local variables in Bash.
* 4.3 [Loops](./04-variables/3-loops.sh)
Example for loops without and with implicit subshell.
* 4.4 [Arguments](./04-variables/4-arguments.sh)
Example of quoting command arguments.
* 4.5 [Parameters](./04-variables/5-parameters.sh)
Examples of evaluating the commandline arguments of a script,
* 4.6 [Defaults](./04-variables/6-defaults.sh)
Example on how to assign defaults to variables if the correpsonding parameters are unset.
* 4.7 [Option Parsing](./04-variables/7-option-parsing.sh)
Example using sed and Bash arrays to parse single-letter-options.
* 4.8 [Exercises](./04-variables/8-exercises.md)
* 5 [Redirection](./05-redirection/)
### 5 [Redirection](./05-redirection/)
* 5.1 [Redirect](./05-redirection/1-redirect.sh)
Redirect script output to a file.
* 5.2 [Pipeline](./05-redirection/2-pipeline.sh)
Appending script output to a logfile, repeating to standard output.
* 5.3 [Custom Password Prompt](./05-redirection/3-custom-password-prompt.bash)
Example switching stdout and stderr to capture output of a function.
* 5.4 [Pipestatus](./05-redirection/4-pipestatus.bash)
* 6 [Subshells](./06-subshells/)
Example using Bash'S PIPESTATUS array.
### 6 [Subshells](./06-subshells/)
* 6.1 [Shell-Variable Version](./06-subshells/1-shell--variable-version.sh)
Changing a variable in a subshell, then accessing it from the main program.
* 6.2 [Fifo](./06-subshells/2-fifo.sh)
Using a FIFO for passing a value from a subshell to the main program.
* 6.3 [Command Redirection](./06-subshells/3-command-redirection.bash)
Bash only: Using command-redirection to pipe output into the main program's stdandard input.
* 6.4 [Tempfile](./06-subshells/4-tempfile.sh)
* 7 [Terminal](./07-terminal/)
Using a temporary file to pass values from a subshell to the main program.
### 7 [Terminal](./07-terminal/)
* 7.1 [Text Attributes](./07-terminal/1-text-attributes.sh)
Example of how to use terminal escape sequences for text attributes.
* 7.2 [Positioning](./07-terminal/2-positioning.sh)
shellcheck disable=SC2034
* 7.3 [Carriage Return](./07-terminal/3-carriage-return.sh)
Example for using carriage return on UNIX terminals.
* 7.4 [Exercises](./07-terminal/4-exercises.md)
* 8 [Powertools](./08-powertools/)
### 8 [Powertools](./08-powertools/)
* 8.1 [Extract (AWK version)](./08-powertools/1-extract.awk.sh)
Show running commands; skip kernel processes (AWK version).
* 8.1 [Extract (sed version)](./08-powertools/1-extract.sed.sh)
Show running commands; skip kernel processes (sed version).
* 8.2 [Awk](./08-powertools/2-awk.sh)
Display the first 16 bytes of every plain file found in /tmp.
* 8.2 [Head](./08-powertools/2-head.sh)
Print up to the frist two lines of very plain file fount in /tmp.
* 8.3 [Textutils Replacements](./08-powertools/3-textutils-replacements.sh)
Examples of awk replacing textutils for common tasks.
* 8.4 [Showdir (AWK version)](./08-powertools/4-showdir.awk.sh)
Using AWK to display complex information formatting.
* 8.5 [Exercises](./08-powertools/5-exercises.md)
* 9 [Trap](./09-trap/)
### 9 [Trap](./09-trap/)
* 9.1 [EXIT](./09-trap/1-exit.sh)
Example for exit event handler.
* 9.1 [SIGUSR1](./09-trap/2-usr1.sh)
Example for UNIX signal handler.
* 9.3 [Exercises](./09-trap/3-exercises.md)
* 0 [Completion](./10-completion/)
### 0 [Completion](./10-completion/)
* 0.1 [Wordlist](./10-completion/1-wordlist.sh)
Bash-completion provided as static list of words.
* 0.2 [Function](./10-completion/2-function.sh)
Bash-completion dynamically provided by a function.
* 0.3 [Exercise](./10-completion/3-exercise.md)
Author and Contact
......
#!/bin/sh
# Scan directory structure and transform to Markdown-TOC for README.
# Convert name of directory or file to section or example number.
# Extract summary comment from file.
# @param string File or directory basename.
file2summary() {
awk 'NR<=2 && /^#[^!]/{sub("^# ", ""); print}' < "$1"
}
# Convert name of directory or file to section or example number.
# @param string File or directory name.
file2nr() {
basename "$1" | perl -pne '
s/.*([0-9]+).*/$1/
......@@ -10,7 +16,7 @@ file2nr() {
}
# Convert name of directory or file to section or example title.
# @param string File or directory basename.
# @param string File or directory name.
file2title() {
basename "$1" | perl -pne '
s/.*[0-9]+-(.*?)/$1/;
......@@ -50,7 +56,6 @@ EOF
# @param string Output filename.
write_stop() {
write "$1" <<EOF
Author and Contact
------------------
......@@ -66,7 +71,6 @@ Tilman Kranz, Wolfstraße 17, D-74937 Spechbach, Germany
EOF
}
# Write text to file and stdout.
# @param string Output filename.
# @param string text
......@@ -115,13 +119,16 @@ for dir in "$project"/[0-9]* ; do
fname=$(basename "$file")
fnr="$(file2nr "$file")"
ftitle=$(file2title "$file")
fsummary=$(file2summary "$file")
echo " * $dnr.$fnr [$ftitle](./$dname/$fname)" >> "$buffer"
test -n "$fsummary" && echo " $fsummary" >> "$buffer"
done
if test "$(wc -l < "$buffer")" -gt 0 ; then
write_line "$target" "* $dnr [$dtitle](./$dname/)"
write_line "$target" "### $dnr [$dtitle](./$dname/)"
write_file "$target" "$buffer"
write_line "$target"
fi
done
......