LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-11-2005, 11:39 AM   #1
twistedpair
Member
 
Registered: Jan 2004
Posts: 71

Rep: Reputation: 15
echo multiple lines of text


Hi all,
I haven't found an answer to this just yet, and maybe one of you can help. I need to have a script write multiple lines of text to a file. Instead of doing it like this:

echo stuff > /etc/file
echo more stuff >> /etc/file

Is there a way to make it so that I don't have to prefix every line with "echo" and put the path after each line? Maybe there is a better way to do this?

Thanks,
Pair
 
Old 04-11-2005, 12:34 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Hi,

Just start it with a quote and don't use the closing quote until you are done. For example....
Code:
# echo " this is line one
> this is line two
> this is line thre
> rats, spelling is off" > file.txt

# cat file.txt
 this is line one
this is line two
this is line thre
rats, spelling is off
 
Old 03-28-2007, 12:02 PM   #3
eric.frederich
LQ Newbie
 
Registered: Mar 2007
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by homey
Hi,

Just start it with a quote and don't use the closing quote until you are done. For example....
Code:
# echo " this is line one
> this is line two
> this is line thre
> rats, spelling is off" > file.txt

# cat file.txt
 this is line one
this is line two
this is line thre
rats, spelling is off
I'm trying to create a file by echoing as well but my file is a script and starts with #!/bin/sh and I'm having problems with that. This is what I get...

Code:
$ echo "#!/bin/sh
bash: !/bin/sh: event not found
 
Old 03-28-2007, 05:49 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,161

Rep: Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504
Use for that line a single quote. Double quotes are weak quotes and special characters are still recognized, '#!' apparently being on eof them. Single quotes are strong: what is between the quotes is takens as string.

jlinkels
 
Old 03-28-2007, 06:22 PM   #5
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
what about cat?
Code:
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM
 
Old 08-07-2007, 11:15 PM   #6
vadirajcs
LQ Newbie
 
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20

Rep: Reputation: 1
Quote:
Originally Posted by elsheikhmh
what about cat?
Code:
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM


this gives the output different.

#/bin/sh
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM

echo "I just wrote 2 lines to the file ./outfile"
-------------------------------------------------

#cat ./outfile
hello proble
h2 fadernaly
DELIM

echo "I just wrote 2 lines to the file ./outfile"




Any better solution on this?
 
Old 08-08-2007, 01:02 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Your last message doesn't look right.

The "DELIM" will not be written to the file. This method is known as a "here document". It allows for variable expansion as well, and is a common method used to do just what you said you wanted to do.

It allows you to embed documents you want written inside a single script. It was once a common technique used in installation scripts.

Quote:
$ echo "#!/bin/sh
bash: !/bin/sh: event not found
In an interactive shell, the exclamation point is expanded to a previous bash command. This isn't the case with non-interactive scripts however.

Please refer to the "info bash" manual for the details. Here documents are covered in Section 3.6.5. The Event designator (!) is covered in section 9.3.1.

Last edited by jschiwal; 08-08-2007 at 01:04 AM.
 
Old 08-08-2007, 08:24 AM   #8
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
Quote:
Originally Posted by vadirajcs
this gives the output different.

#/bin/sh
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM

echo "I just wrote 2 lines to the file ./outfile"
-------------------------------------------------

#cat ./outfile
hello proble
h2 fadernaly
DELIM

echo "I just wrote 2 lines to the file ./outfile"




Any better solution on this?
No way! Your message is very weird!! I tried it again and it gave me the same result. Please report your distro/version
 
Old 08-08-2007, 08:27 AM   #9
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
I'm running this moment on a windows box equipped with MinGW32:
Code:
$ cat test.sh
#/bin/sh
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM

echo "I just wrote 2 lines to the file ./outfile"

mustafa@LILPC ~
$ ./test.sh
I just wrote 2 lines to the file ./outfile

mustafa@LILPC ~
$ cat ./outfile
hello proble
h2 fadernaly
 
Old 08-08-2007, 06:07 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You last post shows that it is working using a HERE document.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
multiple clients for echo server msriram_linux Linux - Networking 1 12-05-2007 03:27 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
echo typing text? LocoMojo Linux - Newbie 9 08-30-2005 09:41 PM
Can't get echo to produce two lines of text sknarf Linux - Software 1 06-21-2004 11:48 AM
Delay in echo when entering text into Firefox 0.8 Shade Linux - Software 4 04-02-2004 12:01 PM


All times are GMT -5. The time now is 12:28 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration