diff options
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/each | 21 | ||||
| -rwxr-xr-x | tools/filter | 25 | ||||
| -rwxr-xr-x | tools/map | 21 | ||||
| -rwxr-xr-x | tools/reduce | 27 | ||||
| -rwxr-xr-x | tools/splitat | 14 | ||||
| -rwxr-xr-x | tools/words | 13 |
6 files changed, 121 insertions, 0 deletions
diff --git a/tools/each b/tools/each new file mode 100755 index 0000000..da5ce84 --- /dev/null +++ b/tools/each @@ -0,0 +1,21 @@ +#!/bin/sh + +if [ -z "$1" ]; then + echo "Usage: $0 <command>" + echo "" + echo "Run the given command for each newline-separated line in stdin (think for-each in many programming languages)." + echo 'The iterator variable is called "$ELEM", and the index variable is called "$IDX".' + exit 1 +fi + +i=0 +while IFS=$'\n' read -r line; do + j=0 + for item in $line; do + eval "X${j}='$item'" + j=$((j+1)) + done + + IDX=$i ELEM="$line" eval "$1" + i=$((i+1)) +done diff --git a/tools/filter b/tools/filter new file mode 100755 index 0000000..94258d0 --- /dev/null +++ b/tools/filter @@ -0,0 +1,25 @@ +#!/bin/sh + +if [ -z "$1" ]; then + echo "Usage: $0 <command>" + echo "" + echo "Run the given test expression for each newline-separated line in stdin, and echo only the lines that return zero." + echo "(Think filter in many programming languages)." + echo 'The iterator variable is called "$ELEM", and the index variable is called "$IDX".' + exit 1 +fi + +i=0 +while IFS=$'\n' read -r line; do + j=0 + for item in $line; do + eval "X${j}='$item'" + j=$((j+1)) + done + + IDX=$i ELEM="$line" eval "test $1" + if [ $? -eq 0 ]; then + echo "$line" + fi + i=$((i+1)) +done diff --git a/tools/map b/tools/map new file mode 100755 index 0000000..8c4e5ef --- /dev/null +++ b/tools/map @@ -0,0 +1,21 @@ +#!/bin/sh + +if [ -z "$1" ]; then + echo "Usage: $0 <expr>" + echo "" + echo "Give the given expression for each newline-separated line in stdin as an argument to echo (think map in many programming languages)." + echo 'The iterator variable is called "$ELEM", and the index variable is called "$IDX".' + exit 1 +fi + +i=0 +while IFS=$'\n' read -r line; do + j=0 + for item in $line; do + eval "X${j}='$item'" + j=$((j+1)) + done + + IDX=$i ELEM="$line" eval "echo $1" + i=$((i+1)) +done diff --git a/tools/reduce b/tools/reduce new file mode 100755 index 0000000..effb98d --- /dev/null +++ b/tools/reduce @@ -0,0 +1,27 @@ +#!/bin/sh + +if [ "$#" -ne 2 ]; then + echo "Usage: $0 <init> <expr>" + echo "" + echo "Run the given command for each newline-separated line in stdin." + echo 'The expression computes the next value of "$ACC" (think reduce in many programming languages).' + echo 'The initial value of "$ACC" is the first argument to the script.' + echo 'The iterator variable is called "$ELEM", and the index variable is called "$IDX".' + echo 'The final value of "$ACC" is printed after all lines have been processed.' + exit 1 +fi + +i=0 +ACC="$1" +while IFS=$'\n' read -r line; do + j=0 + for item in $line; do + eval "X${j}='$item'" + j=$((j+1)) + done + + IDX=$i ELEM="$line" eval "ACC=\"$2\"" + i=$((i+1)) +done + +echo "$ACC" diff --git a/tools/splitat b/tools/splitat new file mode 100755 index 0000000..d41521e --- /dev/null +++ b/tools/splitat @@ -0,0 +1,14 @@ +#!/bin/sh + +if [ "$#" -ne 1 ]; then + echo "Usage: $0 <separator>" + echo "" + echo "Split the stdin into lines, splitting at the given separator character." + exit 1 +fi + +while IFS=$'\n' read -r line; do + for part in $(echo "$line" | tr "$1" '\n'); do + echo "$part" + done +done diff --git a/tools/words b/tools/words new file mode 100755 index 0000000..ac76d0d --- /dev/null +++ b/tools/words @@ -0,0 +1,13 @@ +#!/bin/sh + +if [ "$#" -ne 0 ]; then + echo "Usage: $0" + echo "" + echo "Echo each whitespace-separated word in stdin on a new line." + exit 1 +fi + +i=0 +for word in $(cat); do + echo "$word" +done |
