0

I am total beginer with linux.

I have found that my question might be sloved already, but i cannot transfer answers to my example.

I am trying to make script which is asking user to provide folder name, then creates directory. After that, bash asks does user want to create next folder, if answer is different than yes, while loop should break.

I appreciate any kind of help. Thank you in advance. My code: https://pastebin.com/xKNgV9gg

#!/bin/bash
echo 'Welcome in folder generator'
echo '#################################################'
echo '#################################################'

new_directory="yes"

while [ "$new_directory"=="yes" ]
do
    echo 'Give me folder name'
    read folderName
    mkdir $folderName
    echo "Would you like to create next folder ?"
    read $new_directory
done
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • 5
    copy your code in https://shellcheck.net – ingroxd Nov 25 '18 at 21:04
  • Add spaces around `==`. – Barmar Nov 25 '18 at 21:12
  • Don't put `$` before the variable name in `read` command. – Barmar Nov 25 '18 at 21:12
  • Quote your variables in case the folder name has spaces. – Barmar Nov 25 '18 at 21:12
  • Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Nov 25 '18 at 21:54

1 Answers1

-1
  1. Bash is whitespace (spaces, tabs, newlines) separated.
  2. [ a = b ] is not equal to [ a=b ]. The first compares string 'a' with string 'b', the second check if the string 'a=b' has non-zero length.
  3. Always quote your variables, unless you know you don't have to.
  4. Bash uses single = for string comparision. Double == is supported, but is not standard.
  5. A good read can be found in this thread.

#!/bin/bash
echo 'Welcome in folder generator'
echo '#################################################'
echo '#################################################'

new_directory="yes"

while [ "$new_directory" == "yes" ]
do
    echo 'Give me folder name'
    read folderName
    mkdir "$folderName"
    echo "Would you like to create next folder ?"
    read new_directory
done
KamilCuk
  • 120,984
  • 8
  • 59
  • 111