重定向输入时命令未找到错误

时间:2016-05-20 23:48:42

标签: bash shell unix

我编写了一个shell脚本,用于在文本文件(称为test.txt)上运行。此脚本将读取输入文件,迭代其行,并执行一些操作。

我首先编译我的程序:

chmod +x ./cleanLines.txt

然后运行我的命令:

./cleanLines.txt < ./test.txt > output.txt

然而,我得到的错误是:

./cleanLines.txt: line 7: [[: command not found
./cleanLines.txt: line 7: [[#Some: command not found
./cleanLines.txt: line 7: [[HelloWorld: command not found
./cleanLines.txt: line 7: [[Today: command not found
./cleanLines.txt: line 7: [[Cookie: command not found
./cleanLines.txt: line 7: [[: command not found
./cleanLines.txt: line 7: [[Out: command not found
./cleanLines.txt: line 7: [[: command not found
./cleanLines.txt: line 7: [[: command not found

我的test.txt文件如下:

*blank line*(Won't let me edit it like this here)
#Some note
HelloWorld #note
Today is sunny
Cookie Monster

Out of things to say

和cleanLines.txt如下:

#!/bin/bash

    PATH=${PATH[*]}:.
    #filename: clearLines

    while read line; do
        if [[$line != ""]]; then
                .
                .
                # Doing stuff
                .
                .
        fi
    done < test.txt

注意:第7行是:

if [[$line != ""]]; then

这是什么问题?除非我必须(因为我们被严格告知不要),我不想发布我的其余代码。

1 个答案:

答案 0 :(得分:1)

空间很重要。替换:

if [[$line != ""]]; then

使用:

if [[ $line != "" ]]; then

实施例

注意这些失败:

$ line=1 ; [[$line != "" ]] && echo yes
bash: [[1: command not found
$ line=1 ; [[ $line != ""]] && echo yes
bash: conditional binary operator expected
bash: syntax error near `yes'

但是这个版本的间距正确,成功了:

$ line=1 ; [[ $line != "" ]] && echo yes
yes