What's the issue in my "adduser" from another file bash script

376 views Asked by At

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
1

There are 1 answers

6
tripleee On

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.