This collection of bash scripts aims to improve readability in complex bash scripts that involve piped data processing. It provides a set of convenient utilities to extract specific elements from piped input, simplifying data manipulation.
These utilities retrieve the corresponding element from the piped input:
echo "one two three four five six seven eight nine ten" | first
one
Alternatively, you can use numeric equivalents like 1
, 2
, 3
, and so on:
echo "one two three four five six seven eight nine ten" | 1
one
You can use select any element from 1 to 10.
Additionally, the last
utility retrieves the last element from the input. To access the elements in reverse order, you can use last-1
, last-2
, last-3
, and so on.
$ echo "one two three four five six" | last
six
$ echo "one two three four five six" | last-1
five
$ echo "one two three four five six" | last-2
four
$ echo "one two three four five six" | last-3
three
$ echo "one two three four five six" | last-4
two
$ echo "one two three four five six" | last-5
one
Copy the scripts somewhere and chmod +x
You can run the provided test suite to validate the functionality of each utility. The test results will be displayed, indicating whether each test passed or failed, represented by a green tick (✓) for passed tests.
$ ./test
Testing 'first':
✓ Test passed
Testing 'second':
✓ Test passed
Testing 'third':
✓ Test passed
Testing 'fifth':
✓ Test passed
Testing 'sixth':
✓ Test passed
Testing 'seventh':
✓ Test passed
Testing 'eighth':
✓ Test passed
Testing 'ninth':
✓ Test passed
Testing 'tenth':
✓ Test passed
Testing 'last':
✓ Test passed
Testing 'last-1':
✓ Test passed
Testing 'last-2':
✓ Test passed
Testing 'last-3':
✓ Test passed
Testing 'last-4':
✓ Test passed
Testing 'last-5':
✓ Test passed