LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   write and append to a file using shell programming (https://www.linuxquestions.org/questions/programming-9/write-and-append-to-a-file-using-shell-programming-464193/)

christina_rules 07-15-2006 12:13 AM

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

nadroj 07-15-2006 12:17 AM

you can send output to a file with '>' and append to the end of a file with '>>'

urzumph 07-15-2006 12:18 AM

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.

christina_rules 07-15-2006 11:43 PM

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

spooon 07-16-2006 12:22 AM

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


christina_rules 07-16-2006 07:00 AM

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.


All times are GMT -5. The time now is 09:23 AM.