Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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. ...
  1. #1
    Just 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.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    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.

  3. #3
    Just 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

  4. #4
    Linux 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:

    Code:
    sed -i '15iText' /etc/fstab
    As GNU-Fan said, always make a backup before you change important system files.

  5. #5
    Just Joined!
    Join Date
    Sep 2008
    Posts
    26
    Quote Originally Posted by vsemaska View Post
    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:

    Code:
    sed -i '15iText' /etc/fstab
    As GNU-Fan said, always make a backup before you change important system files.
    thanks so much Vsemaska. that worked perfect!

  6. #6
    Just 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?

  7. #7
    Linux 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

  8. #8
    Linux 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

  9. #9
    Just 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?

  10. #10
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Put a carrot (^) at the beginning of the regular expression. It indicates the start of a line.

    Code:
    sed -i 's|\(^tools:/mnt/monster\)|#\1|' /etc/fstab
    That way a line that starts with a # won't match.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...