Error using alias and read in Shellscript

Asked

Viewed 58 times

2

I’m trying to create a command on. Ubuntu bashrc, however I’m having a problem, I’m trying to create a folder with the variable typed when calling the alias, but it’s giving error. When I open the terminal it already asks for the folder name, but I don’t want it, I want to type the command anywhere. What I’m doing wrong?

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

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

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

my code is like this:

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

CreateFolder

thanks in advance for the help.

2 answers

1


How there is a function call CreateFolder at the end of the archive .bashrc, it is already running this function right after login.

Follow an example of a function that asks the user the name of the folder, if it is not reported 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

  • 1

    Thank you, your reply helped me a lot, I didn’t think it would be that easy lol.

0

If you just need pasta create a directory, so you only need this in .bashrc:

alias pasta='mkdir'

The moment you type pasta uma_pasta bash pasta for mkdir before executing the command.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.