bash学习备忘录

来源:互联网 发布:a标签 js单击事件 编辑:程序博客网 时间:2024/05/20 22:27

教材: LINUX SHELL编程从学习到精通

1.shell怎样输出换行

在做例:8-24例子时候有需要加入回车

教材中使用是

echo "" #换行

作者使用的是fedora,我使用的是ubuntu 14.04,这个不起作用

使用

echo -e ""可以,

如果使用

echo -e "\n"

就会回车两次了.


2. 在13.6.2 不使用局部变量的递归中代码好像有问题,参考新代码:

#!/bin/bash
move=0
dohanoi()
{
   if [ $1 -eq 0 ]
   then
      echo -n ""
   elif [ $1 -eq 1 ]
   then
      echo "move $2 ----> $4"
      let "move=move+1"
      echo "move=$move"
   elif [ $1 -eq 2 ]
   then
      echo "move $2 ----> $3"
      let "move=move+1"
      echo "move=$move"
      echo "move $2 ----> $4"
      let "move=move+1"
      echo "move=$move"
      echo "move $3 ----> $4"
      let "move=move+1"
      echo "move=$move"
   else
      dohanoi "$(($1-1))" $2 $4 $3
      echo "move $2 ----> $4"
      let "move=move+1"
      echo "move=$move"
      dohanoi "$(($1-1))" $3 $2 $4
   fi
  }
echo "Please input the num of disk:"
  read num
  dohanoi $num 'A' 'B' 'C'
  echo "move=$move"

3.特别需要注意if语句中的空格

 if ["$num" -eq 0 ]
由于缺少相应的空格会提示:

./function13.sh: 行 5: [-1579: 未找到命令

4.在例子15-12中:

#!/bin/bash
echo "number of arguments is $#"
echo "What you input is: "
while [[ "$*" != "" ]]
do
    echo "$1"
    shift
done
如果把""改成" ",那会导致无限循环输出空.

5.在例子16-11中:

#!/bin/bash
trap 'echo "before execute line:$LINENO,a=$a,b=$b,c=$c"' DEBUG
a=0
b=2
c=100
while :
do
        if (( a >= 10 ))
         then
           break
        fi
        let "a=a+2"
        let "b=b*2"
        let "c=C-10"
done


sh -x trapdebug.sh

输出结果:

+ trap echo "before execute line:$LINENO,a=$a,b=$b,c=$c" DEBUG
trap: DEBUG: bad trap
+ a=0
+ b=2
+ c=100
+ :
+ a 10
trapdebug.sh: 10: trapdebug.sh: a: not found
+ let a=a+2
trapdebug.sh: 14: trapdebug.sh: let: not found
+ let b=b*2
trapdebug.sh: 15: trapdebug.sh: let: not found
+ let c=C-10
trapdebug.sh: 16: trapdebug.sh: let: not found
+ :
+ a 10
trapdebug.sh: 10: trapdebug.sh: a: not found
+ let a=a+2
trapdebug.sh: 14: trapdebug.sh: let: not found
+ let b=b*2
trapdebug.sh: 15: trapdebug.sh: let: not found
+ let c=C-10
trapdebug.sh: 16: trapdebug.sh: let: not found
+ :
+ a 10
trapdebug.sh: 10: trapdebug.sh: a: not found
+ let a=a+2
trapdebug.sh: 14: trapdebug.sh: let: not found
+ let b=b*2
trapdebug.sh: 15: trapdebug.sh: let: not found
+ let c=C-10
trapdebug.sh: 16: trapdebug.sh: let: not found
+ :
+ a 10
trapdebug.sh: 10: trapdebug.sh: a: not found
+ let a=a+2
trapdebug.sh: 14: trapdebug.sh: let: not found

修改文件:

#!/bin/bash

trap 'echo "before execute line:$LINENO,a=$a,b=$b,c=$c"' DEBUG

a=0
b=2
c=100
while :
do
        if (( a >= 10 ))
         then
           break
        fi
        let "a=a+2"
        let "b=b*2"
        let "c=C-10"
done

运行./trapdebug.sh

+ trap 'echo "before execute line:$LINENO,a=$a,b=$b,c=$c"' DEBUG
++ echo 'before execute line:5,a=,b=,c='
before execute line:5,a=,b=,c=
+ a=0
++ echo 'before execute line:6,a=0,b=,c='
before execute line:6,a=0,b=,c=
+ b=2
++ echo 'before execute line:7,a=0,b=2,c='
before execute line:7,a=0,b=2,c=
+ c=100
++ echo 'before execute line:8,a=0,b=2,c=100'
before execute line:8,a=0,b=2,c=100
+ :
++ echo 'before execute line:10,a=0,b=2,c=100'
before execute line:10,a=0,b=2,c=100
+ ((  a >= 10  ))
++ echo 'before execute line:14,a=0,b=2,c=100'
before execute line:14,a=0,b=2,c=100
+ let a=a+2
++ echo 'before execute line:15,a=2,b=2,c=100'
before execute line:15,a=2,b=2,c=100
+ let 'b=b*2'
++ echo 'before execute line:16,a=2,b=4,c=100'
before execute line:16,a=2,b=4,c=100
+ let c=C-10
++ echo 'before execute line:8,a=2,b=4,c=-10'
before execute line:8,a=2,b=4,c=-10
+ :
++ echo 'before execute line:10,a=2,b=4,c=-10'
before execute line:10,a=2,b=4,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:14,a=2,b=4,c=-10'
before execute line:14,a=2,b=4,c=-10
+ let a=a+2
++ echo 'before execute line:15,a=4,b=4,c=-10'
before execute line:15,a=4,b=4,c=-10
+ let 'b=b*2'
++ echo 'before execute line:16,a=4,b=8,c=-10'
before execute line:16,a=4,b=8,c=-10
+ let c=C-10
++ echo 'before execute line:8,a=4,b=8,c=-10'
before execute line:8,a=4,b=8,c=-10
+ :
++ echo 'before execute line:10,a=4,b=8,c=-10'
before execute line:10,a=4,b=8,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:14,a=4,b=8,c=-10'
before execute line:14,a=4,b=8,c=-10
+ let a=a+2
++ echo 'before execute line:15,a=6,b=8,c=-10'
before execute line:15,a=6,b=8,c=-10
+ let 'b=b*2'
++ echo 'before execute line:16,a=6,b=16,c=-10'
before execute line:16,a=6,b=16,c=-10
+ let c=C-10
++ echo 'before execute line:8,a=6,b=16,c=-10'
before execute line:8,a=6,b=16,c=-10
+ :
++ echo 'before execute line:10,a=6,b=16,c=-10'
before execute line:10,a=6,b=16,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:14,a=6,b=16,c=-10'
before execute line:14,a=6,b=16,c=-10
+ let a=a+2
++ echo 'before execute line:15,a=8,b=16,c=-10'
before execute line:15,a=8,b=16,c=-10
+ let 'b=b*2'
++ echo 'before execute line:16,a=8,b=32,c=-10'
before execute line:16,a=8,b=32,c=-10
+ let c=C-10
++ echo 'before execute line:8,a=8,b=32,c=-10'
before execute line:8,a=8,b=32,c=-10
+ :
++ echo 'before execute line:10,a=8,b=32,c=-10'
before execute line:10,a=8,b=32,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:14,a=8,b=32,c=-10'
before execute line:14,a=8,b=32,c=-10
+ let a=a+2
++ echo 'before execute line:15,a=10,b=32,c=-10'
before execute line:15,a=10,b=32,c=-10
+ let 'b=b*2'
++ echo 'before execute line:16,a=10,b=64,c=-10'
before execute line:16,a=10,b=64,c=-10
+ let c=C-10
++ echo 'before execute line:8,a=10,b=64,c=-10'
before execute line:8,a=10,b=64,c=-10
+ :
++ echo 'before execute line:10,a=10,b=64,c=-10'
before execute line:10,a=10,b=64,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:12,a=10,b=64,c=-10'
before execute line:12,a=10,b=64,c=-10
+ break

如果修改成:

#!/bin/bash
trap 'echo "before execute line:$LINENO,a=$a,b=$b,c=$c"' DEBUG
set -x
a=0
b=2
c=100
while :
do
        if (( a >= 10 ))
         then
           break
        fi
        let "a=a+2"
        let "b=b*2"
        let "c=C-10"
done


运行结果:

before execute line:3,a=,b=,c=
++ echo 'before execute line:4,a=,b=,c='
before execute line:4,a=,b=,c=
+ a=0
++ echo 'before execute line:5,a=0,b=,c='
before execute line:5,a=0,b=,c=
+ b=2
++ echo 'before execute line:6,a=0,b=2,c='
before execute line:6,a=0,b=2,c=
+ c=100
++ echo 'before execute line:7,a=0,b=2,c=100'
before execute line:7,a=0,b=2,c=100
+ :
++ echo 'before execute line:9,a=0,b=2,c=100'
before execute line:9,a=0,b=2,c=100
+ ((  a >= 10  ))
++ echo 'before execute line:13,a=0,b=2,c=100'
before execute line:13,a=0,b=2,c=100
+ let a=a+2
++ echo 'before execute line:14,a=2,b=2,c=100'
before execute line:14,a=2,b=2,c=100
+ let 'b=b*2'
++ echo 'before execute line:15,a=2,b=4,c=100'
before execute line:15,a=2,b=4,c=100
+ let c=C-10
++ echo 'before execute line:7,a=2,b=4,c=-10'
before execute line:7,a=2,b=4,c=-10
+ :
++ echo 'before execute line:9,a=2,b=4,c=-10'
before execute line:9,a=2,b=4,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:13,a=2,b=4,c=-10'
before execute line:13,a=2,b=4,c=-10
+ let a=a+2
++ echo 'before execute line:14,a=4,b=4,c=-10'
before execute line:14,a=4,b=4,c=-10
+ let 'b=b*2'
++ echo 'before execute line:15,a=4,b=8,c=-10'
before execute line:15,a=4,b=8,c=-10
+ let c=C-10
++ echo 'before execute line:7,a=4,b=8,c=-10'
before execute line:7,a=4,b=8,c=-10
+ :
++ echo 'before execute line:9,a=4,b=8,c=-10'
before execute line:9,a=4,b=8,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:13,a=4,b=8,c=-10'
before execute line:13,a=4,b=8,c=-10
+ let a=a+2
++ echo 'before execute line:14,a=6,b=8,c=-10'
before execute line:14,a=6,b=8,c=-10
+ let 'b=b*2'
++ echo 'before execute line:15,a=6,b=16,c=-10'
before execute line:15,a=6,b=16,c=-10
+ let c=C-10
++ echo 'before execute line:7,a=6,b=16,c=-10'
before execute line:7,a=6,b=16,c=-10
+ :
++ echo 'before execute line:9,a=6,b=16,c=-10'
before execute line:9,a=6,b=16,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:13,a=6,b=16,c=-10'
before execute line:13,a=6,b=16,c=-10
+ let a=a+2
++ echo 'before execute line:14,a=8,b=16,c=-10'
before execute line:14,a=8,b=16,c=-10
+ let 'b=b*2'
++ echo 'before execute line:15,a=8,b=32,c=-10'
before execute line:15,a=8,b=32,c=-10
+ let c=C-10
++ echo 'before execute line:7,a=8,b=32,c=-10'
before execute line:7,a=8,b=32,c=-10
+ :
++ echo 'before execute line:9,a=8,b=32,c=-10'
before execute line:9,a=8,b=32,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:13,a=8,b=32,c=-10'
before execute line:13,a=8,b=32,c=-10
+ let a=a+2
++ echo 'before execute line:14,a=10,b=32,c=-10'
before execute line:14,a=10,b=32,c=-10
+ let 'b=b*2'
++ echo 'before execute line:15,a=10,b=64,c=-10'
before execute line:15,a=10,b=64,c=-10
+ let c=C-10
++ echo 'before execute line:7,a=10,b=64,c=-10'
before execute line:7,a=10,b=64,c=-10
+ :
++ echo 'before execute line:9,a=10,b=64,c=-10'
before execute line:9,a=10,b=64,c=-10
+ ((  a >= 10  ))
++ echo 'before execute line:11,a=10,b=64,c=-10'
before execute line:11,a=10,b=64,c=-10
+ break

6.在例子 17-1中:

415页第15行用正则表达式"$"匹配行首,应该是行尾巴.

在输入时需要主要>>CLOUD前不用留空格,在做分界的CLOUD前也不要有空格.

在例子17-1 htmlconver2.sh中:

#!/bin/bash
cat <<CLOUD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transtitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
   <HEAD>
      <TITLE>
      教授的信息
      </TITLE>
   </HEAD>
   <BODY>
      <TABLE>
CLOUD
awk 'BEGIN {FS=":";OFS="</TD><TD>"} sub(/^/,"<TR><TD>") sub(/$/,"</TD><TR>") {print  $1,$2,$3,$4}'
cat <<CLOUD
      </TABLE>
   </BODY>
</HTML>
CLOUD

运行结果:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transtitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
   <HEAD>
      <TITLE>
      教授的信息
      </TITLE>
   </HEAD>
   <BODY>
      <TABLE>
<TR><TD>B Liu</TD><TD>Shanghai Jiaotong University</TD><TD>Shanghai</TD><TD>China</TD></TR>
<TR><TD>C Lin</TD><TD>University of Toronto</TD><TD>Toronto</TD><TD>Canada</TD></TR>
<TR><TD>D Hou</TD><TD>Beijing University</TD><TD>Beijing</TD><TD>China</TD></TR>
<TR><TD>J Luo</TD><TD>Southeast University</TD><TD>Nanjing</TD><TD>China</TD></TR>
<TR><TD>Y Zhang</TD><TD>Victory University</TD><TD>Melbourne</TD><TD>Australia</TD></TR>
      </TABLE>
   </BODY>
</HTML>

注意到这里书中使用的不是sub,而是gsub:

#!/bin/bash
cat <<CLOUD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transtitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
   <HEAD>
      <TITLE>
      教授的信息
      </TITLE>
   </HEAD>
   <BODY>
      <TABLE>
CLOUD
awk 'BEGIN {FS=":";OFS="</TD><TD>"} gsub(/^/,"<TR><TD>") gsub(/$/,"</TD><TR>") {print  $1,$2,$3,$4}'
cat <<CLOUD
      </TABLE>
   </BODY>
</HTML>
CLOUD
运行结果:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transtitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
   <HEAD>
      <TITLE>
      教授的信息
      </TITLE>
   </HEAD>
   <BODY>
      <TABLE>
<TR><TD>B<TR><TD> <TR><TD>L<TR><TD>i<TR><TD>u<TR><TD></TD><TD><TR><TD>S<TR><TD>h<TR><TD>a<TR><TD>n<TR><TD>g<TR><TD>h<TR><TD>a<TR><TD>i<TR><TD> <TR><TD>J<TR><TD>i<TR><TD>a<TR><TD>o<TR><TD>t<TR><TD>o<TR><TD>n<TR><TD>g<TR><TD> <TR><TD>U<TR><TD>n<TR><TD>i<TR><TD>v<TR><TD>e<TR><TD>r<TR><TD>s<TR><TD>i<TR><TD>t<TR><TD>y<TR><TD></TD><TD><TR><TD>S<TR><TD>h<TR><TD>a<TR><TD>n<TR><TD>g<TR><TD>h<TR><TD>a<TR><TD>i<TR><TD></TD><TD><TR><TD>C<TR><TD>h<TR><TD>i<TR><TD>n<TR><TD>a<TR><TD></TD><TR>
<TR><TD>C<TR><TD> <TR><TD>L<TR><TD>i<TR><TD>n<TR><TD></TD><TD><TR><TD>U<TR><TD>n<TR><TD>i<TR><TD>v<TR><TD>e<TR><TD>r<TR><TD>s<TR><TD>i<TR><TD>t<TR><TD>y<TR><TD> <TR><TD>o<TR><TD>f<TR><TD> <TR><TD>T<TR><TD>o<TR><TD>r<TR><TD>o<TR><TD>n<TR><TD>t<TR><TD>o<TR><TD></TD><TD><TR><TD>T<TR><TD>o<TR><TD>r<TR><TD>o<TR><TD>n<TR><TD>t<TR><TD>o<TR><TD></TD><TD><TR><TD>C<TR><TD>a<TR><TD>n<TR><TD>a<TR><TD>d<TR><TD>a<TR><TD></TD><TR>
<TR><TD>D<TR><TD> <TR><TD>H<TR><TD>o<TR><TD>u<TR><TD></TD><TD><TR><TD>B<TR><TD>e<TR><TD>i<TR><TD>j<TR><TD>i<TR><TD>n<TR><TD>g<TR><TD> <TR><TD>U<TR><TD>n<TR><TD>i<TR><TD>v<TR><TD>e<TR><TD>r<TR><TD>s<TR><TD>i<TR><TD>t<TR><TD>y<TR><TD></TD><TD><TR><TD>B<TR><TD>e<TR><TD>i<TR><TD>j<TR><TD>i<TR><TD>n<TR><TD>g<TR><TD></TD><TD><TR><TD>C<TR><TD>h<TR><TD>i<TR><TD>n<TR><TD>a<TR><TD></TD><TR>
<TR><TD>J<TR><TD> <TR><TD>L<TR><TD>u<TR><TD>o<TR><TD></TD><TD><TR><TD>S<TR><TD>o<TR><TD>u<TR><TD>t<TR><TD>h<TR><TD>e<TR><TD>a<TR><TD>s<TR><TD>t<TR><TD> <TR><TD>U<TR><TD>n<TR><TD>i<TR><TD>v<TR><TD>e<TR><TD>r<TR><TD>s<TR><TD>i<TR><TD>t<TR><TD>y<TR><TD></TD><TD><TR><TD>N<TR><TD>a<TR><TD>n<TR><TD>j<TR><TD>i<TR><TD>n<TR><TD>g<TR><TD></TD><TD><TR><TD>C<TR><TD>h<TR><TD>i<TR><TD>n<TR><TD>a<TR><TD></TD><TR>
<TR><TD>Y<TR><TD> <TR><TD>Z<TR><TD>h<TR><TD>a<TR><TD>n<TR><TD>g<TR><TD></TD><TD><TR><TD>V<TR><TD>i<TR><TD>c<TR><TD>t<TR><TD>o<TR><TD>r<TR><TD>y<TR><TD> <TR><TD>U<TR><TD>n<TR><TD>i<TR><TD>v<TR><TD>e<TR><TD>r<TR><TD>s<TR><TD>i<TR><TD>t<TR><TD>y<TR><TD></TD><TD><TR><TD>M<TR><TD>e<TR><TD>l<TR><TD>b<TR><TD>o<TR><TD>u<TR><TD>r<TR><TD>n<TR><TD>e<TR><TD></TD><TD><TR><TD>A<TR><TD>u<TR><TD>s<TR><TD>t<TR><TD>r<TR><TD>a<TR><TD>l<TR><TD>i<TR><TD>a<TR><TD></TD><TR>
      </TABLE>
   </BODY>
</HTML>

7.例子17-2的解析

#!/bin/bash
end=$1
cat $2
#tr -cs "[a-z][A-Z]" "[\012*]" |
#   tr A-Z a-z |
#       sort |
#          uniq -c |
#             sort -k1nr -k2 |
#                 head -n "$end"
运行./topn.sh 5 poem.txt
The Yangs: No silk thread can string these pearls;
           Dim now the tear-stains of those bygone years;
           A thousand bamboos grow before my window---
           Is each dappled and stained with tears?
Hawkes:    Yet silk preserves but ill the Naiad's tears:
           Each salty trace of them fast disapperars.
           Only the speckled bamboo stems that grow
           Outside the window still her tear-marks show.
==========================================

把非字母的部分变成回车

cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]"
#   tr A-Z a-z |
#       sort |
#          uniq -c |
#             sort -k1nr -k2 |
#                 head -n "$end"
运行./topn.sh 5 poem.txt

