1

I have a file(test) It contains two columns and two lines

pippo mm

pluto tt

I would like to create directory,owner and create password I do some think like that

while IFS= read a b; do
mkdir -p /srv/ftp/$a
chown -R ftp:ftp /srv/ftp/$a/
passwd $a $b
done <  /test

I expect to have 2 directory pippo and pluto under /srv/ftp/ but i have only pippo

thanks for your help.

  • Does your file test have blanks lines? – EightBitTony Apr 28 '16 at 8:44
0

You 'll want IFS to be a space, it should be like this :

while IFS=" " read a b; do
mkdir -p /srv/ftp/$a
chown -R ftp:ftp /srv/ftp/$a/
passwd $a $b
done <  /test 
  • unfortunately no I still have only Pluto not pluto and also pippo – Marco Apr 28 '16 at 8:55
  • Btw, what is $b supposed to be? – linerd Apr 28 '16 at 9:00
  • is the password mm I expect create director pippo set password for user pippo and also create director pluto set password for user pluto ecc .... – Marco Apr 28 '16 at 9:04
  • @Macro: Does your file test have blanks lines ? – Rahul Apr 28 '16 at 9:04
  • You can't pass the password to passwd command's arguments. So $b make no sense. Your test file should have only 1 column - the username – linerd Apr 28 '16 at 9:07
0

You have a file containing two space separated fields, the username and the password:

pippo mm
pluto tt

You can use a script like this to process the file, creating the user account and the corresponding directory:

ftpuid=$(getent passwd ftp | cut -d: -f3)
if test -z "$ftpuid"; then echo "No ftp account" >&2; exit 1; fi

while read -r user password
do
    test -z "$user" && continue
    echo "Creating account for $user, with home /srv/ftp/$user" >&2
    useradd -d /srv/ftp/"$user" -M -N -g ftp -o -u "$ftpuid" "$user"
    mkdir -m700 -p /srv/ftp/"$user"
    chown "$ftpuid":ftp /srv/ftp/"$user"
    test -n "$password" && ( echo "$password"; echo "$password" ) | passwd -q "$user" 2>/dev/null
done < file.txt

There are other options for settings passwords such as chpasswd, but I don't believe that this appropriate here.

  • unfortunately no I still have only Pluto not pluto and also pippo – Marco Apr 28 '16 at 14:09
  • @Marco the example data file and code I have provided here works fine on my test machine. I'll add some simple debug; please try it again with that line added. – roaima Apr 28 '16 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.