判断用户输入的数字是否为质数:
#!/bin/bash
read -p "输入一个数字:" num
if [ $num -eq 1 ];then
echo "$num不是质数"
exit 1
elif [ $num -eq 2 ];then
echo "$num是质数"
exit 2
fi
for i in `seq 2 $[$num-1]`
do
if [ $[$num%$i] -eq 0 ];then
echo "$num不是质数"
exit 3
else
echo "$num是质数"
exit 4
fi
done
批量创建用户:
#!/bin/bash
read -p "请输入创建用户的数量:" num
read -p "请输入创建用户的前缀:" a
read -p "请输入创建用户的密码:" password
for((i=1;i<=$num;i++))
do
user=$a$i
useradd $user
echo "$password" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created"
fi
done
判断用户名是否存在:
#! /bin/env bash
read -p "请输入要查找的用户名:" name
id $name &>/dev/null
if [ $? -eq 0 ]
then
echo "你输入的用户名为:$name,存在,id是:$a"
else
echo "你输入的用户名为:$name,该用户是不存在的"
fi