1

I am asked to add some users with all their info (password , id ,group id ...) present in a given file. Whenever I run my bash "gedit" script , it gives me a message that the "useradd command not found". If anyone could help me please , this is my code :

 #!/bin/bash
choice=0
while [ $choice != "1" -a $choice != "2" -a $choice != "3" ]
do 
  echo "What do you want to do"
  echo "1-Add Users"
  echo "2-Remove users"
  echo "3-Update users"
  echo -n "Enter choice : "
  read choice
done

x=0
while [ $x -ne 1 ]
do
  echo -n "Donner le fichier :"
  read fichier
  if test -f $fichier              # check if file ficher exists
  then
    x=1
  else
  echo "File not found"  
  x=0
  fi
done

if [ $choice == "1" ]
  then

 FILE="user"
   USERNAME=$(cut -d ":" -f 1 $FILE)
   PASSWORD=$(cut -d ":" -f 2 $FILE)
   USER_ID=$(cut -d ":" -f 3 $FILE)
   GROUP_ID=$(cut -d ":" -f 4 $FILE)
   USER_INFO=$(cut -d ":" -f 5 $FILE)
   HOME_DIRECTORY=$(cut -d ":" -f 6 $FILE)
   SHELL=$(cut -d ":" -f 7 $FILE)
   MIN=$(cut -d ":" -f 8 $FILE)
   MAX=$(cut -d ":" -f 9 $FILE)
   INACTIVE=$(cut -d ":" -f 10 $FILE)
   WARNING=$(cut -d ":" -f 11 $FILE)
   
   useradd -m -c "${USERNAME}" "${PASSWORD}" "${USER_ID}" "${GROUP_ID}" "${USER_INFO}" "${HOME_DIRECTORY}" "${SHELL}" "${MIN}" "${MAX}" "${INACTIVE}" "${WARNING}"
fi

To call the script i am using chmod u+x project (which is the name of the gedit file)

The displayed error message is :./project: line 43: useradd: command not found

This is the content of the input file "user" :

charbel:password:1001:1001:Charbel Haddad:/home/charbel:/bin/bash:0:30:15:7:y
assil:p@ssw0rd:1002:1002:Assil:/home/assel:/bin/bash:0:30:10:5:n
marwan:p@ssw0rd:1003:1003:Marwan Ghantous:/home/marwan:/bin/bash:0:50:30:7:n
michel:password:1004:1004:Michel:/home/michel:/bin/bash:1:30:10:5:y
Daher Roy
  • 19
  • 6
  • I tried it here and it works well, can you please share the code error full message? – Gabriel Pellegrino Dec 15 '20 at 09:27
  • 2
    Do you have useradd on your system? – Raman Sailopal Dec 15 '20 at 09:31
  • @GabrielPellegrino Enter choice : 1 Donner le fichier :user ./project: line 42: useradd: command not found – Daher Roy Dec 15 '20 at 09:34
  • Not directly related to the error message: Please show the contents of the input file or a few lines of it. Can the input contain empty fields? If it contains more than one line, the `cut` commands would print all values from the selected field/column. Please always [edit] your question to add requested information. – Bodo Dec 15 '20 at 09:36
  • locate useradd or whereis useradd – Raman Sailopal Dec 15 '20 at 09:37
  • Or run the command `useradd` (without arguments) manually and check the error message. It should either print something like "command not found" or complain about missing arguments. – Bodo Dec 15 '20 at 09:40
  • @RamanSailopal this message was displayed : useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz – Daher Roy Dec 15 '20 at 09:51
  • There is a similar question with the same error in the code: https://unix.stackexchange.com/q/624534/330217 – Bodo Dec 15 '20 at 09:59

1 Answers1

4

Your script would attempt to extract all the user names into one variable, all the home directories into one variable, etc. You want to loop over the input file and handle one user name and all the other fields from that line at a time.

...
if [ $choice = "1" ]   # notice also single = not ==
then
  while IFS=":" read -r username password user_id group_id \
              user_info home_dir shell min max inactive warning
  do
      useradd -m -c "$username" "$password" "$user_id" "$group_id" \
                    "$user_info" "$home_dir" "$shell" \
                    "$min" "$max" "$inactive" "$warning"
  done <"$fichier"
fi

Notice also how read -r can split a line into fields for you, using the value of IFS as the field separator, and how we avoid using upper case for our private variables. (I'm speculating that you forgot to tell us that you managed to overwrite PATH too.)

As an aside, the while loop earlier in the script could also be simplified radically:

while true
do
  read -p "Donner le fichier :" -r fichier
  test -f "$fichier" && break
  echo "$0: $fichier: file not found" >&2
done

Notice how we print the error message to standard error, and include the name of the script and the name of the file we could not find, and also how read allows for the printing of a prompt with -p, and how we basically always quote variables with file names. As a rule, always use read -r unless you specifically want the shell to do funny things with backslashes (this is a legacy behavior for compatibility with the original Bourne shell).

In some more detail, when you read the whole file in one go, you would run a command line

useradd -m -c "charbel
assil
marwan
michel" "password 
p@ssw0rd
p@ssw0rd
password"

... etc instead of running

useradd -m -c "charbel" "password" "1001" "1001" "Charbel Haddad" "/home/charbel" "/bin/bash" "0" "30" "15" "7" "y"

separately and then

useradd -m -c "assil" "p@ssw0rd" "1002" "1002" "Assil" "/home/assel" "/bin/bash" "0" "30" "10" "5" "n"

etc with one line of input from the file at a time, which is what the while IFS=":" read -r loop above does.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I'm really thankful for your help but i tried your code and the error message is still the same – Daher Roy Dec 15 '20 at 11:00
  • You are overwriting `PATH` in some part of the script which you are not showing us. – tripleee Dec 15 '20 at 11:01
  • There is other functions like update and delete written in the script by the tutor , should i show you these functions too ? – Daher Roy Dec 15 '20 at 11:12
  • Thank you , i appreciate your help ! – Daher Roy Dec 15 '20 at 11:41
  • @tripleee Did you check the form of the `useradd` command or did you just copy it from OP? If I understood the manual correctly, `useradd -m -c "$username" ...` would interpret `$username` as a comment, not as the username. I think you have to prefix each variable with an option, e.g. `-u $user_id`, `-g $group_id`, although I'd rather drop the old IDs. Who knows what will happen if these IDs are already taken by other users. – Socowi Dec 15 '20 at 23:11
  • I didn't check; I was vaguely wondering about the same thing, but never brought it up. Thanks for your remark! – tripleee Dec 16 '20 at 04:29