LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-29-2008, 02:57 PM   #1
ravi2ray
LQ Newbie
 
Registered: Oct 2008
Location: Kansas, MO
Posts: 9

Rep: Reputation: 0
Question writing to a file


I am looking for help on writing to a text file:

Lets say i have a file name text.text and i want to echo out "enter name"
then echo "enter age". The collected information i would like to append it to a file that is created when the function is called:

function addText() {

name=$1
age=$2

folder="$HOME/dir/"

fileName=${folder}dir${age}.txt
tempFile=${folder}films${name}Temp.txt

echo "$name" >> $fileName

}

input:
Enter Name: Raymond
Enter Age: 20

next input:

enter name: ray
enter age: 22


the Output inside the file should look like:

"Raymond", "20"
"ray", "22"

... etc.
 
Old 11-12-2008, 11:39 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
ravi2ray

#!/bin/bash
folder="$HOME/dir"
fileName=${folder}/text.text
read -p "Name " name
read -p "Age " age
echo "\"$name\",\"$age\"" >>${fileName}

Last edited by /bin/bash; 11-12-2008 at 11:57 AM. Reason: Typo
 
Old 11-13-2008, 02:00 PM   #3
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
If you are doing a lot of lines all at once... it is best to do the following

Code:
MYVAR=123

# use > to write over the whole file
# use >> if you want to append to the end

cat > file_name << EOF_ANYTHING_GOES_HERE
this is all text treated
as if it was quoted with ""
you can use variables such as \$MYVAR=$MYVAR
and you can use commands like: `date`

To end the file, place a matching phrase from after the << right up against the
left side.  DO NOT INDENT! that requires <<-SOMETHING_ELSE

EOF_ANYTHING_GOES_HERE
I don't have a linux box with me at work... BUT that will get you headed in the right direction.
the ` mark on `date` is the mark from the tilde~ key

I use this method when i need to build a dynamic menu for blackbox/openbox
 
Old 11-13-2008, 02:41 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Generally, you do not want to tack something like this onto an existing thread---particularly an old one. I'm splitting this out to a separate thread.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing /etc files to a new file strello Linux - Newbie 4 11-19-2006 06:47 PM
Ruby file writing NSKL Programming 0 10-30-2005 07:15 AM
PHP file writing benrose111488 Programming 6 07-01-2005 03:57 PM
writing to file ej25 Programming 17 11-19-2004 11:23 AM
writing a batch file ???? Micro Linux - General 3 01-09-2003 11:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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