Results 1 to 6 of 6
I need to script some /etc/fstab option changes for security standard compliance and am having a problem with matching one field (mount point) and excluding a no-match on another (i.e. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-01-2012 #1
awk action when matching one field and not matching another
I need to script some /etc/fstab option changes for security standard compliance and am having a problem with matching one field (mount point) and excluding a no-match on another (i.e. options already contain the one I'm adding). I can't seem to make the no-match work in conjunction with the match.
The following adds my option whether or not it's already present. I've tried both the ~ && and the !~ || variations (and a bunch of others).
This code keeps adding "nodev" when it's already there. What am I not getting?
It does fine when /tmp does not already have nodev, but on second run results in:Code:#!/bin/bash ## Open /etc/fstab as input to command line, replace it within braces with modified copy FSOPT="nodev" FSOPTCOMMA="$FSOPT," FSYS="^/tmp$" #syntax ok, does not exclude $4!~/FSOPT/ ( rm /etc/fstab && awk -v FSYS=$FSYS -v FSOPT=$FSOPT -v FSOPTCOMMA=$FSOPTCOMMA ' $4 !~ /FSOPT/ && $2~FSYS{$4=FSOPTCOMMA$4}1' OFS="\t" > /etc/fstab ) < /etc/fstab
Code:/dev/VolGroup00/LogVol02 /tmp ext3 nodev,nodev,defaults 1 2
Last edited by Mudgen; 11-01-2012 at 05:42 PM.
- 11-01-2012 #2
Simplified problem statement
The tl;dr version:
This awk code outputs all fstab lines, prepending "nodev" to $4 if $2 matches "^/tmp$". I want to add "nodev" only if it is not already in $4. How do I do that?
Code:awk -v FSYS="^/tmp$" -v FSOPT="nodev" -v FSOPTCOMMA="nodev," ' $2~FSYS{$4=FSOPTCOMMA$4}1' OFS="\t" < /etc/fstab
- 11-01-2012 #3Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
hi,
variable expansion does not occur between / and /
seeCode:$ awk -v var="bar" '$1 !~ /var/{ print}' <<<"foo bar baz" foo bar baz $ awk -v var="bar" '$1 !~ var{ print}' <<<"foo bar baz" foo baz $
- 11-01-2012 #4
- 11-01-2012 #5Linux Newbie
- Join Date
- Nov 2012
- Posts
- 134
is that so difficult?
look at another example:Code:$ awk -v var1="o" -v var2="a" '$2 ~ var1 && $1 !~ var2{ print}' <<<"foo bar bar baz baz foo foo foo" foo foo
- 11-01-2012 #6
Ah. Took me a moment to get it. I thought there was something wrong with the way I was performing the tests in the chain. It was the pattern matching.
This does what I want (with the shell variables previously set).Code:awk -v FSYS=$FSYS -v FSOPT=$FSOPT -v FSOPTCOMMA=$FSOPTCOMMA ' $4 !~ FSOPT && $2~FSYS{$4=FSOPTCOMMA$4}1' OFS="\t" < /etc/fstabLast edited by Mudgen; 11-01-2012 at 09:54 PM. Reason: snark


1Likes
Reply With Quote

