Skip to content

Latest commit

 

History

History

snip

Snippets

The scripts in here, are not all necessary complete & runnable scripts. They mostly contain snippets of re-usable shell script code, to be incorporated in other scripts.

count_evens.sh — Count Even Numbers

Simple script to count only the even numbers in a list of integers. The main point is to serve as an example for the continue command, discussed under continue Command.

indented_heredoc.sh — Indented Here Documents

In scripts, here documents (heredocs) can be indented if they start with <<-. However, the indentation must be performed with hard ‹TAB› characters (not spaces). This snippet in this script shows various hard ‹TAB› indented heredocs. The output will show that the shell removes all hard ‹TAB› indentation.

linux_console.sh — Linux Console Palette

This file contains escape sequences to set the palette of 16 colours that a Linux console supports. These sequences are unique to the Linux console, which otherwise emulates a VT-102 terminal.

menu_demo.sh — Menu Demo

Interactively displays a list (or menu) of options, from which the user can select one by number. It does not require the select command, while select_demo.bash does.

mult_odds.sh — Multiply Odd Numbers

Simple script to output a multiplication table for only the odd numbers in a list. This script serves as an example for the continue command that takes a positive integer argument, discussed under continue Command in the wiki.

process_lines.sh — Line-Wise Processing

To process input line-by-line, we need to change IFS (Input Field Separator), part of the POSIX Shell Variables; and use the read(1p) built-in.

For safety, we add the -r (‘raw’) option, which will prevent read from interpreting backslash (\) escape sequences. This means that if the input contains a backslash followed by a newline character, read will interpret it as two separate characters, instead of ignoring the newline character.

Here's an example to illustrate the difference:

#!/usr/bin/env sh

printf 'Line 1\\\nLine 2\n' | while read line; do
    printf '%s\n' "$line"
done

printf 'Line 1\\\nLine 2\n' | while read -r line; do
    printf '%s\n' "$line"
done

The first while loop uses read without the -r option, so it interprets the backslash followed by a newline character as an escape sequence and ignores the newline character. As a result, it outputs a single line: Line 1Line 2.

The second while loop uses read with the -r option, so it treats the backslash and newline characters as separate characters. As a result, it outputs two lines: Line 1\ and Line 2.

See misc/line_by_line.sh for an example script that process standard input line-by-line, without using cat(1p).

select_demo.bash — Select Demo

This script works with bash and zsh (maybe ksh), and uses the non-POSIX select command to display a menu of options, from which the user is required to choose one by number.

sosedemo.sh — Standard Out, Standard Error

This script only contains commands that write lines of text. Some lines are written to standard error (2), and some lines are written to standard output (1). Several workable options are shown. The 1>&2 (or >&2) ‘binding’ works on all commands that produce standard output, not just echo.

Use the script to experiment with output redirection:

$ sh sosedemo.sh  > /dev/null          #←show only stderr output.
$ sh sosedemo.sh 1> /dev/null          #←same as above.
$ sh sosedemo.sh 2> /dev/null          #←show only stdout output.
$ sh sosedemo.sh  > /dev/null 2>&1     #←snow nothing.

stdin_or_file.sh — Read Standard Input or File

Many programs, such as cat(1p) and sort(1p), can read from standard input if no files are specified as command-line arguments. To read from standard input until the end-of-file (‹EOF›) marker is reached, you can use $(cat). Alternatively, you can use $(cat "$1") to read from a file that is passed as the first argument to the program.

Note that the GNU Core Utilities versions of the cat(1) and sort(1) commands have significantly more useful options than the POSIX versions. Keep this in mind if POSIX-compatibility is a concern.

See misc/line_by_line.sh for an example that process standard input line-by-line, without using cat(1p).

unique_paths.bash — Unique Paths

This script works with bash and zsh (maybe ksh), and uses non-POSIX arrays to eliminate duplicate paths as found in the PATH environment variable. Assuming the script is in the current directory:

$> . ./unique_paths.bash
$> unique_paths "/abc:/def/:/abc/:/def"
#→ abc:/def
$> export PATH="$(unique_paths $PATH)"

Note that paths are normalised, so that /abc and /abc/ are treated the same.

unique_paths.sh — Unique Paths (POSIX)

This script works POSIX shells to eliminate duplicate paths as found in the PATH environment variable. Assuming the script is in the current directory:

$> . ./unique_paths.sh
$> unique_paths "/abc:/def/:/abc/:/def"
#→ abc:/def
$> export PATH="$(unique_paths $PATH)"

Note that paths are normalised, so that /abc and /abc/ are treated the same.