Teaching myself Here Document


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Teaching myself Here Document
# 1  
Old 10-29-2005
Teaching myself Here Document

I understand that a Here Document will redirect all of the lines between the beginning marker for the here document and the ending marker into the command specified just as if the text were coming from standard input.

I am trying to understand the Here Document with this example:
Code:
# Menu file

set -vx

clear=`tput clear`
bold=`tput bold`
bell=`tput bel`
off=`tput sgr0`

export clear bold bell off

${bold}
echo "\n\n              WELCOME TO"
echo "            MENU PROGRAM OF"
echo "         THE PHONEBOOK PROJECT \n\n"
${off}

echo "\tPress ${bold}A${off} to add information to the phonebook."
echo "\tPress ${bold}C${off} to change phonebook information."
echo "\tPress ${bold}D${off} to display phonebook information."
echo "\tPress ${bold}R${off} to remove phonebook information."
echo "\tPress ${bold}Q${off} to quit the phonebook."
read choice

${bell}
echo "\tYou have chosen $choice."

set +vx

I have attempted to make this a Here Document:
Code:
# Menu file

cat << alldone

clear=tput clear
bold=tput bold
bell=tput bel
off=tput sgr0

export clear bold bell off

${bold}
              WELCOME TO
            MENU PROGRAM OF
         THE PHONEBOOK PROJECT
$<off>


     Press ${bold}A${off} to add information to the phonebook.
     Press ${bold}C${off} to change phonebook information.
     Press ${bold}D${off} to display phonebook information.
     Press ${bold}R${off} to remove phonebook information.
     Press ${bold}Q${off} to quit the phonebook.
read choice

${bell}
You have chosen $choice.

alldone

When I execute the menu file, I am not able to input a choice. Am I missing something here?

Thanks,
Eric
# 2  
Old 10-30-2005
I've made progress!

Hello,

I've made some progress, but have a few questions.

Is there a way to read in user input inside a Here Document?

The last line of the add(draft) file is supposed to redirect the user input to the phonebook file, but it is not doing that. I am not sure what I am doing wrong.

So the redirected user input should be appended to the end of the phonebook file.

The display file should perform a search for the keyword(s) that the user inputs. Once again, I am not sure what I am missing or doing wrong.

Thanks,
Eric

Code:
# Menu file 

clear=`tput clear` 
bold=`tput bold` 
bell=`tput bel` 
off=`tput sgr0` 

export clear bold bell off 

cat<<alldone1 

${bold} 
             WELCOME TO 
           MENU PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 

alldone1 

cat<<alldone2 

    Press ${bold}A${off} to add information to the phonebook. 
    Press ${bold}C${off} to change phonebook information. 
    Press ${bold}D${off} to display phonebook information. 
    Press ${bold}R${off} to remove phonebook information. 
    Press ${bold}Q${off} to quit the phonebook. 

alldone2 

read choice 

cat<<alldone3 

${bell} 
You have chosen $choice. 

alldone3 

if [ "$choice" = "a" ] || [ "$choice" = "A" ] 
then 
       echo "Add" 
       add 

elif [ "$choice" = "c" ] || [ "$choice" = "C" ] 
then 
       echo "Change" 

elif [ "$choice" = "d" ] || [ "$choice" = "D" ] 
then 
       echo "Display" 

elif [ "$choice" = "r" ] || [ "$choice" = "R" ] 
then 
       echo "Remove" 

elif [ "$choice" = "q" ] || [ "$choice" = "Q" ] 
then 
       echo "Quit" 

fi 

############################################################# 
# add (draft) file 

clear=`tput clear` 
bold=`tput bold` 
bell=`tput bel` 
off=`tput sgr0` 

export clear bold bell off 

cat<<alldone1 

${clear} 
${bell} 

${bold} 
             WELCOME TO 
           ADD PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 


${bell} 
Enter the person's name: 

alldone1 

read name 

cat<<alldone2 

${bell} 
Enter the person's address: 
(Street address or mailing address) 

alldone2 

read address 

cat<<alldone3 

${bell} 
Enter the person's city: 

alldone3 

read city 

cat<<alldone4 

${bell} 
Enter the person's state: 

alldone4 

read state 

cat<<alldone5 

${bell} 
Enter the person's zip code: 
(Five or nine digits) 

alldone5 

read zipCode 

cat<<alldone6 

${bell} 
Enter the person's phone number: 
(Please include the area code) 

alldone6 

read phoneNumber 

cat<<alldone7 

${bell} 
$name:$address:$city:$state:$zipCode:$phoneNumber >> phonebook 

############################################################## 
# phonebook file 

clear=`tput clear` 
bold=`tput bold` 
bell=`tput bel` 
off=`tput sgr0` 

export clear bold bell off 

cat<<alldone1 

${bold} 
             WELCOME TO 
        PHONEBOOK PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 

The format for phonebook entries is: 
name:address:city:state:zipCode:phoneNumber 

alldone1 

################################################################ 
# display file 

clear=`tput clear` 
bold=`tput bold` 
bell=`tput bel` 
off=`tput sgr0` 

export clear bold bell off 

cat<<alldone1 

${bold} 
             WELCOME TO 
         DISPLAY PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 

alldone1 

cat<<alldone2 

${bell} 
${bold} 
Please enter your search keyword(s): 
${off} 

alldone2 

read keywords 

${bell} 
grep -i $keywords phonebook

# 3  
Old 10-30-2005
1. there's a missing command in the code ... and you cannot put that code in a cat HERE document because the cat command will only echo it instead of executing it ...
Code:
$name:$address:$city:$state:$zipCode:$phoneNumber >> phonebook 

should be ....

echo $name:$address:$city:$state:$zipCode:$phoneNumber >> phonebook

2. did you specify the path to the phonebook file?

also, you are putting in Here documents needlessly ... try to make your code simpler by ...
Code:
cat<<alldone1 

${bold} 
             WELCOME TO 
           MENU PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 

    Press ${bold}A${off} to add information to the phonebook. 
    Press ${bold}C${off} to change phonebook information. 
    Press ${bold}D${off} to display phonebook information. 
    Press ${bold}R${off} to remove phonebook information. 
    Press ${bold}Q${off} to quit the phonebook. 

alldone1

instead of ...
Code:
cat<<alldone1 

${bold} 
             WELCOME TO 
           MENU PROGRAM OF 
        THE PHONEBOOK PROJECT 
${off} 

alldone1 

cat<<alldone2 

    Press ${bold}A${off} to add information to the phonebook. 
    Press ${bold}C${off} to change phonebook information. 
    Press ${bold}D${off} to display phonebook information. 
    Press ${bold}R${off} to remove phonebook information. 
    Press ${bold}Q${off} to quit the phonebook. 

alldone2

... and variables that will have the same value throughout the script can be defined once at the beginning of the script and do not have to be redefined for each section ...
# 4  
Old 10-31-2005
I assume you are referring to the variables with the tput options.

The program is not really connected right now, it is in separate files.

Is it still possible to only declare them in one file and use them in the other files?


Thank you for the tips, they really helped me get everything straightened out.

Eric
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch the value from Here Document

I have code like var="1" echo << EOF export `var="2"` EOF echo $var The value of var is printed here is 1 but it should be 2 Any error there? ---------- Post updated at 11:44 AM ---------- Previous update was at 10:33 AM ---------- Also tried var="1" echo var << EOF echo... (1 Reply)
Discussion started by: adisky123
1 Replies

2. Homework & Coursework Questions

HTML document

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: <HTML><HEAD><TITLE>Personal Web Page</TITLE></HEAD> <BODY BGCOLOR="WHITE"><H3> <CENTER>My Personal Page<HR>... (0 Replies)
Discussion started by: Larry_1
0 Replies

3. UNIX for Dummies Questions & Answers

Teaching myself Unix - Need websites and other pointers

Hi, I am new to Unix. I've started with a book "Unix for Dummies". Please help, any websites with extensive amount of practice exercises are needed. I need to practice! Also I would appreciate any books that are good for beginners. Thanks to all! (1 Reply)
Discussion started by: zamcruiser
1 Replies

4. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

5. Shell Programming and Scripting

Document

Plz can somebody give me the shell and perl scripting documents,i need to start the scrpts learning.now i know about the linux commands,but need help in putting the same in the scripting with do,if,while and also using diffrent commands in the scrpipts,pls help.. (3 Replies)
Discussion started by: satish.res
3 Replies

6. Shell Programming and Scripting

The here document <<

Hello, I want to know the use of the here document with the << operator. Tried to go through some books but the concept was not clear. Please can any1 expalin me this with a simple example. Thanks, Rahul. (6 Replies)
Discussion started by: rahulrathod
6 Replies

7. Shell Programming and Scripting

echo with a here-document

I do not know how valid this post is, but here it is. I was writing a script for testing some development code that I had written. It involved many echo statements to redirect the output into a log file. Given the large number of echo statements, I took to this solution cat <<EOF >>... (2 Replies)
Discussion started by: vino
2 Replies

8. UNIX for Dummies Questions & Answers

one teaching Tip

Student have huge interest about why so many expert choose use UNIX than MS Windows. I consider that SHARE & OPEN is key point.:) (2 Replies)
Discussion started by: 111000
2 Replies

9. UNIX for Dummies Questions & Answers

Help on find a document

Hey all, I've been trying to find a document. The document is a list of all supported hardware that Solaris 7 supports. If anyone knows where this can be found thx in advanced. I've tried looking through the docs.sun.com site and can't find what I am after. It's just a list of hardware... (1 Reply)
Discussion started by: merlin
1 Replies
Login or Register to Ask a Question