GNU/Linux >> Linux Esercitazione >  >> Linux

Come aggiungere un utente Unix/linux in uno script Bash?

Ecco il mio script di test bash. Non riesco a farlo funzionare. Vedo due errori:

  1. Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
  2. Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.

Ecco il mio copione:

#!/bin/bash
sudo adduser myuser << ENDX
password
password
First Last




Y
ENDX
exit 0

Ecco l'output:

[email protected]$ ./adduser.sh 
Adding user `myuser' ...
Adding new group `myuser' (1001) ...
Adding new user `myuser' (1001) with group `myuser' ...
Creating home directory `/home/myuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Changing the user information for myuser
Enter the new value, or press ENTER for the default
        Full Name []:   Room Number []:         Work Phone []:  Home Phone []:  Other []: Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
Is the information correct? [Y/n] [email protected]$

Questo è su Kubuntu 12.04 LTS

$ bash --version
bash --version
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

Ecco le righe di adduser (script di sistema – non modificato da me) con le annotazioni dei due numeri di riga rilevanti:

for (;;) {
       my $chfn = &which('chfn');
    &systemcall($chfn, $new_name);
    # Translators: [y/N] has to be replaced by values defined in your
    # locale.  You can see by running "locale yesexpr" which regular
    # expression will be checked to find positive answer.
    print (gtx("Is the information correct? [Y/n] "));
    chop (my $answer=<STDIN>);        <-- LINE 589
    last if ($answer !~ m/$noexpr/o); <-- LINE 590
}

Risposta accettata:

Usa semplicemente i parametri della riga di comando invece di stdin e usa chpasswd per la password.

Ad esempio:

sudo adduser myuser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password
echo "myuser:password" | sudo chpasswd

Linux
  1. Come aggiungere o rimuovere un utente da un gruppo in Linux

  2. Come cancellare la cronologia della riga di comando BASH in Linux

  3. Come utilizzare la password crittografata nello script della shell Bash Linux

  4. Come modificare, rimuovere o disabilitare la password utente in Linux

  5. Aggiungi utente al gruppo in Linux, come farlo (con esempi)

Come aggiungere un utente al gruppo in Linux

Come aggiungere un utente al gruppo in Linux

Come modificare la password utente in Linux

Come modificare o impostare la password utente in Linux

Come aggiungere un utente al gruppo su Linux

Come aggiungere un utente a un gruppo Linux