$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)

#!/bin/bash
for i in `seq 1 10`
do
    if id user-$i > /dev/null;then
            read -eq "user-$i用户已经存在,是否删除(y/n)"ss
            if [ $ss=y ];then
                  userdel -rf user-$i
           elif [ $ss =n];then
                 continue
          else
          echo "输入有误,user-$i未删除"
         fi
   else
        useradd user-$i
       if [ $? -eq 0];then
             echo "user-$i 创建成功"
             password="user `cat /dev/urandom | head -1 | md5sum | head -c 5`"
             echo "user-$i:$passwd" chpasswd
             echo "user-$i--$passwd" >> user.txt
       else
            echo "user-$i 创建失败"
      fi
  fi
done

原文地址:https://www.cnblogs.com/70xinshidai/p/11598189.html