for i in {1..10}
if [ $i=5 ]
then
echo "Youve completed 5 iterations, do you wish to continue?"
read input
if [$input ="Yes"]
then
continue
else
break
fi
if [$i==10]
then
break
do
echo "Iteration no: $i"
echo "Enter a number"
read number
echo "youve entered $number"
done

- 158,873
- 26
- 254
- 302

- 1
- 1
-
1please update the question to include the complete error message; there are actually several syntax issues ... `for` loop is missing the initial `do`; a couple `if` statements are missing ending `fi`s; your `do ... done` block doesn't appear to match up with any leading `while` or `for` ... – markp-fuso Jun 30 '22 at 22:47
-
2[shellcheck](https://www.shellcheck.net/) will give you good suggestions – jhnc Jun 30 '22 at 23:04
1 Answers
It's helpful to review the basics of Bash Scripting a bit more before jumping into the fire.
if [ "foo" = "foo" ]
then
echo "Equal"
else
echo "Not Equal"
fi
In Bash script, the spacing between variables and brackets is significant, meaning the if statement above is NOT the same as [ "foo"=="foo"]
(Notice the lack of a space)
Now let's add a loop
for i in {1..10}
do
if [ $i -eq 1 ]
then
echo "Equal"
else
echo "Not Equal"
fi
done
If you're comparing integers, you should use -eq
for an equals comparison.
Make sure you're adding the 'do' and 'done' statements. It helps to use indents in your code to make it easier to follow the script execution. Everything 4 spaces over will execute inside the loop.
From here you should be able to build up to your final script which checks for user input.
It's fun to jump in and try new things but always be sure to reference the documentation! https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Commands

- 111
- 1
- 3
-
1
-
See the relevant POSIX spec at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html (`[` being a synonym for `test`). Yes, `==` is always supported in bash's builtin version of `[`, but being in the habit of using it means that code will break with baseline POSIX `/bin/sh`. – Charles Duffy Jun 30 '22 at 23:12
-
-
BTW, in bash (which is needed for `{1..10}`, you can use `(( i == 1 ))` instead of `[ "$i" -eq 1]` (note the quotes, without which error messages when `i` is empty are less useful and weird values of IFS can cause surprising behavior; it's unusual to have something like `IFS=0`, but there's been a time I was parsing a file format where it was appropriate to do so).. – Charles Duffy Jul 01 '22 at 15:06