开发者

simple use of function in shell script

I am trying to use the simple fuinction below. But i get error sayin unary oprator expected and the output is always one. Can any1 help me correct it.

#!/bin/bash
checkit ()
{
if [ $1 = "none" ]
then
开发者_运维技巧     echo "none"
else
     echo "one"
fi
}
checkit


$1 is an argument to the entire script and not to the function checkit(). So send the same argument to the function too.

#!/bin/bash
checkit ()
{
if [ $1 = "none" ]
then
     echo "none"
else
     echo "one"
fi
}

checkit $1

This has to work.


Use the [[ keyword instead of the [ builtin:

#!/bin/bash
checkit ()
{
if [[ $1 = none ]]
then
     echo none
else
     echo one
fi
}
checkit

Please read the Bash pitfalls and the Bash FAQ.


Surround $1 with quotes like this:

#!/bin/bash
checkit ()
{
if [ "$1" = "none" ]
then
     echo "none"
else
     echo "one"
fi
}
checkit
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