Writing a simple bash filter

To write a simple bash filter application, start by declaring your filter as a bash script with #!/usr/bin/env bash. This informs your shell what interpreter to use when running the script.

The trick to writing a filter is to read lines from a filename if supplied ${1}, or from /dev/stdin if no filename is supplied.

An example script lowercase

#!/usr/bin/env bash

while read line; do
    echo ${line,,}
done < "${1:-/dev/stdin}"

This script can then be utilized in a pipeline, e.g. cat file | lowercase or by feeding it a filename,lowercase file.

 

Dette indlæg blev udgivet i Programmering, Shellscript. Bogmærk permalinket.

Skriv et svar