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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
01-12-2010, 11:01 PM
|
#1
|
LQ Newbie
Registered: Sep 2009
Posts: 26
Rep:
|
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?
|
|
|
01-12-2010, 11:08 PM
|
#2
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
01-12-2010, 11:13 PM
|
#3
|
LQ Newbie
Registered: Sep 2009
Posts: 26
Original Poster
Rep:
|
Quote:
Originally Posted by GrapefruiTgirl
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!
|
|
|
01-12-2010, 11:14 PM
|
#4
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
01-12-2010, 11:46 PM
|
#5
|
LQ Newbie
Registered: Sep 2009
Posts: 26
Original Poster
Rep:
|
Quote:
Originally Posted by GrapefruiTgirl
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!
|
|
|
01-13-2010, 12:01 AM
|
#6
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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
|
|
|
01-13-2010, 12:04 AM
|
#7
|
LQ Newbie
Registered: Sep 2009
Posts: 26
Original Poster
Rep:
|
Quote:
Originally Posted by GrapefruiTgirl
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!
|
|
|
01-13-2010, 12:07 AM
|
#8
|
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
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.
|
|
|
All times are GMT -5. The time now is 05:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|