LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-02-2020, 11:00 AM   #1
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Rep: Reputation: Disabled
simple for loop


HEllo guys,

I'm trying to take my output into a txt file but something is wrong.

Code:
H=$(head -n1 SAMPLE.txt) ## = 19990

for ((i = $H  ; i <= 19999 ; i++))
  do
  echo $i > FILL2.txt
 done
for some reason it gives me only 19999 in FILL2.txt
while the output of echo $i is:
19990
19991
19992
19993
19994
and so on until 19999

what is wrong? I don't understand.
 
Old 03-02-2020, 11:08 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
head -n1 SAMPLE.txt will give you the first line of SAMPLE.txt

You want to append not overwrite.
Code:
echo $i >> FILL2.txt
 
Old 03-02-2020, 11:16 AM   #3
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
head -n1 SAMPLE.txt will give you the first line of SAMPLE.txt

You want to append not overwrite.
Code:
echo $i >> FILL2.txt
I already tried it, when I do it
Code:
echo $i >> FILL2.txt
it gives me the next output:
19999
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999

hence - 19999 is two times in FILL2.txt
I want to overwrite, not to append.
 
Old 03-02-2020, 11:29 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,141
Blog Entries: 6

Rep: Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828Reputation: 1828
If you want the file to be empty at the start of each loop then clear the file.

Code:
for i in {1..10}; do
    echo "$i" >> file.txt
done

cat file.txt
1
2
3
4
5
6
7
8
9
10

> file.txt
or
truncate -s 0 file.txt

for i in {a..j}; do
    echo "$i" >> file.txt
done

cat file.txt
a
b
c
d
e
f
g
h
i
j
 
Old 03-02-2020, 11:35 AM   #5
hatfani
Member
 
Registered: Oct 2019
Distribution: centos7
Posts: 52

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
If you want the file to be empty at the start of each loop then clear the file.

Code:
for i in {1..10}; do
    echo "$i" >> file.txt
done

cat file.txt
1
2
3
4
5
6
7
8
9
10

> file.txt
or
truncate -s 0 file.txt

for i in {a..j}; do
    echo "$i" >> file.txt
done

cat file.txt
a
b
c
d
e
f
g
h
i
j

Those things I already knew before, I appreciate your attention but it's not exactly what I wanted to do.
I wanted to overwrite it, appending interrupting me when I run the script once again - cause it's appending more unnecessary data again.
 
Old 03-02-2020, 12:28 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,733

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by hatfani View Post
Those things I already knew before, I appreciate your attention but it's not exactly what I wanted to do.
I wanted to overwrite it, appending interrupting me when I run the script once again - cause it's appending more unnecessary data again.
It's not at all clear what you're trying to accomplish, to me.

Your original post says read one line from a file that has the value of 19990, then loop from 19990 to 19999 and save the loop value into a text file, overwriting each time. That wasn't doing what you wanted, as it always showed 19999.
It was however, doing what you told it to do, which is the nature of programming...the darn machines will always only do what you tell them to do.

You were told how to avoid overwriting by appending, but that wasn't what you wanted either.

Please try to explain exactly what you're trying to accomplish, because I, for one, don't understand.
 
1 members found this post helpful.
Old 03-02-2020, 01:13 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Redirect the whole loop:
Code:
for ((i = $H  ; i <= 19999 ; i++))
do
  echo $i
done > FILL2.txt
The shell opens the file when the loop is read into memory.
Then the loop is processed and standard output goes to the file.
The file is closed when the loop ends.

Last edited by MadeInGermany; 03-02-2020 at 01:14 PM.
 
1 members found this post helpful.
Old 03-02-2020, 06:47 PM   #8
thw1959
LQ Newbie
 
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
One suggestion:
Or use -v or -x to debug.
Script = t1.sh
run: bash -x t1.sh

OR:

Add #!/bin/bash -x as first line to script
make executable: chmod u+x t1.sh
Run: ./t1.sh

Beware that depending on sh or bash, variables in for loops have different requirements.
I always use bash..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Simple Slackware vs simple Arch vs simple Frugalware punchy71 Linux - Distributions 2 08-28-2012 02:30 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration