Adduser in Shellscript [duplicated]

0

I have to create a script that can receive up to two parameters:

In case you receive 0 parameters the script has to show the login of all system users.

In the case that it receives 1 parameter if parameter 1 is "-h" that shows a help or text and if parameter 1 is a name for example "Antonio" it creates said user by asking for the password that he will want, name, address, email and phone number, creating the user with this information.

If you receive 2 parameters:

If you receive the format "-l Antonio, Paco will do the same as the previous case but creating the users entered, separated by a comma.

If the script is called incorrectly, it should return an error.

At the moment I have managed more or less the structure of the script but only gives me one failure after another and I can not finish it ... Let's see if someone who is more placed on ShellScripts can help me to finish it, because I can not find the way for more than I edit the code.

I leave you what I have here:

#!/bin/bash
if [ $# = 0 ];then
      echo $(users)
      echo $(who)
elif [ $# = 1 ];then
     if [ $1 = "-h" ];then
     echo "Tienes que escribir el nombre del script y un nombre de usuario o 
si pones mas de un usuario, tienes que separarlos por coma."
     else
        $(adduser -d -c $1) #no me acuerdo de como era la sintaxis exacta del 
adduser para preguntar por la contraseña
      fi
elif [ $# = 2 ]; then
    if [ $1 = '-l' ] then;
       for x in (echo $2 | cut -d ',' -f1) then
        usuario = echo $2 | cut -d ',' -f1
        $(adduser -d -c $1)
       done
    fi
else
    echo "Usa -h para más información"
fi
    
asked by ShJod 13.05.2018 в 15:30
source

1 answer

1

1 Look at the way you execute a command or program.

    echo $(users) 
    # Ejecutas el programa echo que imprime en la stdout del script 
    # la stdout de la ejecución del programa users.
    users
    # Ejecuta directamente el programa users y muestra la stdout directamente.

    $(adduser -d -c $1)
    # Ejecutas adduser en otro ambito y supongo que el script intentará
    # ejecutar su stdout cuando finalice interpretandolo como si fuera el nombre
    # de un programa.
    adduser -d -c $1
    # Ejecuta directamente adduser

2 If conditions, check the status of ;

    #!/bin/bash
    if [ "foo" = "foo" ]; then
        echo expression evaluated as true
    fi

3 For loop, check the syntax

    #!/bin/bash
    for i in $( ls ); do
        echo item: $i
    done

4 Review how you assign the stdout of a command to a variable, important the spaces

    var=$(comando)

5 Notice if users with the -l option are separated from the Antonio, Paco form, such as a string delimited by quotation marks "Antonio, Paco" or Antonio,Paco since space is taken as a delimiter Internal field separator when setting the number of parameters.

6 Note that echo $2 | cut -d ',' -f1 only prints the first field delimited by a comma, as indicated by -f1 .

7 I show you if it is useful for you that this loop prints line by line the letters of a string of letters separated by space or any other character if indicated in the $ IFS variable

    for x in $(echo "a b c d");do
        echo $x
    done

8 To add a user with adduser link

    
answered by 13.05.2018 / 18:13
source