Chinaunix

标题: 小腳本 [打印本页]

作者: twf_cc    时间: 2007-06-30 21:53
标题: 小腳本
寫了個腳本計算文件單字數目的和根據數量打印 #### 代表數字的 chart,
但有一個問題 , read -r 已用了 , 但反斜線 \ 還是讀不到, 幫忙一下

#! /bin/bash
# words fequence counting and format with a chart
# CYGWIN_NT-6.0 User-PC 1.5.24,GNU bash version 3.2.17
# Jun 30, 2007 twf_cc@yahoo.com.hk
# public domain

# init some stuff
  tempfile=$HOME/junk1.$$
  tempfile2=$HOME/junk2.$$
  msg="abort by user..exiting"
  msg2="Usage: $0 [option][ -c --chart | -n --number ] file"
  msg3="file not found"

  if [ $# -eq 0 ] ; then
     echo "$msg2" >&2
     exit 1
  elif [ $# -eq 1 ] ; then
     file=$1
  elif [ $# -eq 2 ] ; then
     option=$1
     file=$2
  else
     option=$1
     file=$2
  fi

# make something to avoid remaining temp file
# when an accident is happened
  trap "echo $msg >&2 ; rm -f $tempfile $tempfile2 ; exit 1"  1 2 15

# check command line arugment, file is existed
  [ -f "$file" ] || { echo "$file: $msg3" >&2 ; exit 1 ; }

# ok , make a temp file, sorting and counting its word
  cat "$file" | tr -s ' ' '\n' | sort | uniq -c | sort -rn > $tempfile

# make something to the data
  jpg='#'

  while read -r num word
    do
     echo -ne "$num\t$word\t"
     printf '%*s\n' $num | tr " " "$jpg"
    done < $tempfile > $tempfile2
# format the output
   case "$option" in
       -c| --chart) awk '{
                           printf("%-20s%-30s\n", $2, $3)
                           }' $tempfile2
                           ;;

       -n| --number) awk '{
                           printf("%-20s%-30s\n", $2, $1)
                           }' $tempfile2
                           ;;

                  *) awk '{
                           printf("%-20s%-30s%-20s\n", $2, $1, $3)
                           }' $tempfile2
                           ;;
   esac

   rm -f $tempfile $tempfile2
   exit 0
作者: twf_cc    时间: 2007-06-30 22:17
read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
If the -r option is given, this signifies `raw' input, and
    backslash escaping is disabled.

怪了...我還以為記錯了....???




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2