0

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 2
    Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Nov 24 '21 at 19:40
  • @Cyrus, that's brilliant! Thank you. Such a great learning tool, both for syntax and best practice. Big thanks. I don't have 'rep' to up-vote your answer, but it's there in kind. :) – silverdagger Nov 25 '21 at 11:35

2 Answers2

0
  if [ grep -q "$user_name" /etc/passwd == 1 ];

The brackets are not just syntax: [ is a command.

Take note of the if syntax:

$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.
...

The part you put after if is a command. if branches based on the exit status of the command.

You want

  if ! grep -q "$user_name" /etc/passwd; then
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Ahhh, very useful pointer. That's helped. I have new errors now, but that's certainly eliminated the 'too many arguments'. Thank you. – silverdagger Nov 25 '21 at 11:30
0

You should remove square brackets this way if you want to check return value (true or false):

while [ grep -q "$group_name" /etc/group == 0 ]

change to:

while grep -q "$group_name" /etc/group; do

If you want to check that this value is incorrect, add ! before the command

You should do that not only for while loops, but also for if statements

Or if you want to check return value for a specific value, let's say 15:

grep something file

ret=$(echo $?)
if [ $ret == 15 ]; then
    do something;
fi

$? will return the last return value from the command that you have executed in bash

  • 1
    Don't use `ret=$(echo $?)`, just `ret=$?`. Using `$(echo something)` is almost always a mistake, since the `$( )` and the `echo` mostly cancel each other out (except for some parsing that you usually don't want). – Gordon Davisson Nov 24 '21 at 20:49
  • Great, thanks guys. That's certainly moved the problem elsewhere, and good to remember for the future. – silverdagger Nov 25 '21 at 11:31