shell编程学习小记

  看《鸟哥的linux私房菜》的shell script编程一章,敲if 和 elif,fi的代码时就出了点小问题,卡了很久,终于发现是空格惹的祸。

  

#!/bin/bash
#Date:2012/08/06
#Written by Edward

echo "Press y to continue."
if ["$1"='hello']
	then
	echo "Hello! How are you?"
elif ["$1"='']
	then
	echo "You must input paramters."
else
	echo "The only accept paramter is hello."
fi

   这段代码乍看上去好像没有什么问题,但是执行的时候,就会出现以下问题:

  命令未发现。脚本里if语句后面的“["是会给解释成一个命令的。

  对源码做出一下改动后就可以了。在if [ condition ] 中的 [ 和 condition 之间加入空格。

!/bin/bash
#Date:2012/08/06
#Written by Edward

echo "Press y to continue."
if [ "$1"='hello' ]
	then
	echo "Hello! How are you?"
elif [ "$1"='' ]
	then
	echo "You must input paramters."
else
	echo "The only accept paramter is hello."
fi

 运行如下:

  

posted on 2012-08-06 06:22  edward1992  阅读(177)  评论(0)    收藏  举报

导航