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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-11-2005, 11:39 AM
|
#1
|
Member
Registered: Jan 2004
Posts: 71
Rep:
|
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
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
04-11-2005, 12:34 PM
|
#2
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
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
|
|
2 members found this post helpful.
|
03-28-2007, 12:02 PM
|
#3
|
LQ Newbie
Registered: Mar 2007
Posts: 5
Rep:
|
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
|
|
|
03-28-2007, 05:49 PM
|
#4
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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
|
|
1 members found this post helpful.
|
03-28-2007, 06:22 PM
|
#5
|
Member
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101
Rep:
|
what about cat?
Code:
cat > ./outfile <<DELIM
hello proble
h2 fadernaly
DELIM
|
|
|
08-07-2007, 11:15 PM
|
#6
|
LQ Newbie
Registered: Jun 2004
Location: India
Distribution: Red Hat
Posts: 20
Rep:
|
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?
|
|
|
08-08-2007, 01:02 AM
|
#7
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
08-08-2007, 08:24 AM
|
#8
|
Member
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101
Rep:
|
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
|
|
|
08-08-2007, 08:27 AM
|
#9
|
Member
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101
Rep:
|
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
|
|
|
08-08-2007, 06:07 PM
|
#10
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You last post shows that it is working using a HERE document.
|
|
|
All times are GMT -5. The time now is 10:53 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|