SHELL 練習 有沒人可以講解下點做?

Task 4-1
You have a large album collection, and you want to write some software to keep track of it. Assume that you have a file of data on how many albums you have by each artist. Lines in the file look like this:

5        Depeche Mode

2        Split Enz

3        Simple Minds

1        Vivaldi, Antonio


Write a program that prints the N highest lines, i.e., the N artists by whom you have the most albums. The default for N should be 10. The program should take one argument for the name of the input file and an optional second argument for how many lines to print.

[ 本帖最後由 愚生 於 2009-3-29 21:18 編輯 ]

sort | head

TOP

  1. #!/bin/bash

  2. if [ $# -eq 1 ];then
  3.    file=$1
  4.    line=10
  5. elif [ $# -eq 2 ];then
  6.    file=$1
  7.    line=$2
  8. else
  9.    echo "Usage:"$0" albumdata numoflines"
  10.    exit 127
  11. fi

  12. if [ ! -f $file ];then
  13.   exit 126
  14. fi

  15. sort -r $file | head -n $line

  16. exit 0
複製代碼

TOP