LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2013, 03:48 PM   #1
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302
Blog Entries: 4

Rep: Reputation: Disabled
for loop to add text to multiple files


Ok, yes, I'm a newb, but at least I'm trying to learn bash shell scripting. 8^)

From what I've learned about for loops, it seems like the following should work:

for i in $(ls ~) do;
> echo 'Pristine text for file ' > $i

Before I can continue the script, I get an error after hitting Enter (after $i):
bash: syntax error near unexpected token `echo'

I've tried using double quotes around the statement after echo, but I get the same results. The reason I'm doing all of this to test subversion version control, so I've made file1 file2 and so on for a total of 5 files. Since I'm still new at bash shell scripting, I'm not sure how else to have each file contain "Pristine text for file1", then for file2: "Pristine text for file2" and so on. The thought was, after each file gets the first echo statement, I could go back through each file and append (>>) the number to the end of each, containing 1, 2 and so on.

Any/all feedback is greatly appreciated.
 
Old 12-04-2013, 07:23 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

the semicolon should be before the "do", not after it.
Eg
Code:
for i in ~/* ; do 
  #echo 'Foo bar' > $i
done
Note two other things:
1. Better to use shell globbing instead of ls.
2. I don't really understand what you are trying to achieve, but not that you will be replacing every file in your home directory with this single line of text. So, running the above is VERY DANGEROUS and almost certainly not what you want to do (it is for this reason I put a comment before the echo).

Evo2.
 
Old 12-05-2013, 09:41 AM   #3
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302

Original Poster
Blog Entries: 4

Rep: Reputation: Disabled
Thanks for your reply. I'm testing subversion so I've created a couple of directores, d1 and d2. Within them, I have file1 through file5. I wanted to quickly generate text specific to each file - hence the script requirement. Obviously, if there's a better way to achieve this than what I'm doing, I'd definitely appreciate the help.
 
Old 12-05-2013, 07:08 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by loadedmind View Post
Thanks for your reply. I'm testing subversion so I've created a couple of directores, d1 and d2. Within them, I have file1 through file5.
Ok that's fine, but in the code you posted, the script will be operating on all the files in your home directory, not in the directories d1 and d2.

Quote:
I wanted to quickly generate text specific to each file - hence the script requirement.
Ok, that's fine, but you need to change the first line of the for loop. For example
Code:
for i in ~/d1/* ~/d2/* ; do
Quote:
Obviously, if there's a better way to achieve this than what I'm doing, I'd definitely appreciate the help.
I don't know the purpose of the test. Anyway what you are doing will make the contents of these files identical.

Evo2.
 
Old 12-05-2013, 07:41 PM   #5
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
one more thing do not use ls in find command it is not recommended nor required use it the way @evo2 mentioned above.
 
  


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
c++ loop and call in text files wackysiya Programming 1 07-14-2013 03:54 PM
Combine multiple text files into one text file? dirtydog7655 Linux - Newbie 6 05-16-2013 10:47 PM
Bash Question(for loop): How to Zip multiple files under multiple directories Znrall Linux - General 2 08-01-2012 01:52 PM
Delete files matching text - using a loop anjanesh Linux - General 3 09-26-2009 03:19 AM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-07-2007 11:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

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