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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -