刚刚速成了一下shell语言,在试着用它写个小脚本的时候发现,这种编程语言的if语句用起来讲究还蛮多的,比如下面这段程序,就有很多错误:

#!/bin/bash
while read line
do
	if [$line != "abc"]; then
		echo $line;
	fi
done < "input.txt"

运行的时候会报unexpected operator,xxx not found, too many arguments等各种错误。
原因是if后面的[]中前后都要有一个空格,并且$line这个变量要房子引号中。
正确的写法如下:

#!/bin/bash
while read line
do
	if [ "$line" != "abc" ]; then
		echo $line;
	fi
done < "input.txt"
如转载,请以超链接形式注明:转载自:有个博客 [ http://www.yelinsky.com/blog/ ]




Tags: ,