编写脚本以排序单词

时间:2016-05-04 07:30:40

标签: bash sorting

我正在处理根据给定的参数在Bash中排序单词。我得到了论据-r-a-v-h,根据它可以选择对单词进行排序,正如您在“帮助”中看到的那样。

不知何故,如果我传递参数-r,它会产生错误。我真的不明白我做错了什么,因为if[["$arg"=="-a"]]有效,但我必须以某种方式使用案例。

这是我的代码:

#!/bin/bash
# Natalie Zubkova , zubkonat
# zubkonat@cvut.fel.cz , LS

#help
help="This script will calculate occurances of words in a given file, and it will sort them according to the given argument in following order> \n
without parametre = increasing order according to a number of occurance\n
-r    =  decreasing order according to a number of occurance\n
-a    =  in alphabetical increasing order\n
-a -r =  in alphabetical decreasing order\n
There are also special cases of the given parametre, when the script is not sorting but:\n

-h    = for obtaining help  \n
-v    = for obtaining a number of this task "

# this function will divide a given chain into a words, so we can start calculating the occurances, we also convert all the capital letters to the small ones by - tr

a=0;
r=0;
EXT=0;
if [ "$1" == "-h" ]; then
    echo $help
    exit 0
fi
if [ "$2" == "-h" ]; then
    echo $help
    exit 0
fi
if [ "$1" == "-v" ]; then
    echo "5"
    exit 0
fi
if [ "$2" == "-v" ]; then
    echo "5"
    exit 0
fi

function swap {
    while read x y; do
         echo "$y" "$x";
    done
}

function clearAll {
    sed -e 's/[^a-z]/\n/gI' | tr '[A-Z]' '[a-z]' | sort | uniq -c |awk '{i++; if(i!=1) print $2" "$1}' #swap
}

for arg do
case "$arg" in
      "-a")
       a=1
       ;;
    "-r")
       r=1
       ;;
    "-v")
      echo "5"      #number of task is 5
      exit 0        
         ;;
    "-h")
     echo $help
     exit 0
     ;;
    "-?")
     echo "invalid parametre, please display a help using argument h"
     exit 0
     ;;
   esac
done
#Sort according to parametres -a and -r
function sortWords {       
        if [[ a -eq 1 ]]; then
           if [[ r -eq 0 ]]; then
           clearAll | sort -nk1
        fi  
              fi

              if [[ a -eq 1 ]]; then
        if [[ r -eq 1 ]]; then
            clearAll | sort -nk1 -r
        fi
           fi


        if [[ r -eq 1 ]]; then
            if [[ a -eq 0 ]]; then
           clearAll | sort -nk2 -r
        fi
           fi

        if [[ a -eq 0 ]]; then
            if [[ r -eq 0 ]]; then
            clearAll | sort -nk2
        fi 
    fi
}
#code is from Stackoverflow.com
 function cat-all {
    while IFS= read -r file
    do
        if [[ ! -z "$file" ]]; then
            cat "$file"
        fi
    done
 }
#histogram
hist=""
for arg do
    if [[ ! -e "$arg" ]]; then
               EXT=1;
        echo "A FILE DOESNT EXIST" >&2
        continue;
        elif [[ ! -f "$arg" ]]; then
               EXT=1;
        echo "A FILE DOESNT EXIST" >&2
        continue;
       elif [[ ! -r "$arg" ]]; then
                EXT=1;
        echo "A FILE DOESNT EXIST" >&2
       continue;
        fi
done
for arg do
    hist="$hist""$arg""\n"
done

echo -e "$hist" | cat-all | sortWords
exit $EXT;

以下是我们的上传系统进行一些测试,以确定我们的程序是否有效:

Test #6

> b5.sh  -r ./easy.txt

ERROR: script output is wrong:

--- expected output
+++ script stdout
@@ --- line 1 (167 lines) ; +++ no lines @@
-the 89
-steam 46
-a 39
-of 37
-to 35
...

script written 484 lines, while 484 lines are expected

script error output:

A FILE DOESNT EXIST
cat: invalid option -- 'r'
Try `cat --help' for more information.

script exit value: 1

ERROR: Interrupted due to failed test

如果有人能帮助我,我会非常感激。

1 个答案:

答案 0 :(得分:0)

您忘记使用shift移动参数索引位置:

"-r")
   r=1
   shift
   ;;
上面的

shift移动到你的情况下的下一个命令行arg:./easy.txt。 没有它,read -r file将读取-r而不是文件名。