[英]Variable in Command in bash script
Hello i want to automate setting up users on my servers. 您好,我想在我的服务器上自动设置用户。 So i started with this simple bash
所以我从这个简单的bash开始
#! /bin/bash
if [ $# -ne 2 ]
then
echo "Usage: $(basename $0) USERNAME PASSWORD"
exit 1
fi
user_name=$1
password_var=$2
exec useradd -m $user_name
usermod -s /bin/bash
#echo "$2" | exec chpasswd $user_name --stdin
usermod -aG www-data "${user_name}"
I have a problem with the last line. 我对最后一行有疑问。 The user i just created does not get assigned to the group
www-data
. 我刚刚创建的用户未分配给组
www-data
。 When i use only the last line and comment everthing else and feed my one user into the script i am able to add myself, can someone explain me why this is faling? 当我仅使用最后一行并注释其他内容并将我的一个用户喂入脚本时,我便可以添加自己,有人可以解释一下为什么这会失败吗?
exec useradd -m $user_name
substitutes the current process ie bash
here with useradd -m $user_name
. 用
useradd -m $user_name
替换当前进程,即bash
。
Moreover I don't see any practical advantage of using exec
here. 此外,我在这里看不到使用
exec
任何实际优势。
Also, as the Linux password can have whitespaces, I suggest doing 另外,由于Linux密码可以包含空格,因此我建议您这样做
password_var="$2" #prevents word splitting
With some error checking, my final script would be 经过一些错误检查,我的最终脚本是
password_var="$2"
useradd -mp "$password_var" "$user_name" # You haven't used password
if [ $? -ne 0 ] # checking the exit status of the last command
then
echo "User creation failed"
exit 1
else
usermod -s /bin/bash "$user_name" #username missing in the original code
usermod -aG www-data "$user_name"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.