The
Yangs
No
silk
thread
can
string
these
pearls
Dim
now
the
tear
stains
of
those
bygone
years
A
thousand
bamboos
grow
before
my
window
Is
each
dappled
and
stained
with
tears
Hawkes
Yet
silk
preserves
but
ill
the
Naiad
s
tears
Each
salty
trace
of
them
fast
disapperars
Only
the
speckled
bamboo
stems
that
grow
Outside
the
window
still
her
tear
marks
show
=============================

把大写改成小写

#!/bin/bash
end=$1
cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]" |
   tr A-Z a-z
#       sort |
#          uniq -c |
#             sort -k1nr -k2 |
#                 head -n "$end"
运行./topn.sh 5 poem.txt

the
yangs
no
silk
thread
can
string
these
pearls
dim
now
the
tear
stains
of
those
bygone
years
a
thousand
bamboos
grow
before
my
window
is
each
dappled
and
stained
with
tears
hawkes
yet
silk
preserves
but
ill
the
naiad
s
tears
each
salty
trace
of
them
fast
disapperars
only
the
speckled
bamboo
stems
that
grow
outside
the
window
still
her
tear
marks
show
=========================
进行排序

#!/bin/bash
end=$1
cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]" |
   tr A-Z a-z |
       sort
#          uniq -c |
#             sort -k1nr -k2 |
#                 head -n "$end"
运行:

