Solved

Need to convert sed expression to a similar awk expression

Posted on 2012-04-12
3
570 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Guacamole and browser performance 1 65
Linux Mint 18 31 78
Fixing Non-Standard characters in text 8 93
Adjust the position 3 50
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

864 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

20 Experts available now in Live!

Get 1:1 Help Now