I'm fairly new to Linux and I'm working my way though a course of learning. I've been working on a script to create a new group, which checks for duplicates and then also does the same for a new user and configures some parameters for the user. I've gotten so far, but I suspect, I'm either over-complicating it, or just too much of a noob.
Here's my script:
#!/bin/bash
# Challenge Lab B: Bash Scripting
# Script settings in OS:
#  1. set user owner to root
#  2. set group owner to root
#  3. set permission to include setuid (4755)
#  4. run script with sudo
echo -n 'Enter new group: '
read group_name
echo 'checking for duplicates...'
while [ grep -q "$group_name" /etc/group == 0 ]
do
  echo 'group already exists; try another.'
  if [ grep -q "$group_name" /etc/group == 1 ];
    then
    break
  fi
done
    
echo 'group does not exist.';
echo 'creating group.';
groupadd $group_name;
echo "$group_name group created";
echo -n 'Enter new user: '
read user_name
while [ grep -q "$user_name" /etc/passwd == 0 ]
do
  echo 'user already exists; try another.'
  if [ grep -q "$user_name" /etc/passwd == 1 ];
  then 
  break
  fi
done
echo 'user does not exist.';
echo 'creating user directory.';
mkdir /$user_name;
echo 'creating user.';
useradd -s /bin/bash -g $group_name $user_name;
echo "changing home directory user and group ownership to $user_name";  
chown $user_name:$group_name /$user_name;
echo 'changing user and group mode to RWX and setting sticky bit';
chmod 1770 /$user_name
echo "Process complete"
And here's the result:
Enter new group: test1                                                          
checking for duplicates...                                                      
./test_sc: line 25: [: too many arguments                                       
group does not exist.                                                           
creating group.                                                                 
test1 group created                                                             
Enter new user: user1                                                           
./test_sc: line 57: [: too many arguments                                       
user does not exist.                                                            
creating user directory.                                                        
creating user.                                                                  
changing home directory user and group ownership to user1                       
changing user and group mode to RWX and setting sticky bit                      
Process complete
Clearly, it kind of works, but I'm sure the low-level handling of the duplicate isn't working based upon the result.
I'm sure many will look upon my work as terrible, but this is my first one, so please bear that in mind.
Cheers, S
 
     
    