Bash - Breaking while loop with user input [closed]
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
linux bash
closed as off-topic by Barmar, Tsyvarev, lagom, CozyAzure, Andrei Suvorkov Nov 26 '18 at 8:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Tsyvarev, lagom, CozyAzure, Andrei Suvorkov
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
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
linux bash
closed as off-topic by Barmar, Tsyvarev, lagom, CozyAzure, Andrei Suvorkov Nov 26 '18 at 8:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Tsyvarev, lagom, CozyAzure, Andrei Suvorkov
If this question can be reworded to fit the rules in the help center, please edit the question.
5
copy your code in 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 inread
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, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54
add a comment |
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
linux bash
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
linux bash
linux bash
edited Nov 25 '18 at 21:03
George Vasiliou
4,20221018
4,20221018
asked Nov 25 '18 at 20:59
Przemysław PorębskiPrzemysław Porębski
32
32
closed as off-topic by Barmar, Tsyvarev, lagom, CozyAzure, Andrei Suvorkov Nov 26 '18 at 8:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Tsyvarev, lagom, CozyAzure, Andrei Suvorkov
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Barmar, Tsyvarev, lagom, CozyAzure, Andrei Suvorkov Nov 26 '18 at 8:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Tsyvarev, lagom, CozyAzure, Andrei Suvorkov
If this question can be reworded to fit the rules in the help center, please edit the question.
5
copy your code in 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 inread
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, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54
add a comment |
5
copy your code in 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 inread
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, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54
5
5
copy your code in shellcheck.net
– ingroxd
Nov 25 '18 at 21:04
copy your code in shellcheck.net
– ingroxd
Nov 25 '18 at 21:04
Add spaces around
==
.– Barmar
Nov 25 '18 at 21:12
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
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
Quote your variables in case the folder name has spaces.
– Barmar
Nov 25 '18 at 21:12
Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54
Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54
add a comment |
1 Answer
1
active
oldest
votes
- Bash is whitespace (spaces, tabs, newlines) separated.
[ 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.- Always quote your variables, unless you know you don't have to.
- Bash uses single
=
for string comparision. Double==
is supported, but is not standard. - 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
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Bash is whitespace (spaces, tabs, newlines) separated.
[ 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.- Always quote your variables, unless you know you don't have to.
- Bash uses single
=
for string comparision. Double==
is supported, but is not standard. - 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
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
add a comment |
- Bash is whitespace (spaces, tabs, newlines) separated.
[ 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.- Always quote your variables, unless you know you don't have to.
- Bash uses single
=
for string comparision. Double==
is supported, but is not standard. - 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
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
add a comment |
- Bash is whitespace (spaces, tabs, newlines) separated.
[ 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.- Always quote your variables, unless you know you don't have to.
- Bash uses single
=
for string comparision. Double==
is supported, but is not standard. - 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
- Bash is whitespace (spaces, tabs, newlines) separated.
[ 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.- Always quote your variables, unless you know you don't have to.
- Bash uses single
=
for string comparision. Double==
is supported, but is not standard. - 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
edited Nov 25 '18 at 21:47
answered Nov 25 '18 at 21:21
Kamil CukKamil Cuk
11.8k1529
11.8k1529
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
add a comment |
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
2
2
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
It still has errors and will not work as intended. Line 14 is buggy. Is all explained in shellcheck.net
– George Vasiliou
Nov 25 '18 at 21:29
add a comment |
5
copy your code in 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 inread
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, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.
– jww
Nov 25 '18 at 21:54