W
W
WhiteBachelor2018-10-28 21:45:54
linux
WhiteBachelor, 2018-10-28 21:45:54

Why doesn't a bash script navigate to a directory by parameter?

Hello! I am a beginner web developer. OS: Ubuntu 18.04
Recently encountered a problem: too lazy to manually create several directories and files with different extensions. That is, for each project, I have to make a directory, naming it with the name of the project, inside another 3-4 directories, with their own names, in each directory there are 1-2 files in different extensions, the names are always the same. It is much more convenient to do all this through the terminal, because the terminal has touch, but GNOME "create a text file" does not.
I decided to make a bash script. I used to do very, very simple ones, but here, I thought, it was not more difficult. The script has not been completed yet, because at startup it does not go to the specified directory. Why?
Here is the code:

#!/bin/bash
adress=$1
newFolderName=$2
cd
cd $1

Execution rights are set, there shouldn't be any problems. But it doesn't go to the directory.
Nerminal output after several attempts:
[email protected]:~$ chmod +x dirCreater.sh
[email protected]:~$ ./dirCreater.sh Документы
[email protected]:~$ ./dirCreater.sh Документы/
[email protected]:~$ ./dirCreater.sh /Документы
./dirCreater.sh: строка 5: cd: /Документы: Нет такого файла или каталога
[email protected]:~$ ls
     Общедоступные       Android      'VirtualBox VMs'  'Рабочий стол'   
   Видео     dirCreater.sh    Документы         Шаблоны       MW        Загрузки
 file.py         Изображения         netbeans-8.2     ScanResult2.txt       Музыка
[email protected]:~$ ./dirCreater.sh Документы
[email protected]:~$

As you can see, the address does not go.
PS: if I later use $2 to set the folder name, will it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Softer, 2018-10-28
@WhiteBachelor

Passes, but inside the script environment. To check - just call pwd in the script

G
Germanjon, 2018-10-29
@Germanjon

I would also advise adding quotes to the last line, it will save you from possible problems with spaces.

cd "$1"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question