Bash 未使用文本文件输入运行Shell脚本
我正在使用MapperShell脚本解决一些问题。我已经创建了两个文件Bash 未使用文本文件输入运行Shell脚本,bash,shell,hadoop,Bash,Shell,Hadoop,我正在使用MapperShell脚本解决一些问题。我已经创建了两个文件 mr_test.txt 我们将看到map reduce如何处理单词计数问题。单词计数被认为是map reduce编程的hello world。该程序不会打印hello world,而是提供字数。让我们看看地图在行动中的减少 还有另一个shell脚本,名为 mapper.sh while read line; do for token in $line; do if ["$token" = "hello" ];
mr_test.txt
我们将看到map reduce如何处理单词计数问题。单词计数被认为是map reduce编程的hello world。该程序不会打印hello world,而是提供字数。让我们看看地图在行动中的减少
还有另一个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
我已经检查了很多地方的错误语法,但无法缩小范围,以了解出了什么问题 这应该适合你。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
为避免“读取行”之间出现空格问题,请使用以下方法:
#!/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"
!/bin/bash
文件=$1
CNT=0
边读边做
如果[[$line==“hello world”];那么
CNT=$($CNT+1))
fi
已完成<$1
echo“文件“$1”的$CNT乘以“hello world”短语”
要运行脚本,请将wordfile作为参数传递,如下所示:
./mapper.sh mr_test.txt
[
不是shell语法的一部分,而是一个命令。更明显的问题是拼写它test
(命令test
和命令[
之间的唯一区别是[
要求它的最后一个参数是]
)
if[“$token”=“hello”]
与if test“$token”=hello
在语义上是相同的。显然,if test“$token”=hello
是一个错误,这应该说明为什么if[“$token”=hello]
是一个错误
请注意,引用规则很重要,可能需要明确指出,如果测试“$token”
的处理方式与如果“测试$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
将脚本放入其中会告诉错误您只读取文本文件,对吗?在这种情况下,只需在文件中键入./mapper.sh
if
语句的语法是if
命令,括号不是条件语法的一部分(与许多其他语言不同)。单个[
实际上是测试
命令(执行类型[
)。正如您所知,在shell语言中,空格必须分隔命令,因此if
和[
命令之间应该有空格,而之间应该有空格[
和传递给它的参数。cat奖励的无用用法?/mapper.sh[
不是一个“条件检查”。它是一个命令。如果拼写为“test”,如果test“$token”=hello;写,就更清楚了;
考虑[
进行更好的测试。