letter-count.sh

来自「Shall高级编程」· Shell 代码 · 共 56 行

SH
56
字号
#!/bin/bash# letter-count.sh: Counting letter occurrences in a text file.# Written by Stefano Palmeri.# Used in ABS Guide with permission.# Slightly modified by document author.MINARGS=2          # Script requires at least two arguments.E_BADARGS=65FILE=$1let LETTERS=$#-1   # How many letters specified (as command-line args).                   # (Subtract 1 from number of command line args.)show_help(){	   echo           echo Usage: `basename $0` file letters             echo Note: `basename $0` arguments are case sensitive.           echo Example: `basename $0` foobar.txt G n U L i N U x.	   echo}# Checks number of arguments.if [ $# -lt $MINARGS ]; then   echo   echo "Not enough arguments."   echo   show_help   exit $E_BADARGSfi  # Checks if file exists.if [ ! -f $FILE ]; then    echo "File \"$FILE\" does not exist."    exit $E_BADARGSfi# Counts letter occurrences .for n in `seq $LETTERS`; do      shift      if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then             #  Checks arg.             echo "$1" -\> `cat $FILE | tr -cd  "$1" | wc -c` #  Counting.      else             echo "$1 is not a  single char."      fi  doneexit $?#  This script has exactly the same functionality as letter-count2.sh,#+ but executes faster.#  Why?

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?