• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

Using usermod

#/bin/bash
cat $1 | while read un gn pw
do
cat /etc/group | egrep $gn
if [ $? = 0 ]; then
echo "group $gn already exists"
else
groupadd $gn
fi
pass=$(perl -e 'print crypt($ARGV[0], "password")' $pw)
cat /etc/passwd | egrep $un
if [ $? == 0 ];then
echo "user $un already exists"
else
useradd -p $pass -g $gn $un
if [ $? == 0 ];then
echo "user $un added"
fi
fi
done


I have about 20user accounts in user.txt file which contains name, groupname, password. However how can i apply usermod command to
Above code to modify 20user accounts name in one shot. For example if i do
./usermod.sh user.txt it will modify all 20 user accounts name in once.
0
bt1942
Asked:
bt1942
1 Solution
 
arnoldCommented:
Your tests for the exit code should use
[ $? -eq 0 ]
rater than [ $? = 0 ] or [ $? == 0 ] both will always return true First is an assignment while

#/bin/bash
cat $1 | while read un gn pw
do
cat /etc/group | egrep $gn
if [ $? -eq 0 ]; then
echo "group $gn already exists"
else
echo groupadd $gn
fi
pass=$(perl -e 'print crypt($ARGV[0], "password")' $pw)
cat /etc/passwd | egrep $un
if [ $? -eq 0 ];then
echo "user $un already exists"
else
echo useradd -m -k /etc/skel -p $pass -g $gn $un
if [ $? -eq 0 ];then
echo "user $un added"
fi
fi
done

Open in new window


This should work.

Look at the output.  When the output matches what you expect, remove the echo from the lines that have usermod and useradd
Note your useradd does not include any create home directory instructions
-m -k /etc/skel as modified above
-s /bin/bash

double check that your defaults are set  useradd -D and set to the correct parameters
useradd -D -g <default group> -d /home -s /bin/bash -k /etc/skel -f  <days after password expires and account becomes inactive/disabled (-1 if unneeded)> etc/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now