Linux 用于添加和删除用户的bash脚本
我是bash脚本的初学者,我创建了一个bash脚本,用于在Linux上添加用户和删除用户。但是,由于我在脚本方面面临一些问题,这并不是什么大问题,但是如果有人能告诉我如何改进脚本,我会很有帮助,而我做脚本的最糟糕的做法也会很有帮助 然而,我注意到的问题是,脚本需要-a来添加用户-d来删除用户,-h来获取帮助-a标志作为两个可选参数-p表示密码,-s表示shell,因此命令将是Linux 用于添加和删除用户的bash脚本,linux,bash,shell,server,sh,Linux,Bash,Shell,Server,Sh,我是bash脚本的初学者,我创建了一个bash脚本,用于在Linux上添加用户和删除用户。但是,由于我在脚本方面面临一些问题,这并不是什么大问题,但是如果有人能告诉我如何改进脚本,我会很有帮助,而我做脚本的最糟糕的做法也会很有帮助 然而,我注意到的问题是,脚本需要-a来添加用户-d来删除用户,-h来获取帮助-a标志作为两个可选参数-p表示密码,-s表示shell,因此命令将是 ./useradd.sh -a user -p password -s shell 这正如预期的那样工作,用户被添加到
./useradd.sh -a user -p password -s shell
这正如预期的那样工作,用户被添加到系统中,但我面临的问题是,如果我没有输入-a标志并指定-s和-p标志,那么脚本刚刚退出,我想向用户展示一个清晰的想法,它退出的原因,我假设有这么多这样的错误,但我还没有测试出这么多,任何帮助都将不胜感激,这是我的剧本
#!/bin/bash
## checking if the user is privileged or not
if [[ $EUID != 0 ]]
then
echo "Script has to be ran as root or sudo"
echo "Aborting"
exit 101
fi
## creating help functions
function usage() {
echo "usage: ${0} -a <user> -p <password> -s <shell> | ${0} -d <user> | ${0} -h"
}
function help() {
echo "$0 - Script to add of remove users"
echo "-a - Add a new user"
echo " -p - Set password while creating user if not mentioned will not set any password by default"
echo " -s - Set a shell for the user default is /bin/bash if none specified"
echo "-a - Remove a user"
echo "-h - Print this help text"
}
if [[ "$#" -lt "1" ]]; then
echo "Argument has to be provided see $0 -h"
fi
shell=/bin/bash
password=$(openssl rand -base64 32)
while getopts :a:d:h opt; do
case $opt in
a) user=$OPTARG
while getopts :p:s: test
do
case $test in
p) password=$OPTARG;;
s) shell=$OPTARG;;
/?) echo "The provided flag is not identified see $0 -h"
exit;;
:) echo "$OPTARG requires arguments see $0 -h"
exit;;
esac
done
if [[ "$1" != "-a" ]]
then
echo "You have to specify username using -a flag see $0 -h"
fi
useradd -m $user -s $shell
echo "$user":"$password" | chpasswd
echo "The password for the $user is $password";;
d) userdel -f $OPTARG
if [[ $? == 0 ]]
then
echo "user has been removed"
else
echo "There was some error removing the user"
fi;;
h) help
exit;;
/?) echo "$OPTARG option not valid";;
:) echo "$OPTARG requires argument";;
esac
done
#/bin/bash
##检查用户是否具有特权
如果[$EUID!=0]]
然后
echo“脚本必须作为root或sudo运行”
回声“中止”
101出口
fi
##创建帮助功能
函数用法(){
echo“用法:${0}-a-p-s |${0}-d |${0}-h”
}
函数帮助(){
echo“$0-添加或删除用户的脚本”
echo“-a-添加新用户”
echo“-p-创建用户时设置密码如果未提及,默认情况下不会设置任何密码”
echo“-s-为用户设置shell如果未指定,则默认值为/bin/bash”
echo“-a-删除用户”
echo“-h-打印此帮助文本”
}
如果[[“$#””-lt“1”];然后
echo“必须提供参数,请参见$0-h”
fi
shell=/bin/bash
密码=$(openssl rand-base64 32)
而getopts:a:d:h opt;做
案例$opt-in
a) 用户=$OPTARG
而getopts:p:s:test
做
案例$testin
p) 密码=$OPTARG;;
s) 外壳=$OPTARG;;
/?)echo“未识别提供的标志,请参见$0-h”
出口
:)echo“$OPTARG需要参数请参见$0-h”
出口
以撒
完成
如果[[“$1”!=”-a”]]
然后
echo“您必须使用-a标志指定用户名,请参见$0-h”
fi
useradd-m$user-s$shell
回显“$user”:“$password”| chpasswd
回显“$user的密码是$password”;;
d) userdel-f$OPTARG
如果[[$?==0]]
然后
回显“用户已被删除”
其他的
echo“删除用户时出错”
fi;;
h) 帮助
出口
/?)回显“$OPTARG选项无效”;;
:)回显“$OPTARG需要参数”;;
以撒
完成
请出示您的代码!我通常用case处理args。。。例如:
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $1 in
"-a")
echo "-a is $2"
shift 2;;
"-d")
echo "-d is $2"
shift 2;;
esac
done
getopts
的嵌套使用有点奇怪。通常你会有一个单一的循环,如果选项不可调和,就会出现一个错误