LinuxQuestions.org
Visit Jeremy's Blog.
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 01-12-2010, 11:01 PM   #1
dagummit
LQ Newbie
 
Registered: Sep 2009
Posts: 26

Rep: Reputation: 0
bash scripting... taking user input and adding it to a particular area of a file...


in bash scripting...say I want to take the input from a user via a question...I would do this:

Quote:
#!/bin/bash

echo "How large do you want this partition to be in GB (enter only the number)?"
read PART_SIZE
echo "You want your partition to be $PART_SIZE GB"
But I don't want to echo it back to the screen, I want to add it to the content of /etc/fstab. I have been mucking around with sed to find the tmpfs partition in /etc/fstab and add the partition size attribute (this is to use the onboard RAM as a volatile partition)...but am not having any luck...

The portion of /etc/fstab that uses /dev/shm for the tmpfs partition is:

tmpfs /dev/shm tmpfs defaults 0 0

So, if a user says "24" GB to the answer (from above), how do I get it to automatically add that value to the tmpfs partition line in /etc/fstab? So it would look like:

tmpfs /dev/shm tmpfs size=24g,defaults 0 0

I understand that I would also have to come up with a way to put "size=XXg", which I could do with a copied over generic file before this action...then the script would have to find "XX" and replace it with the user's figure...

Anyone have any ideas?
 
Old 01-12-2010, 11:08 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
var="size=${PART_SIZE}g"

sed "/s/\(^tmpfs.*\)\(size=.*,)\(.*$\)/\1$var,\3/" /etc/fstab
If I understand you right, the above code (untested) should do what you want.. I'll test it momentarily.

Sasha

Last edited by GrapefruiTgirl; 01-12-2010 at 11:17 PM. Reason: added filename at end.
 
Old 01-12-2010, 11:13 PM   #3
dagummit
LQ Newbie
 
Registered: Sep 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
var="size=$PART_SIZEg"

sed "/s/\(^tmpfs.*\)\(size=.*,)\(.*$\)/\1$var,\3/" /etc/fstab
If I understand you right, the above code (untested) should do what you want.. I'll test it momentarily.

Sasha
thanks!

I probably should have stated that I am somewhat of a n00b in scripting...but I will try this solution out... again, thanks!
 
Old 01-12-2010, 11:14 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
sed -i "s/\(^tmpfs.*\)\(size=.*,\)\(.*$\)/\1$var,\3/" testfile
Oops.. Had a few typos/omissions... The above now works for me though. Test it on a phony fstab file first though and then replace "testfile" with /etc/fstab".

NOTE: I also edited my first post, and put the {braces} around the variable; I neglected to do that the first time, which would have led to the variable being empty, because it would have thought the 'g' was part of the variable name. My apology.
Sasha

Last edited by GrapefruiTgirl; 01-12-2010 at 11:18 PM.
 
1 members found this post helpful.
Old 01-12-2010, 11:46 PM   #5
dagummit
LQ Newbie
 
Registered: Sep 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
sed -i "s/\(^tmpfs.*\)\(size=.*,\)\(.*$\)/\1$var,\3/" testfile
Oops.. Had a few typos/omissions... The above now works for me though. Test it on a phony fstab file first though and then replace "testfile" with /etc/fstab".

NOTE: I also edited my first post, and put the {braces} around the variable; I neglected to do that the first time, which would have led to the variable being empty, because it would have thought the 'g' was part of the variable name. My apology.
Sasha
you are more than money! Thanks soooo much! It works perfectly!
 
Old 01-13-2010, 12:01 AM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Cool! Glad it helped. Now, it's up to you to learn what exactly the command is doing so that you can do stuff like this with other variables, or other replacements, etc.
The sed man page doesn't list even a fraction of what sed can do, but the sed documentation and lots of tutorials can be found easily on the net.

Cheers!
Sasha
 
Old 01-13-2010, 12:04 AM   #7
dagummit
LQ Newbie
 
Registered: Sep 2009
Posts: 26

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GrapefruiTgirl View Post
Cool! Glad it helped. Now, it's up to you to learn what exactly the command is doing so that you can do stuff like this with other variables, or other replacements, etc.
The sed man page doesn't list even a fraction of what sed can do, but the sed documentation and lots of tutorials can be found easily on the net.

Cheers!
Sasha
Yes, I have been toying around with sed and awk... I have a helper book on them... But things like this problem really show me the in's and out's...real world examples that you can't learn from a book. Thanks again!
 
Old 01-13-2010, 12:07 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Like someone's signature around here reads: "Sed & Awk -- never leave home without them!"

Best of success in learning them; there's a lot to them, but one example at a time is the way to go. Soon, you'll see it isn't too hard to go from a short simple sed or awk function, to a longer more complex one, because it's really just 'more of the same'.

PS - you'll notice I used double-quotes around the sed function. The reason is so that the $variable would be properly interpreted as a shell variable. With single quotes, it would have been taken literally. Usually though, single quotes are used around sed statements.

You're welcome and best of success.
Sasha

Last edited by GrapefruiTgirl; 01-13-2010 at 12:09 AM.
 
  


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
Taking User input and storing values in Hash in perl kdelover Programming 3 09-22-2009 02:02 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Bash scripting: redirecting input to a command hal8000b Programming 2 12-10-2007 07:29 AM
[ruby with emacs] taking user input lilili Programming 2 01-18-2007 12:17 PM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM

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

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