2014-03-27 48 views
1

我不断收到一个运行时错误,说[:太多的参数。获取[:太多的参数

这里是我的代码

变量

mem_list=/root/Desktop/Dan/List/list.txt 
word=[user inputs some word] 

代码:

if [ -f "$mem_list" -a grep "$word" "$mem_list" ] 
then 
    echo "word already exists in list" 
else 
    echo "word does not exist yet" 
fi 

也能收到[:参数太多!

请帮助我!

回答

4

要运行命令,请使用cmd。不是[ cmd ]

/bin/[就像grep只是一个命令,if将检查退出状态:

if [ -f "$mem_list" ] && grep -q "$word" "$mem_list" 
then 
    echo "word already exists in list" 
else 
    echo "word does not exist yet" 
fi