Error in using aliases and read in ShellScript

2

I'm trying to create a command in the .bashrc of ubuntu, however I'm having a problem, I'm trying to create a folder with the variable entered at the time of calling the alias, but this is giving an error. When I open the terminal it already asks for the folder name, but I do not want this, I want to type the command anywhere. What am I doing wrong?

#comando digitado no terminal
username@username:~$ pasta nome_pasta

Where folder is the name of the alias, and folder_name is the variable obtained by read.

If I'm doing this I way wrong what would be the best method to have a command like the one above?

My code looks like this:

# arquivo .bashrc
function CreateFolder(){
    if [ -n "$USER" ]; then
        alias pasta='mkdir $name'
        read name
    else
        echo "Error :/"
    fi
}

CreateFolder

Thank you in advance for your help.

    
asked by anonymous 20.02.2016 / 17:14

2 answers

1

Since there is a call to the CreateFolder function at the end of the file .bashrc , it is already executing this function soon after login.

Here is an example function that asks the user for the folder name, if it is not informed in the parameter (created in .bashrc ):

pasta () {
        # Se o nome da pasta estiver definido no parametro
        if [ $1 ]; then
                # atribui diretamente a variavel PASTA
                PASTA=$1;
        else
                # Senão, pergunta para o usuário
                # qual o nome da pasta a ser criado
                echo "Qual o nome da pasta? "

                # Lê a entrada do usuário e guarda na variável PASTA
                read PASTA
        fi

        # Cria a pasta
        mkdir $PASTA
} 

# Não colocar a chamada para pasta aqui

After execution:

~/teste$ pasta teste1
~/teste$ ls
teste1
~/teste$ pasta
Qual o nome da pasta?
teste2
~/teste$ ls
teste1  teste2

tested with Debian 4.2.3-2 and GNU bash - 4.3.42

    
21.02.2016 / 01:41
0

If you only need pasta to create a directory, then you only need it in .bashrc :

alias pasta='mkdir'

At the time you type pasta uma_pasta Bash replaces pasta with mkdir before executing the command.

    
21.02.2016 / 01:27