简体   繁体   English

Shell 脚本未在输入文本文件时运行

[英]Shell script not running with the input of a text file

I am solving something using mapper shell script.我正在使用映射器 shell 脚本解决一些问题。 I have created two files我创建了两个文件

mr_test.txt

we will see how map reduce works for word count problem.我们将看到 map reduce 如何解决字数问题。 word count is considered as hello world of map reduce programming.字数被认为是 map reduce 编程的 hello world。 this program will not print hello world instead it will give word counts.该程序不会打印 hello world,而是会提供字数统计。 lets see map reduce in action.让我们看看 map reduce 的效果。

and another shell script called和另一个名为的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

I am looking out here to print all the hello and world present in my txt document using the shell script.我在这里寻找使用 shell 脚本打印我的 txt 文档中存在的所有 hello 和 world。 Now I enter the following commands现在我输入以下命令

chmod +x mapper.sh

later followed by后来紧随其后

cat mr_test.txt | ./mapper.sh

I get the output as我得到的输出为

./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

I have checked a lot of places for incorrect syntax however cannot narrow down as to what is going wrong.我已经检查了很多地方是否有错误的语法,但是无法缩小问题的范围。

To avoid spaces problems among "read line" use while in this way:为了避免“读取行”之间的空格问题,以这种方式使用:

#!/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"

To run the script, pass your wordfile as a parameter, like that:要运行脚本,请将您的 wordfile 作为参数传递,如下所示:
./mapper.sh mr_test.txt ./mapper.sh mr_test.txt

[ is not part of the shell grammar, but is a command. [不是 shell 语法的一部分,而是一个命令。 The problem is more obvious is you spell it test .问题更明显的是你拼写test (The only difference between the command test and the command [ is that [ requires that its last argument be ] ). (命令test和命令[之间的唯一区别是[要求它的最后一个参数是] )。

if [ "$token" = "hello" ] is semantically the same as if test "$token" = hello . if [ "$token" = "hello" ]在语义上与if test "$token" = hello It should be obvious that if test"$token" = hello is an error, which ought to make it clear why if ["$token" = hello ] is an error.很明显if test"$token" = hello是一个错误,这应该说明为什么if ["$token" = hello ]是一个错误。

Note that quoting rules are important, and perhaps it needs to be made explicit that if test"$token" is treated exactly the same as if "test$token" .请注意,引用规则很重要,也许需要明确说明if test"$token"if "test$token"完全相同。

For this particular script, you're better off using a case statement:对于这个特定的脚本,最好使用 case 语句:

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

This should work for you.这应该对你有用。 Shell is touchy about spaces - especially around "[" which is not considered a square bracket but a condition check. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM