LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2006, 12:13 AM   #1
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Rep: Reputation: 15
write and append to a file using shell programming


i recently wrote a program using shell programming. i prompted for entries but i don't how to write this entry to a file..if the file already exist..can i append to the existing file? i search for the linux commands but couldnt find one though..please help..thanks
 
Old 07-15-2006, 12:17 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
you can send output to a file with '>' and append to the end of a file with '>>'
 
Old 07-15-2006, 12:18 AM   #3
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Rep: Reputation: 30
try :
echo "$data" > file
or
echo "$data" >> file

where $data is the data you want to write, and file is the file you want to write it to. the first (with > ) will overwrite the file if it exists already, and the second (with >> ) will append if the file exists already.

I could give you more specific help if you showed me more of the source code you have already.
 
Old 07-15-2006, 11:43 PM   #4
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 15
ok...i wrote this

Quote:
echo "Enter the number of employees"
read number

for count in number
do
echo -e "\nEnter Name:"
read name
echo -e "\nEnter ID:"
read id
echo -e "\nEnter Basic Salary:"
read salary
echo -e "\nEnter Department:"
read dept

done
so does this mean i just add $name, $id, $salary, $dept > filename? and when i wanna next record i use >>?..but i want to ask if when i use >>, does it go to the next line automatically or do i have to indicate it? coz when i write data to a file in c++ i have to indicate the next line in my program.

thanks
 
Old 07-16-2006, 12:22 AM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by christina_rules
ok...i wrote this



so does this mean i just add $name, $id, $salary, $dept > filename? and when i wanna next record i use >>?..but i want to ask if when i use >>, does it go to the next line automatically or do i have to indicate it? coz when i write data to a file in c++ i have to indicate the next line in my program.

thanks
> deletes the contents of the file and writes the output of the command to the file. >> writes the output of the command to the end of the file. What it writes depends on what the command outputs. By convention, most programs in Unix (including echo) have a newline at the end of the input unless you tell it not to.

how about something like
Code:
echo "Enter the number of employees"
read number

for count in `seq $number`
do
echo "Enter Name:"
read name
echo "Enter ID:"
read id
echo "Enter Basic Salary:"
read salary
echo "Enter Department:"
read dept

echo $name $id $salary $dept >> filename

done
 
Old 07-16-2006, 07:00 AM   #6
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 15
ok..i'll try that out but what does "seq" means? why can't i just directly use number instead? sorry for troubling but i don't understand and i want to understand.
 
  


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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 05:08 PM
How to write a shell script to download a file via FTP? guarriman Linux - General 4 12-21-2004 09:31 AM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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