Results 1 to 10 of 18
Not sure where to begin but i need a script that will automatically add this line:
"tools:/mnt/spool /mnt/spool nfs intr 0 0"
to "fstab" instead of using the vi editor. ...
- 09-10-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
need help with using script to edit lines in fstab
Not sure where to begin but i need a script that will automatically add this line:
"tools:/mnt/spool /mnt/spool nfs intr 0 0"
to "fstab" instead of using the vi editor. Please advice, thanks.
- 09-10-2008 #2
You can append lines to files with the >> operator.
echo "new line" >> myfile
Make a backup of such an important system file, though.Debian GNU/Linux -- You know you want it.
- 09-11-2008 #3Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
is there a way to use the "sed" command so i can apply this line anywhere in the fstab file? for example heres how my fstab looks.... look for the red text and thats where i want the new line to be. how should my script look to achieve this?
# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/dev/mapper/main-root / reiserfs defaults 0 1
/dev/sda1 /boot ext3 defaults 0 2
/dev/sda3 none swap sw 0 0
/dev/sdb2 none swap sw 0 0
/dev/hda /media/cdrom0 udf,iso9660 user,noauto 0 0
#NFS mounts
users:/mnt/users /users nfs intr 0 0
tools:/mnt/tools /studio/tools nfs intr 0 0
tools:/mnt/hdpsbin /mnt/hdpsbin nfs intr 0 0
"tools:/mnt/spool /mnt/spool nfs intr 0 0"
tools:/mnt/monster /nfs/shared nfs intr 0 0
grid:/mnt/grid /usr/sge nfs intr 0 0
- 09-11-2008 #4Linux User
- Join Date
- Jun 2007
- Posts
- 318
Well if you know which line no. you want the line to be at you can do the following. In your example, if I counted correctly, you want the line at line 15. So the command would be:
As GNU-Fan said, always make a backup before you change important system files.Code:sed -i '15iText' /etc/fstab
- 09-11-2008 #5Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
- 09-11-2008 #6Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
if i run "sed -i 'line number i text' /etc/fstab" it automatically moves whatever existing line thats already there one line down and inserts the new text.
so how do i add a "#" sign in front of an existing line with out moving the original line at all?
- 09-12-2008 #7Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
the old line becomes line 16, so do a simple sed substitution:
sed -i '16 s/^/#/' /etc/fstab
a safer way to do it is via pattern instead of line number,
sed -i 's|\(tools:/mnt/monster\)|#\1|' /etc/fstab
- 09-12-2008 #8Linux User
- Join Date
- Jun 2007
- Posts
- 318
secondmouse brings up a good point. Back to the insert line question you could insert the line by searching for the line you want to insert before with this command:
Code:sed -i '/tools:\/mnt\/monster/' iText' /etc/fstab
- 09-12-2008 #9Just Joined!
- Join Date
- Sep 2008
- Posts
- 26
Right, searching for pattern would be lot more acurate. Good call.
Theres one more thing I want to add to this. If the "#" sign already exist on that particular line in my file. How would I script this so it doesn't add an extra "#" sign?
- 09-12-2008 #10Linux User
- Join Date
- Jun 2007
- Posts
- 318
Put a carrot (^) at the beginning of the regular expression. It indicates the start of a line.
That way a line that starts with a # won't match.Code:sed -i 's|\(^tools:/mnt/monster\)|#\1|' /etc/fstab


Reply With Quote
