shell脚本没有使用文本文件的输入运行

ejk8hzay  于 2021-06-02  发布在  Hadoop
关注(0)|答案(3)|浏览(393)

我正在用mappershell脚本解决一些问题。我已经创建了两个文件

mr_test.txt

我们将看到map reduce如何处理单词计数问题。单词计数被认为是Map缩减编程的hello世界。这个程序不会打印helloworld,而是会计算字数。让我们看看Map减少行动。
另一个shell脚本
Map器.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

我期待在这里打印所有的hello和世界在我的txt文件使用shell脚本。现在我输入以下命令

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

我已经检查了不正确的语法很多地方,但不能缩小到什么是错误的地方。

piv4azn7

piv4azn71#

[ 不是shell语法的一部分,而是命令。更明显的问题是你拼出来了 test . (命令之间的唯一区别 test 还有命令 [ 是吗 [ 要求它的最后一个参数 ] ). if [ "$token" = "hello" ] 在语义上与 if test "$token" = hello . 很明显 if test"$token" = hello 是一个错误,应该弄清楚原因 if ["$token" = hello ] 是个错误。
请注意,引用规则很重要,也许需要明确指出 if test"$token" 被完全等同于 if "test$token" .
对于这个特定脚本,最好使用case语句:

while read line; do
  for token in $line; do
    case $token in
    hello) echo hello,1;;
    world) echo world,1;;
    esac
  done
done
8xiog9wr

8xiog9wr2#

这应该对你有用。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
ffdz8vbo

ffdz8vbo3#

为避免“read line”之间出现空格问题,请使用以下方法:


# !/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\u test.txt文件

相关问题