What am I doing wrong here (issue with loops)
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This thing here
n=0
x=1
while [ $n -lt 6 ]
do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]
then
while [ $x -le 5 ]
do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Output this thing
sasadgsad gsda 1
sasadgsad gsda 2
sasadgsad gsda 3
sasadgsad gsda 4
sasadgsad gsda 5
end of line1
end of line2
end of line3
end of line4
end of line5
sasadgsad gsda 6
That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?
Thank you for the help :)
I even tried it with n=1 and while [ $n -le 5 ]
linux shell-script
add a comment |Â
up vote
0
down vote
favorite
This thing here
n=0
x=1
while [ $n -lt 6 ]
do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]
then
while [ $x -le 5 ]
do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Output this thing
sasadgsad gsda 1
sasadgsad gsda 2
sasadgsad gsda 3
sasadgsad gsda 4
sasadgsad gsda 5
end of line1
end of line2
end of line3
end of line4
end of line5
sasadgsad gsda 6
That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?
Thank you for the help :)
I even tried it with n=1 and while [ $n -le 5 ]
linux shell-script
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This thing here
n=0
x=1
while [ $n -lt 6 ]
do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]
then
while [ $x -le 5 ]
do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Output this thing
sasadgsad gsda 1
sasadgsad gsda 2
sasadgsad gsda 3
sasadgsad gsda 4
sasadgsad gsda 5
end of line1
end of line2
end of line3
end of line4
end of line5
sasadgsad gsda 6
That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?
Thank you for the help :)
I even tried it with n=1 and while [ $n -le 5 ]
linux shell-script
This thing here
n=0
x=1
while [ $n -lt 6 ]
do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]
then
while [ $x -le 5 ]
do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Output this thing
sasadgsad gsda 1
sasadgsad gsda 2
sasadgsad gsda 3
sasadgsad gsda 4
sasadgsad gsda 5
end of line1
end of line2
end of line3
end of line4
end of line5
sasadgsad gsda 6
That 11th line shouldn't be there... Shouldn't the first while thing finish when n = 5? Why does it create that last line?
Thank you for the help :)
I even tried it with n=1 and while [ $n -le 5 ]
linux shell-script
linux shell-script
edited Aug 7 at 14:22
Kusalananda
106k14209327
106k14209327
asked Aug 7 at 13:53
iamAguest
945
945
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Your script with proper indentation:
n=0
x=1
while [ $n -lt 6 ]; do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]; then
while [ $x -le 5 ]; do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Your outer loop runs from 0 to 5, which is six times. Since you update n
at the start of the outer loop, the value of n
will go from 1 to 6 in the body of the loop. When n
is 5, you run another loop from 1 to 5 outputting end of line...
. When that's done, you still have one iteration of the outer loop to do.
Another way to write the script in bash
:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
if (( n == 5 )); then
for (( x=1; x<=5; ++x )); do
printf 'end of line%sn' "$x"
done
fi
done >>/home/test/rptest
This would not have the same issue because the outer loop stops when n
reaches 6.
However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
done >>/home/test/rptest
for (( n=1; n<=5; ++x )); do
printf 'end of line%sn' "$n"
done >>/home/test/rptest
or, for this simple example only,
printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
printf 'end of line%sn' 1..5 >>/home/test/rptest
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember thatsh
may not understand this type of loop, so run your scripts withbash
to be sure it works, notsh
.
â Kusalananda
Aug 7 at 14:13
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your script with proper indentation:
n=0
x=1
while [ $n -lt 6 ]; do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]; then
while [ $x -le 5 ]; do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Your outer loop runs from 0 to 5, which is six times. Since you update n
at the start of the outer loop, the value of n
will go from 1 to 6 in the body of the loop. When n
is 5, you run another loop from 1 to 5 outputting end of line...
. When that's done, you still have one iteration of the outer loop to do.
Another way to write the script in bash
:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
if (( n == 5 )); then
for (( x=1; x<=5; ++x )); do
printf 'end of line%sn' "$x"
done
fi
done >>/home/test/rptest
This would not have the same issue because the outer loop stops when n
reaches 6.
However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
done >>/home/test/rptest
for (( n=1; n<=5; ++x )); do
printf 'end of line%sn' "$n"
done >>/home/test/rptest
or, for this simple example only,
printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
printf 'end of line%sn' 1..5 >>/home/test/rptest
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember thatsh
may not understand this type of loop, so run your scripts withbash
to be sure it works, notsh
.
â Kusalananda
Aug 7 at 14:13
add a comment |Â
up vote
2
down vote
accepted
Your script with proper indentation:
n=0
x=1
while [ $n -lt 6 ]; do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]; then
while [ $x -le 5 ]; do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Your outer loop runs from 0 to 5, which is six times. Since you update n
at the start of the outer loop, the value of n
will go from 1 to 6 in the body of the loop. When n
is 5, you run another loop from 1 to 5 outputting end of line...
. When that's done, you still have one iteration of the outer loop to do.
Another way to write the script in bash
:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
if (( n == 5 )); then
for (( x=1; x<=5; ++x )); do
printf 'end of line%sn' "$x"
done
fi
done >>/home/test/rptest
This would not have the same issue because the outer loop stops when n
reaches 6.
However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
done >>/home/test/rptest
for (( n=1; n<=5; ++x )); do
printf 'end of line%sn' "$n"
done >>/home/test/rptest
or, for this simple example only,
printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
printf 'end of line%sn' 1..5 >>/home/test/rptest
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember thatsh
may not understand this type of loop, so run your scripts withbash
to be sure it works, notsh
.
â Kusalananda
Aug 7 at 14:13
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your script with proper indentation:
n=0
x=1
while [ $n -lt 6 ]; do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]; then
while [ $x -le 5 ]; do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Your outer loop runs from 0 to 5, which is six times. Since you update n
at the start of the outer loop, the value of n
will go from 1 to 6 in the body of the loop. When n
is 5, you run another loop from 1 to 5 outputting end of line...
. When that's done, you still have one iteration of the outer loop to do.
Another way to write the script in bash
:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
if (( n == 5 )); then
for (( x=1; x<=5; ++x )); do
printf 'end of line%sn' "$x"
done
fi
done >>/home/test/rptest
This would not have the same issue because the outer loop stops when n
reaches 6.
However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
done >>/home/test/rptest
for (( n=1; n<=5; ++x )); do
printf 'end of line%sn' "$n"
done >>/home/test/rptest
or, for this simple example only,
printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
printf 'end of line%sn' 1..5 >>/home/test/rptest
Your script with proper indentation:
n=0
x=1
while [ $n -lt 6 ]; do
n=$(( n+1 ))
echo "sasadgsad gsda $n" >> /home/test/rptest
if [ $n -eq 5 ]; then
while [ $x -le 5 ]; do
echo "end of line$x" >> /home/test/rptest
x=$(( x+1 ))
done
fi
done
Your outer loop runs from 0 to 5, which is six times. Since you update n
at the start of the outer loop, the value of n
will go from 1 to 6 in the body of the loop. When n
is 5, you run another loop from 1 to 5 outputting end of line...
. When that's done, you still have one iteration of the outer loop to do.
Another way to write the script in bash
:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
if (( n == 5 )); then
for (( x=1; x<=5; ++x )); do
printf 'end of line%sn' "$x"
done
fi
done >>/home/test/rptest
This would not have the same issue because the outer loop stops when n
reaches 6.
However, if you just want to append the output of the inner loop after the that of the outer, you might just as well run the after each other:
for (( n=1; n<=5; ++n )); do
printf 'sasadgsad gsda %sn' "$n"
done >>/home/test/rptest
for (( n=1; n<=5; ++x )); do
printf 'end of line%sn' "$n"
done >>/home/test/rptest
or, for this simple example only,
printf 'sasadgsad gsda %sn' 1..5 >>/home/test/rptest
printf 'end of line%sn' 1..5 >>/home/test/rptest
edited Aug 7 at 14:24
answered Aug 7 at 13:59
Kusalananda
106k14209327
106k14209327
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember thatsh
may not understand this type of loop, so run your scripts withbash
to be sure it works, notsh
.
â Kusalananda
Aug 7 at 14:13
add a comment |Â
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember thatsh
may not understand this type of loop, so run your scripts withbash
to be sure it works, notsh
.
â Kusalananda
Aug 7 at 14:13
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
Thanks! The "for" version of this is much better, I'm gonna use it from now on :D
â iamAguest
Aug 7 at 14:12
@iamAguest Just remember that
sh
may not understand this type of loop, so run your scripts with bash
to be sure it works, not sh
.â Kusalananda
Aug 7 at 14:13
@iamAguest Just remember that
sh
may not understand this type of loop, so run your scripts with bash
to be sure it works, not sh
.â Kusalananda
Aug 7 at 14:13
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461076%2fwhat-am-i-doing-wrong-here-issue-with-loops%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password