uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

ex7_countmatch (486B)


      1 #!/bin/sh
      2 
      3 main() {
      4         xread "String: " str
      5         test -z "${str}" && echo "${0##*/}: give a non-empty string" 1>&2 && exit 1
      6 
      7         xread "File: " file
      8         test ! -f "${file}" && echo "${0##*/}: ${file}: no such file" 1>&2 && exit 1
      9 
     10         awk "/${str}/ {cnt++} END {print cnt}" ${file}
     11 }
     12 
     13 # A portable `read` function with behavior similar to
     14 # `read -erp`.
     15 xread() {
     16         printf "%s" "${1}" && read -r ${2}
     17 }
     18 
     19 # Pass all command line arguments to `main`.
     20 main "$@"