mirror of https://github.com/tLDP/LDP
35 lines
654 B
Bash
35 lines
654 B
Bash
#!/bin/bash
|
|
# grp.sh: Rudimentary reimplementation of grep.
|
|
|
|
E_BADARGS=85
|
|
|
|
if [ -z "$1" ] # Check for argument to script.
|
|
then
|
|
echo "Usage: `basename $0` pattern"
|
|
exit $E_BADARGS
|
|
fi
|
|
|
|
echo
|
|
|
|
for file in * # Traverse all files in $PWD.
|
|
do
|
|
output=$(sed -n /"$1"/p $file) # Command substitution.
|
|
|
|
if [ ! -z "$output" ] # What happens if "$output" is not quoted?
|
|
then
|
|
echo -n "$file: "
|
|
echo "$output"
|
|
fi # sed -ne "/$1/s|^|${file}: |p" is equivalent to above.
|
|
|
|
echo
|
|
done
|
|
|
|
echo
|
|
|
|
exit 0
|
|
|
|
# Exercises:
|
|
# ---------
|
|
# 1) Add newlines to output, if more than one match in any given file.
|
|
# 2) Add features.
|