LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash scripting... taking user input and adding it to a particular area of a file... (https://www.linuxquestions.org/questions/programming-9/bash-scripting-taking-user-input-and-adding-it-to-a-particular-area-of-a-file-781852/)

dagummit 01-12-2010 11:01 PM

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?

GrapefruiTgirl 01-12-2010 11:08 PM

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

dagummit 01-12-2010 11:13 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3824648)
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!

GrapefruiTgirl 01-12-2010 11:14 PM

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

dagummit 01-12-2010 11:46 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3824652)
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!

GrapefruiTgirl 01-13-2010 12:01 AM

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

dagummit 01-13-2010 12:04 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3824687)
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!

GrapefruiTgirl 01-13-2010 12:07 AM

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


All times are GMT -5. The time now is 11:06 PM.