2014-10-11 44 views
1
if [ -f users.txt ]; 
    a=$(cat users.txt | grep "$email" | cut -d ',' -f1) 
    then 
    if [ $a -eq $email ]; 
     then 
     echo " Your email is already registed" 
     ./new_user.sh 
    fi 
fi 

我有一个叫users.txt包含所有用户,其中电子邮件是在第一列的列表文件,我想验证邮件是否已经存在.. 。 有人能帮我吗 ?验证邮件是否已经存在 - Shell脚本

这是第一次,我创建一个用户,该文件users.txt不存在,这就是为什么我做if [ -f users.txt ];

+0

那么你得到的输出是什么? – nu11p01n73R 2014-10-11 11:30:07

+0

./new_user.sh:line15:[:(repeed email):expected expression expected //// line 15 is“if [$ a -eq $ email];” – jfbribeiro 2014-10-11 11:53:35

+0

请不要评论您的编辑内容有问题。应该始终在主线程中完成。谢谢 – nu11p01n73R 2014-10-11 11:56:25

回答

1

if ISS错误的语法。正确的语法是

if [ condition ] 
then 
    body 
fi 

so a=$(cat users.txt | grep "$email" | cut -d ',' -f1)不能在你写的地方。

现在如果你想检查$emailusers.txt的存在grep只是必需的。第二个如果可以重写

if [ -f users.txt ]; 
    grep -q "$email" users.txt 
    if (($? == 0)) 
    then 
     echo " Your email is already registed" 
     ./new_user.sh 
    fi 

fi 

它做了什么?

grep -q "$email" users.txt匹配$emailusers.txt文件-q是安静的,所以匹配的行不会打印。

$?是上一个命令的退出状态,这里的grep将在成功完成时具有值0,即当存在匹配时。

相关问题