LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-23-2011, 02:21 AM   #1
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Rep: Reputation: Disabled
Question Trying to understand script


In the following script why does it use the read builtin the first time it accepts input from the terminal and the cat utility the second time?

$ cat journal
# journal: add journal entries to the file
# $HOME/journal-file

file=$HOME/journal-file
date >> $file
echo -n "Enter name of person or group:"
read name
echo "$name" >> $file
echo >> $file
cat >> $file
echo "------------------------------------" >> $file
echo >> $file
 
Old 07-23-2011, 02:31 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
No idea; it's dysfunctional without a prior instruction to the user to enter data and terminate it with Ctrl+D.
 
Old 07-23-2011, 02:39 AM   #3
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
cat will accept a multi line input. After you hit enter with read all you get is one line.
 
Old 07-23-2011, 02:42 AM   #4
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
From what I gather from the text book is that read is used to store inputted information and cat is used to recall stored information. Is this correct?
 
Old 07-23-2011, 02:46 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kenny53067 View Post
From what I gather from the text book is that read is used to store inputted information and cat is used to recall stored information. Is this correct?
No. cat writes the content of a file. If no file name is given it reads stdin until it ends. Does your text book explain stdin, stdout and stderr? They are essential concepts for understanding how cat and many other programs work.
 
Old 07-23-2011, 02:59 AM   #6
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
No. cat writes the content of a file. If no file name is given it reads stdin until it ends. Does your text book explain stdin, stdout and stderr? They are essential concepts for understanding how cat and many other programs work.
I am unable to locate any of those. the name of my textbook is A practical guide to Fedora and Red Hat Enterprise Linux. If this helps.

The book says when you use cat, it copies a file to standard output. What is stdin??
 
Old 07-23-2011, 03:02 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kenny53067 View Post
I am unable to locate any of those. the name of my textbook is A practical guide to Fedora and Red Hat Enterprise Linux. If this helps.

The book says when you use cat, it copies a file to standard output. What is stdin??
Don't know that book; its description of cat is correct.

Wikipedia explains stdin, stdout and stderr.
 
1 members found this post helpful.
Old 07-23-2011, 03:04 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by kenny53067 View Post
The book says when you use cat, it copies a file to standard output.
Yes, but you need to tell it the file that it is to read!
 
Old 07-23-2011, 03:08 AM   #9
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by catkin View Post
Don't know that book; its description of cat is correct.

Wikipedia explains stdin, stdout and stderr.
Ok, I'll check those out.
Thanks for your help.
 
Old 07-23-2011, 03:20 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My main concern would be the same as catkin's original one, ie the script never tells the user to press Ctrl-D so they will be stuck at the command line until they read about this somewhere
or press Ctrl-C which most know will kill something but is incorrect here.
 
Old 07-23-2011, 03:21 AM   #11
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nylex View Post
Yes, but you need to tell it the file that it is to read!
The files name is journal-file, how do I tell it to read this file? is that where the #!/bin/bash comes in to play?
 
Old 07-23-2011, 03:24 AM   #12
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by kenny53067 View Post
The files name is journal-file, how do I tell it to read this file? is that where the #!/bin/bash comes in to play?
No. That line tells the shell which program is used to interpret the commands in your script.

You have the line

cat >> $file

which says to send the output from cat to the file in the $file (which is journal-file). Without the redirection, you just have

cat

In this case, cat will wait for you to type input at the command line (and terminate it by pressing Ctrl-D). If you want to output the contents of $file to the command line with cat, you use

cat $file

Does this help?
 
Old 07-23-2011, 03:37 AM   #13
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nylex View Post
No. That line tells the shell which program is used to interpret the commands in your script.

You have the line

cat >> $file

which says to send the output from cat to the file in the $file (which is journal-file). Without the redirection, you just have

cat

In this case, cat will wait for you to type input at the command line (and terminate it by pressing Ctrl-D). If you want to output the contents of $file to the command line with cat, you use

cat $file

Does this help?
Should I insert a comment telling to type and then press Ctrl-d after the cat >> $file
 
Old 07-23-2011, 03:40 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by kenny53067 View Post
Should I insert a comment telling to type and then press Ctrl-d after the cat >> $file
A comment would help anybody looking at the code/script. I guess you would prefer to help people running the script, in which case you could insert an echo giving instructions on the line before the cat.
 
Old 07-23-2011, 03:46 AM   #15
kenny53067
Member
 
Registered: Jul 2011
Location: Rio Grande City, Texas
Posts: 54

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
A comment would help anybody looking at the code/script. I guess you would prefer to help people running the script, in which case you could insert an echo giving instructions on the line before the cat.
Is this right: echo "Input information and then press Ctrl+d" on the line before cat >> $file
 
  


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
I want to understand script vaibhavs17 Linux - Newbie 2 10-19-2010 05:46 PM
[SOLVED] Help me understand this Perl script please. It's not homework. vxc69 Programming 33 08-03-2010 10:11 AM
I cannot understand this perl script mq15 Linux - Server 3 10-12-2009 12:44 AM
Perl Script..Difficult to understand ?? ajeetraina Linux - General 2 02-20-2008 11:17 PM
Help me understand this shell script dwhitney67 Linux - Software 2 10-18-2007 09:02 AM

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

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