Reputation: 9
I'm trying to use an IF/ELSE statement inside a FOR loops to generate two output files: count1 with numbers 1-5; count2 with numbers 6-10
I am trying
for i in {1..10}
do
if [ $i -le 5 ]
then
echo $i > count1.out
else
echo $i > count2.out
fi
done
but count1 only has "5" in it and count2 shows "10"
How can I fix this?
Upvotes: 1
Views: 510
Reputation: 19333
You are using the truncate-redirect operator, >
.
You likely intended to use the append-redirect operator, >>
.
Consider reading up on BASh I/O redirection in general. It will help you a lot with understanding shell scripts.
Upvotes: 1
Reputation:
Using >
to redirect to a file replaces the whole content of the file.
What you actually want to do is append to the file, which you can do with >>
like so:
echo "hello " > somefile.out # replace the contents of whatever is in somefile.out
echo "world!" >> somefile.out # append more stuff to somefile.out
More info here: https://www.gnu.org/software/bash/manual/html_node/Redirections.html
Upvotes: 1