shell脚本程序(二)(用户的建立删除,自动应答ssh连接,连接后保存主机名,论坛部署和数据库的备份)

本文介绍了如何使用Shell脚本来管理用户,包括创建和删除用户。接着展示了实现自动SSH连接的脚本,以及在连接后执行命令的技巧。此外,还讲解了论坛自动部署的两种方法,并详细阐述了数据库备份的过程,包括用户交互式的备份选项处理。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值