LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-23-2008, 04:55 PM   #1
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Rep: Reputation: 0
Bash - is it possible to write to memory rather than a file


... and then read the memory back as if it were a file i.e. line by line?
 
Old 09-23-2008, 05:13 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You could use a ramdisk, or if you don't want to use fread and friends, why not use shared memory?
 
Old 09-23-2008, 09:04 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by matthewg42 View Post
You could use a ramdisk, or if you don't want to use fread and friends, why not use shared memory?
Just write to /dev/shm, you can put files there and then read them or do whatever.
 
Old 09-23-2008, 09:57 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
If you simply save in a variable, does that accomplish the same thing?

What problem are you trying to solve---ie why do you need to do this?
 
Old 09-24-2008, 07:17 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by pixellany View Post
If you simply save in a variable, does that accomplish the same thing?

What problem are you trying to solve---ie why do you need to do this?
I don't think that variables are the best way to achieve that, since he mentioned needing multiple lines. Remember that the space for environment variables is far more finite than the available virtual memory, and the available virtual memory is the only limit for /dev/shm. By default, half of the memory is the limit, but it can be specified via a mount option for tmpfs.
 
Old 09-24-2008, 08:13 AM   #6
basildon
LQ Newbie
 
Registered: Sep 2008
Posts: 12

Original Poster
Rep: Reputation: 0
That sounds excellent

Could you please give me a couple of lines of code that show how to implement this i.e. write a few lines and then read them ?
 
Old 09-24-2008, 10:44 AM   #7
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Code:
bash-3.1$ echo oaneuthnsoathue > /dev/shm/new
bash-3.1$ cat /dev/shm/new
oaneuthnsoathue
 
Old 09-24-2008, 12:07 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by basildon View Post
That sounds excellent

Could you please give me a couple of lines of code that show how to implement this i.e. write a few lines and then read them ?
If you intend to so something that's going to run for a few users (or a lot of them) concurrently, it might be a good idea to write this way:

Code:
my_shm_file="/dev/shm/$USER-$0"

echo "whatever" >> "$my_shm_file"
That's the basic thing, it all depends on what do you exactly need to do. If you need a fifo instead of a regular file, you can create it as well. With a fifo, the contents will dissapear from the file as it's consumed by any program. A random link illustrating it:

http://www.linuxjournal.com/article/2156
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 01:44 PM
C Program to write to the memory kushalkoolwal Programming 3 05-30-2008 05:11 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
best way to write a dynamic conf file in bash dkrysak Linux - General 6 02-13-2007 12:49 PM


All times are GMT -5. The time now is 09:45 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration