1.编写一个脚本,当用户存在时输出用户已存在,当用户不存在时创建用户
#!/bin/bash
for i in "$@"
do
id $i &> /dev/null &&{
echo "$i is exist"
}||{
useradd $i &> /dev/null
echo "$i is created"
}
done
测试:
[root@shell_example mnt]# vim user.sh
[root@shell_example mnt]# sh user.sh hello
hello is exist
[root@shell_example mnt]# sh user.sh haha
haha is created
[root@shell_example mnt]# sh user.sh haha
haha is exist
2.编写一个脚本,有三个选项,C表示create 创建用户,当用户存在时,输出用户已存在,用户不存在时创建用户
D表示delete 删除用户
E表示exit 退出
(while循环实现)
#!/bin/bash
while true
do
read -p "[C]reate [D]elete [E]xit
please input action core: " Action
Action=`echo $Action|tr 'A-Z' 'a-z'`
[ "$Action" != "c" -a "$Action" != "d" -a "$Action" != "e" ] && {
echo "please check your action"
}
while [ "$Action" = "c" ]
do
read -p "please input username: " Username
id $Username &> /dev/null && {
echo "$Username is exist!"
}||{
`useradd $Username`
read -p "Please input password: " Password
echo $Password | passwd --stdin $Username &>/dev/null &&{
echo "$Username is created"
}
}
break
done
while [ "$Action" = "d" ]
do
read -p "please input username: " Username
`userdel -r $Username` && {
echo "$Username is deleted"
}||{
echo "please check username"
}
break
do