write and append to a file using shell programming
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.