LinuxQuestions.org
Visit Jeremy's Blog.
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 09-17-2007, 08:20 AM   #1
jerf
Member
 
Registered: Sep 2007
Posts: 46

Rep: Reputation: 15
simple scripting question


im following a tutorial at linuxcommand.org on how to write basic scripts and do simple things with them.

one of the scripts uses echo
//////////////////////////////////////////////
#!/bin/bash

# make_page - A script to produce an HTML file

echo "<HTML>"
echo "<HEAD>"
echo " <TITLE>"
echo " The title of your page"
echo " </TITLE>"
echo "</HEAD>"
echo ""
echo "<BODY>"
echo " Your page content goes here."
echo "</BODY>"
echo "</HTML>"
///////////////////////////////////////////////////

and the author suggests that a smarter way to achieve the same effect would be to use a 'here script' which in the example uses the 'cat' command and a token,

//////////////////////////////////////////////////////
#!/bin/bash

# make_page - A script to produce an HTML file

cat << _EOF_
<HTML>
<HEAD>
<TITLE>
The title of your page
</TITLE>
</HEAD>

<BODY>
Your page content goes here.
</BODY>
</HTML>
_EOF_
/////////////////////////////////////////////////////////

my question is about the cat command i guess. how does cat work, and also how does it replace what was achieved by the echo command?

thanks for your help folks.
 
Old 09-17-2007, 08:38 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hi!
If you haven't, read the man pages for cat and echo, to see where most of the difference lies (type 'man cat' or 'man echo' in a console)

From my limited experience, there isn't much difference in the functionality of getting info into a file. However, I believe the cat statement(s) would require slightly less code. To show this, consider the following example, which you can do in a console:

Example a)
Code:
echo "Hello, this is a sentence" > file.txt
echo " This is another sentence" >> file.txt
echo "This is sentence number three" >> file.txt
echo "This is the fourth and last sentence" >> file.txt
cat file.txt

Example B)
Code:
cat << EOF > file.txt
>This is sentence one.
>this is sentence two!
>This is sentence number three...
>And finally, number four. Were done.
>EOF
cat file.txt
Note that there is less repetition using cat, because you don't need to use the quotes and the word 'echo' with every line.

Now, with echo however, you could do something like :
Code:
echo -e "This is sentence one \nThis is sentence two\nThis is sentence number 3\nAnd this is sentence number four\n" > file.txt

cat file.txt
And wind up with the same results. The -e switch makes echo interpret escape characters (like \n for newline).
Note that the > means 'make the file if it does not exist, or overwrite it if it exists'
Using >> means 'append to the file if it exists, or make it if it does not exist'

Does this help any?

Last edited by GrapefruiTgirl; 09-17-2007 at 09:47 AM. Reason: typo
 
Old 09-17-2007, 08:42 AM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
This helps out with 'cat':

http://unixhelp.ed.ac.uk/CGI/man-cgi?cat

As usual, there are several ways to achieve your goals when scripting. Basically what 'echo' does is repeat what you give it as a parameter. So 'echo hello' prints 'hello'. That means you'll need one echo per one line (in the example script) to produce the lines you want. cat on the other hand concatenates things, and if you feed something to it (say a file, which can also be stdin which is standard input, meaning your keyboard!), it will read the contents and print them out (several files cat'ed result in their contents "spit out" after each other, concatenating the whole content). So instead of issuing seven to seven hundred echo commands, one per line, you can use one 'cat' command and feed it data from a special file 'stdin' (the standard input, keyboard, remember?) which it then reads (until the EOF is found, like in the example, so it won't read forever!), and when it finishes, it spits out the same content as echo but with one 'cat' run instead of several 'echo's. The script, when run, then basically just writes the whatever content you want to stdout (standard output, usually terminal) which you can then forward to a html file for example.

You can try just
Code:
cat
and then writing something. Whatever you write, it'll read it and print out. That's one way of using cat..another way is then giving it a "real" file (not stdin for example) and so on. You can quit with CTRL-C.
 
Old 09-17-2007, 11:56 PM   #4
jerf
Member
 
Registered: Sep 2007
Posts: 46

Original Poster
Rep: Reputation: 15
hello again,

i think what i wasnt getting was that cat serves basically two functions; to not only link files or input together, but also to return the input in the form of standard output much like echo. I had read the man pages before but hadnt grasped the "to standard output" part of "Concatenate FILE(s), or standard input,".

thanks for the clarification.
 
Old 09-19-2007, 03:12 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The short version is that 'echo' expects arg(s) that are/look like variables. 'cat' expects to deal with a whole file (or something that looks like one eg here doc).
 
  


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
simple question regarding shell scripting branche_dude Linux - Newbie 8 07-21-2006 02:35 AM
Very simple BASH scripting question clickster Linux - Newbie 6 11-23-2005 04:28 PM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 03:41 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 01:44 PM
simple scripting question wedgeworth Programming 6 02-04-2004 10:30 AM

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

All times are GMT -5. The time now is 06:04 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