a
and
bamboo
bamboos
before
but
bygone
can
dappled
dim
disapperars
each
each
fast
grow
grow
hawkes
her
ill
is
marks
my
naiad
no
now
of
of
only
outside
pearls
preserves
s
salty
show
silk
silk
speckled
stained
stains
stems
still
string
tear
tear
tears
tears
that
the
the
the
the
the
them
these
those
thousand
thread
trace
window
window
with
yangs
years
yet

=============================

计数

#!/bin/bash
end=$1
cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]" |
   tr A-Z a-z |
       sort   |
          uniq -c
#             sort -k1nr -k2 |
#                 head -n "$end"

运行结果:

      1 a
      1 and
      1 bamboo
      1 bamboos
      1 before
      1 but
      1 bygone
      1 can
      1 dappled
      1 dim
      1 disapperars
      2 each
      1 fast
      2 grow
      1 hawkes
      1 her
      1 ill
      1 is
      1 marks
      1 my
      1 naiad
      1 no
      1 now
      2 of
      1 only
      1 outside
      1 pearls
      1 preserves
      1 s
      1 salty
      1 show
      2 silk
      1 speckled
      1 stained
      1 stains
      1 stems
      1 still
      1 string
      2 tear
      2 tears
      1 that
      5 the
      1 them
      1 these
      1 those
      1 thousand
      1 thread
      1 trace
      2 window
      1 with
      1 yangs
      1 years
      1 yet
