Solved

Need to convert sed expression to a similar awk expression

Posted on 2012-04-12
3
568 Views
Last Modified: 2012-04-19
I am trying to re-engineer the following function in a script so that it can use awk to search for the /home mount in fstab and comment out the line. The reason being the method for mounting the /home directory can vary... eg, UUID, LVM path, Label... etc

So basically I just need the first line below changed so that it uses awk to search for /home instead of using sed to un-comment the /dev/mapper/os_vg-home line

function setupautofs {
if [ $DEV ]; then
	umount /home
	sed -i.orig -e 's/^\/dev\/mapper\/os_vg-home/#\/dev\/mapper\/os_vg-home/' /etc/fstab
	sed -i.orig -e '/+auto.master/i\/home     /etc/auto.home' /etc/auto.master
	echo "*     -_netdev,nosuid,tcp,hard,bg,intr,rsize=32768,wsize=32768     alanetapp001b.choicepoint.net:/vol/home/&" >> /etc/auto.home
	chkconfig autofs on
	service autofs restart
fi
}

Open in new window


Here is the fstab if needed:

UUID=1d086e6d-83f0-4a9c-984f-186dc80eda3f /boot                   ext4    defaults        1 2
/dev/mapper/os_vg-home  /home                   ext4    defaults        1 2
/dev/mapper/os_vg-tmp   /tmp                    ext4    defaults        1 2
/dev/mapper/os_vg-var   /var                    ext4    defaults        1 2
/dev/mapper/os_vg-swap  swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

Open in new window

0
Comment
Question by:Thaidog
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37837156
awk cannot modify files "in place" as sed can do with the "-i" flag and thus can't save a backup copy either.

So you will have to redirect awk's output to a temporary file to then move this file back to the original.

The awk statement could look like this:

awk '{if($0~/^\/dev\/mapper\/os_vg-home/) print "#" $0; else print $0}' /etc/fstab > /tmp/fstab.$$ && mv /tmp/fstab.$$ /etc/fstab

Replace "mv" with "cp" to keep a backup copy of fstab in /tmp, suffixed with the current PID of your script.

wmp
0
 

Author Comment

by:Thaidog
ID: 37850768
This seems to do what I need:

awk '$2 == "/home"{$0 = "#"$0}{print > FILENAME}' /etc/fstab
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37850845
Are you sure?

I think you'll end up in an infinite loop appending lines to fstab until the FS is full.

This is an alternative (but possibly dangerous - make a copy of /etc/fstab first!):

( rm /etc/fstab && awk '$2 == "/home"{$0 = "#"$0} {print}' >/etc/fstab ) </etc/fstab

or shorter

( rm /etc/fstab && awk '$2 == "/home"{$0 = "#"$0}1' >/etc/fstab ) </etc/fstab



wmp
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now