| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # grp.sh: Rudimentary reimplementation of grep. |
| 3 | |
| 4 | E_BADARGS=85 |
| 5 | |
| 6 | if [scip-shell system . . `[`. -z "$1" ] # Check for argument to script. |
| 7 | then |
| 8 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` pattern" |
| 9 | exit $E_BADARGS |
| 10 | fi |
| 11 | |
| 12 | echoscip-shell system . . echo. |
| 13 | |
| 14 | for file in * # Traverse all files in $PWD. |
| 15 | do |
| 16 | output=$(sedscip-shell system . . sed. -n /"$1"/p $file) # Command substitution. |
| 17 | |
| 18 | if [scip-shell system . . `[`. ! -z "$output" ] # What happens if "$output" is not quoted? |
| 19 | then |
| 20 | echoscip-shell system . . echo. -n "$file: " |
| 21 | echoscip-shell system . . echo. "$output" |
| 22 | fi # sed -ne "/$1/s|^|${file}: |p" is equivalent to above. |
| 23 | |
| 24 | echoscip-shell system . . echo. |
| 25 | done |
| 26 | |
| 27 | echoscip-shell system . . echo. |
| 28 | |
| 29 | exit 0 |
| 30 | |
| 31 | # Exercises: |
| 32 | # --------- |
| 33 | # 1) Add newlines to output, if more than one match in any given file. |
| 34 | # 2) Add features. |