=========================

按照第一列排序:

#!/bin/bash
end=$1
cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]" |
   tr A-Z a-z |
       sort   |
          uniq -c |
             sort -k1nr -k2  //按照第一列数字(n)逆向(r)进行排序,按照第二列进行排序
#                 head -n "$end"
输出:

      5 the
      2 each
      2 grow
      2 of
      2 silk
      2 tear
      2 tears
      2 window
      1 a
      1 and
      1 bamboo
      1 bamboos
      1 before
      1 but
      1 bygone
      1 can
      1 dappled
      1 dim
      1 disapperars
      1 fast
      1 hawkes
      1 her
      1 ill
      1 is
      1 marks
      1 my
      1 naiad
      1 no
      1 now
      1 only
      1 outside
      1 pearls
      1 preserves
      1 s
      1 salty
      1 show
      1 speckled
      1 stained
      1 stains
      1 stems
      1 still
      1 string
      1 that
      1 them
      1 these
      1 those
      1 thousand
      1 thread
      1 trace
      1 with
      1 yangs
      1 years
      1 yet
====================================

列出前面几个:

#!/bin/bash
end=$1
cat $2 |
tr -cs "[a-z][A-Z]" "[\012*]" |
   tr A-Z a-z |
       sort   |
          uniq -c |
             sort -k1nr -k2 |
                 head -n "$end"

运行结果:

      5 the
      2 each
      2 grow
      2 of
      2 silk

*************************************************************

在例子17-5中最后缺少了一行:

#!/bin/bash
PIPS=6
MAX=1000
throw=1
one=0
two=0
three=0
four=0
five=0
six=0
count()
{
case "$1" in
0) let "one=one+1";;
1) let "two=two+1";;
2) let "three=three+1";;
3) let "four=four+1";;
4) let "five=five+1";;
5) let "six=six+1";;
esac
}
while [ "$throw" -le "$MAX" ]
do
  let "dice=RANDOM % $PIPS"
  count $dice
  let "throw=throw+1"
done
echo "The statistics results are as follows:"
echo "one=$one"
echo "two=$two"
echo "three=$three"
echo "four=$four"
echo "five=$five"
echo "six=$six"


0 0