Hour 23

1: Write a function called getCharCount that prints the number of characters in a file. Use wc to obtain the character count.

Linux, FreeBSD, and SunOS (not Solaris), use the -c option for wc, whereas other versions of UNIX use the -m option. Feel free to use the function getOSName.

A1: A possible implementation is
getCharCount() {
    case `getOSName` in
        bsd|sunos|linux)
            WCOPT="-c" ;;
        *)
            WCOPT="-m" ;;
    esac

    wc $WCOPT $@
}

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.