我写了一个要在文本文件上运行的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
我的测试.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
这里有什么问题?除非我必须这样做,(因为我们被严格告知不要)我宁愿不发布我的其余代码。
空格
很重要。 取代:
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