#!/bin/sh
exist=`grep $1 /etc/passwd`
if [ -n $exist ];then
   echo "$1 already exist"
   exit 0
fi
if [ `whoami` = "root" ];then
   useradd $1
else
   echo "Must be roo"
   exit 1
fi

解决方案 »

  1.   

    exist=`cat /etc/passwd | awk -F ":" '{print $1}' |grep $1`
      

  2.   

    对不起,我是指在Mysql中的脚本
      

  3.   

    #!/bin/sh
    exist=`mysql -uUSER -pPASS -Dyour_db -e "select user from your_table where user='$1'"`
    if [ -n $exist ];then
       echo "$1 already exist"
       exit 0
    else
      mysql -uUSER -pPASS -Dyour_db -e "insert into ....." 
    fi