Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux script help. Can someone please help fix this code. I am trying to add new

ID: 3764861 • Letter: L

Question

Linux script help. Can someone please help fix this code. I am trying to add new users from a text file. When i run it, all i get for the output is repeated lines of

adding:

adding:

adding:

adding:

but the output should be

adding Jet Black: jetb, and a random password

adding Faye Valentine: fayev, and a random password

adding Radical Edward: radicale, and a random password

HERE IS THE CODE

#!/bin/bash

if [ $# -ne 0 ];then

echo "usage: $# < file"

exit 1

fi

while read line; do

first= `echo $line | cut -d ',' -f1`

last= `echo $line | cut -d ',' -f1`

lastl= `echo $last | cut -b1`

user= `echo "$first$lastl " | tr 'A-Z' 'a-z'`

passwd= `cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c 8`

echo $passwd > /tmp/pw.tmp

echo "adding $first $last: $user, $passwd"

/usr/sbin/groupadd $user

/usr/sbin/useradd -c "$first $last" -d /home/$user -g $user -m -s /bin/bash $user

cat /tmp/pw.tmp | /usr/bin/passwd --stdin $user > /dev/null

done > user_list

Explanation / Answer

I have modified the script for you.

The user add command works depending on the kernel you have. Ubuntu, Red hat will have different versions of user add command,

The below script should work.

#!/bin/bash

if [ $# -ne 0 ]
then
echo "usage: $0 < file"
exit 1
fi

while read user_list
do
first=`echo $user_list | cut -f 1 -d ','`
last=`echo $user_list | cut -f 2 -d ','`
lastl=`echo $last | head -c 1`

usern=`echo $first $lastl | tr 'A-Z' 'a-z'`
tname=`echo $first $last`
       passwd= `cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c 8`
       echo $passwd > /tmp/pw.tmp
       echo "adding $first $last: $user, $passwd"
       /usr/sbin/useradd -p `openssl passwd -1 $passwd` $usern
echo "adding $tname : $usern "
done < user_text

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote