Shell脚本未运行文本文件的输入

时间:2017-10-25 07:53:00

标签: bash shell hadoop

我正在使用mapper shell脚本解决问题。我创建了两个文件

mr_test.txt

我们将看到map reduce如何解决字数问题。字数被认为是地图减少编程的hello世界。这个程序不会打印hello world而是会给出字数。让我们看看map reduce in action。

和另一个名为

的shell脚本

mapper.sh

while read line; do
  for token in $line; do
    if ["$token" = "hello" ]; then 
      echo "hello,1"
    elif ["$token" = "world"]; then 
      echo "world,1"
    fi
  done
done

我正在这里使用shell脚本打印我的txt文档中的所有hello和world。 现在我输入以下命令

chmod +x mapper.sh

后面跟着

cat mr_test.txt | ./mapper.sh

我得到输出为

./mapper.sh: line 5: [hello: command not found
./mapper.sh: line 8: [hello: command not found
./mapper.sh: line 5: $'[everyone,\r': command not found
./mapper.sh: line 8: $'[everyone,\r': command not found
./mapper.sh: line 5: [we: command not found
./mapper.sh: line 8: [we: command not found
./mapper.sh: line 5: [will: command not found
./mapper.sh: line 8: [will: command not found
./mapper.sh: line 5: [see: command not found
./mapper.sh: line 8: [see: command not found
./mapper.sh: line 5: [how: command not found
./mapper.sh: line 8: [how: command not found
./mapper.sh: line 5: [map: command not found
./mapper.sh: line 8: [map: command not found
./mapper.sh: line 5: [reduce: command not found
./mapper.sh: line 8: [reduce: command not found
./mapper.sh: line 5: [works: command not found
./mapper.sh: line 8: [works: command not found
./mapper.sh: line 5: [for: command not found
./mapper.sh: line 8: [for: command not found
./mapper.sh: line 5: [word: command not found
./mapper.sh: line 8: [word: command not found
./mapper.sh: line 5: [count: command not found
./mapper.sh: line 8: [count: command not found
./mapper.sh: line 5: [problem.: command not found
./mapper.sh: line 8: [problem.: command not found
./mapper.sh: line 5: [word: command not found
./mapper.sh: line 8: [word: command not found
./mapper.sh: line 5: [count: command not found
./mapper.sh: line 8: [count: command not found
./mapper.sh: line 5: [is: command not found
./mapper.sh: line 8: [is: command not found
./mapper.sh: line 5: [considered: command not found
./mapper.sh: line 8: [considered: command not found
./mapper.sh: line 5: [as: command not found
./mapper.sh: line 8: [as: command not found
./mapper.sh: line 5: [hello: command not found
./mapper.sh: line 8: [hello: command not found
./mapper.sh: line 5: [world: command not found
./mapper.sh: line 8: [world: command not found
./mapper.sh: line 5: [of: command not found
./mapper.sh: line 8: [of: command not found
./mapper.sh: line 5: [map: command not found
./mapper.sh: line 8: [map: command not found
./mapper.sh: line 5: [reduce: command not found
./mapper.sh: line 8: [reduce: command not found
./mapper.sh: line 5: [programming.: command not found
./mapper.sh: line 8: [programming.: command not found
./mapper.sh: line 5: [this: command not found
./mapper.sh: line 8: [this: command not found
./mapper.sh: line 5: [program: command not found
./mapper.sh: line 8: [program: command not found
./mapper.sh: line 5: [will: command not found
./mapper.sh: line 8: [will: command not found
./mapper.sh: line 5: [not: command not found
./mapper.sh: line 8: [not: command not found
./mapper.sh: line 5: [print: command not found
./mapper.sh: line 8: [print: command not found
./mapper.sh: line 5: [hello: command not found
./mapper.sh: line 8: [hello: command not found
./mapper.sh: line 5: [world: command not found
./mapper.sh: line 8: [world: command not found
./mapper.sh: line 5: [instead: command not found
./mapper.sh: line 8: [instead: command not found
./mapper.sh: line 5: [it: command not found
./mapper.sh: line 8: [it: command not found
./mapper.sh: line 5: [will: command not found
./mapper.sh: line 8: [will: command not found
./mapper.sh: line 5: [give: command not found
./mapper.sh: line 8: [give: command not found
./mapper.sh: line 5: [word: command not found
./mapper.sh: line 8: [word: command not found
./mapper.sh: line 5: [counts.: command not found
./mapper.sh: line 8: [counts.: command not found
./mapper.sh: line 5: [lets: command not found
./mapper.sh: line 8: [lets: command not found
./mapper.sh: line 5: [see: command not found
./mapper.sh: line 8: [see: command not found
./mapper.sh: line 5: [map: command not found
./mapper.sh: line 8: [map: command not found
./mapper.sh: line 5: [reduce: command not found
./mapper.sh: line 8: [reduce: command not found
./mapper.sh: line 5: [in: command not found
./mapper.sh: line 8: [in: command not found
./mapper.sh: line 5: $'[action.\r': command not found
./mapper.sh: line 8: $'[action.\r': command not found

我已经检查了许多地方的语法不正确,但无法缩小是什么问题。

3 个答案:

答案 0 :(得分:0)

以这种方式避免“读取行”使用中出现空格问题:

#!/bin/bash

FILE=$1
CNT=0
while read line; do
    if [[ $line == "hello world" ]]; then
        CNT=$(( $CNT + 1 ))
    fi  
done < $1

echo "File '$1' has $CNT times the 'hello world' phrase"

要运行脚本,请将wordfile作为参数传递,如下所示:    
./mapper.sh mr_test.txt

答案 1 :(得分:0)

WebDriverWait Constructor不是shell语法的一部分,而是一个命令。问题更明显的是你拼写它[。 (命令test和命令test之间的唯一区别是[要求其最后一个参数为[。)

]在语义上与if [ "$token" = "hello" ]相同。显而易见的是,if test "$token" = hello是一个错误,应该明确为什么if test"$token" = hello是错误。

请注意,引用规则很重要,也许需要明确指出if ["$token" = hello ]if test"$token"完全相同。

对于这个特定的脚本,最好使用case语句:

if "test$token"

答案 2 :(得分:-1)

这对你有用。 Shell对空间很敏感 - 特别是在“[”周围,它不被视为方括号而是条件检查。

while read line;
do
        for token in $line
        do
                if [ $token = "hello" ]; then
                        echo "Hello,1"
                elif [ $token = "world" ]; then
                        echo "world,1"
                fi
        done
done