Results 1 to 5 of 5
Hello...
in bash scripting...say I want to take the input from a user via a question...I would do this:
#!/bin/bash
echo "How large do you want this partition to be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-13-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 6
bash scripting... taking user input and adding it to a particular area of a file...
Hello...
in bash scripting...say I want to take the input from a user via a question...I would do this:
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...#!/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"
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-13-2010 #2
I'm not quite sure why anyone would want to do this, as you can mount a tmpfs partition straight from the commandline, without involving /etc/fstab.
umount tmpfs
mount tmpfs -t tmpfs -o size=1G /dev/shm.
if you MUST edit your /etc/fstab, you could do
cat /etc/fstab | awk '!/dev\/shm/ {print $0}' > /tmp/fstab && echo "tmpfs /dev/shm tmpfs size=${PART_SIZE}g,defaults 0 0" >> /tmp/fstab && mv /tmp/fstab /etc/fstab
But again, WHY would you want to do this. The fstab is a critical system file, I would almost NEVER use a script to mess with it, ESPECIALLY when you have unverified user input.Last edited by meton_magis; 01-13-2010 at 05:26 AM. Reason: I need to learn to spell ><
New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 01-13-2010 #3Just Joined!
- Join Date
- Jan 2010
- Posts
- 6
I am doing this to help me. I build systems up and harden them. Some of them have to be built on CF devices which the requirement is to have them RO, so instead of recreating the wheel every time, I like to make scripts that do the work for me. Some of the systems have different amounts of memory, so a nice little set of code in a script would help in setting that.
And I know fstab is a critical file... Trust me, I have fat fingered things in manually editing it before...hence all the better reason to automate it.
- 01-14-2010 #4
CF ??? RO ???
It's usually best not to use abbreviations on forums until you've established what they mean, or they are VERY common, and in a context to instantly make them understood. Even then, it's better to spell them out.
I'm sure you have reasons for wanting to automate this, but the way you wrote your code made it seem like you were writing a script for other people to use. No matter how much you try, this will never be secure. Other people won't know your script, and they won't bother to open it to learn how it works. What happens if they put in bad data that borks the file? It's gonna be you that they call to fix the "broken script" that you wrote. No matter how good you think you made a script, an idiot user will find a way to make it mess something up.New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 01-15-2010 #5
Alrighty. So you're doing this to automate the modification of an fstab file. If this is soley for your personal use, btw, I might suggest taking in the size as a commandline argument instead of asking for it in the script, as this allows for slightly better automation, as well as things like crontabbing and running the script when user input is not available.
So we have an existing fstab and we want to modify a line in it. The line to modify is a line beginning with "tmpfs". This seems like a job for awk!
Let's consider this command:
This is a code that I ran on my test_fstab file. What it does is to check the first field of every line to see if it is "tmpfs". If it is, then it prepends "size=24g" to the fourth field. Then the line (no matter what line it is) gets printed. So let's see how this worked:Code:awk '{if($1 == "tmpfs") {$4 = "size=24g,"$4} print}' test_fstab
As we see, the command worked!Code:alex@danu:~/test$ cat test_fstab # /etc/fstab: static file system information. # # Use 'blkid -o value -s UUID' to print the universally unique identifier # for a device; this may be used with UUID= as a more robust way to name # devices that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc defaults 0 0 # / was on /dev/sda1 during installation UUID=ed2b251c-240d-4c7e-b0f8-5d18e328d1a9 / ext4 errors=remount-ro 0 1 # swap was on /dev/sdb1 during installation UUID=3ab42edb-f706-48a9-929c-d576826afdcc none swap sw 0 0 /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0 /dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0 tmpfs /dev/shm tmpfs defaults 0 0 alex@danu:~/test$ awk '{if($1 == "tmpfs") {$4 = "size=24g,"$4} print}' test_fstab # /etc/fstab: static file system information. # # Use 'blkid -o value -s UUID' to print the universally unique identifier # for a device; this may be used with UUID= as a more robust way to name # devices that works even if disks are added and removed. See fstab(5). # # <file system> <mount point> <type> <options> <dump> <pass> proc /proc proc defaults 0 0 # / was on /dev/sda1 during installation UUID=ed2b251c-240d-4c7e-b0f8-5d18e328d1a9 / ext4 errors=remount-ro 0 1 # swap was on /dev/sdb1 during installation UUID=3ab42edb-f706-48a9-929c-d576826afdcc none swap sw 0 0 /dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0 /dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0 tmpfs /dev/shm tmpfs size=24g,defaults 0 0
Now, because you're going to be splicing in your Bash variables into an awk script, you're going to have to do some crazy quote-manipulation. However, I think that I have successfully demonstrated the concept, so I hope you can figure out how to do it.
Does this help at all?
Beyond this, meton_magis is correct in that you should do some data verification. Even if it's only for your usage, make sure that the size parameter is valid and what you expect, even if it's just to avoid a slipped keystroke. You don't want to run into any problems because of a malformatted fstab, right?
It seems to me that a simple check that the size variable is a number followed optionally by a "k", "m", or "g" is a fairly reasonable check (assuming that this is valid for the size parameter; I actually don't personally know).
Good luck! Feel free to ask some more questions.


Reply With Quote

