Operating System - Linux
1753454 Members
6090 Online
108794 Solutions
New Discussion юеВ

Re: Need to put new entries in /etc/fstab w/o editing the fstab!!

 
Sreer
Valued Contributor

Need to put new entries in /etc/fstab w/o editing the fstab!!

Hello Gurus,

I have heavy load for adding new entries in fstab of more than 100 servers.

As usual I need to edit the /etc/fstab using vi and do the needed changes.

Is there any way to do this? So that I can create a script and to do the execution of that script till mounting.

I heard abt the use of echo command for appending entries in a file, but not sure more abt it.

my require ment is as below.

eg.
mkdir /tmp/test
cp -p /etc/fstab /etc/fstab.old
vi /etc/fstab ==> here I need the cmds for modifying the fstab!
mount -a
bdf /tmp/test
diff /etc/fstab /etc/fstab.old
date

---------

Here only editing fstab is problamatic ...
Your advice will more be appreciated

many thanks.

Rgds
Sreekumar

4 REPLIES 4
Jayakrishnan G Naik
Trusted Contributor

Re: Need to put new entries in /etc/fstab w/o editing the fstab!!


Hi Sreekumar,

You can use echo command and append the fstab file with the needed entry. put an example below.

echo "/dev/vg01/lv1 /tmp/test ext3 defaults 1 2" >> /etc/fstab
if you need to modify a perticular line, then initially discard that line and copy it to a new file

cat /etc/fstab|grep -v "uniq string" >> fstab.new
mv fstab.new fstab
echo "/dev/vg01/lv1 /tmp/test ext3 defaults 1 2" >> /etc/fstab

enter the correct entry needed at the end

Regards
Jayakrishnan G Naik
Matti_Kurkela
Honored Contributor

Re: Need to put new entries in /etc/fstab w/o editing the fstab!!

For example, if you're trying to mount a NFS share to /tmp/test, you could add a line to /etc/fstab in your script like this:

echo "server:/share /tmp/test nfs rw,hard,intr 0 0" >> /etc/fstab

If you're adding a local/SAN disk, you can do it the same way:

echo "/dev/vgSOME/lvolX /tmp/test vxfs delaylog 0 2" >> /etc/fstab

This will add the line to the end of /etc/fstab.

Be careful to use ">>" (not ">"), otherwise you'll overwrite /etc/fstab with your new line instead of appending to it.

MK
MK
Sreer
Valued Contributor

Re: Need to put new entries in /etc/fstab w/o editing the fstab!!

Thanks all.

The echo option worked.

Thx again.
Viktor Balogh
Honored Contributor

Re: Need to put new entries in /etc/fstab w/o editing the fstab!!

Hi,

If instead of adding a new line you need to modify an existing one, you can do it with the stream editor. For more info, see man 1 sed.

Regards,
Viktor
****
Unix operates with beer.