Variable in Command in bash script -
hello want automate setting users on servers. started simple bash
#! /bin/bash if [ $# -ne 2 ] 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 problem last line. user created not assigned group www-data
. when use last line , comment everthing else , feed 1 user script able add myself, can explain me why faling?
exec useradd -m $user_name
substitutes current process ie bash
here useradd -m $user_name
.
don't see practical advantage of using exec
here.
also, linux password can have whitespaces, suggest doing
password_var="$2" #prevents word splitting
with error checking, final script be
password_var="$2" useradd -mp "$password_var" "$user_name" # haven't used password if [ $? -ne 0 ] # checking exit status of last command echo "user creation failed" exit 1 else usermod -s /bin/bash "$user_name" #username missing in original code usermod -ag www-data "$user_name" fi
Comments
Post a Comment