Find the answer to your Linux question:
Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By watael
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.
  1. #1
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664

    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?

    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
    It does fine when /tmp does not already have nodev, but on second run results in:

    Code:
    /dev/VolGroup00/LogVol02        /tmp    ext3    nodev,nodev,defaults    1       2
    Last edited by Mudgen; 11-01-2012 at 05:42 PM.

  2. #2
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664

    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

  3. #3
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    hi,

    variable expansion does not occur between / and /
    see
    Code:
    $ 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
    $
    Mudgen likes this.

  4. #4
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    Quote Originally Posted by watael View Post
    hi,

    variable expansion does not occur between / and /
    see
    Code:
    $ 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
    $
    Thank you, Watael. That's generally useful to know, but can you help me understand how to apply it to my quoted code and need to add a second test? Or a workaround?
    Last edited by Mudgen; 11-01-2012 at 09:31 PM. Reason: oops

  5. #5
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    211
    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

  6. #6
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    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.

    Code:
    awk -v FSYS=$FSYS -v FSOPT=$FSOPT -v FSOPTCOMMA=$FSOPTCOMMA ' $4 !~ FSOPT && $2~FSYS{$4=FSOPTCOMMA$4}1' OFS="\t"   < /etc/fstab
    This does what I want (with the shell variables previously set).
    Last edited by Mudgen; 11-01-2012 at 09:54 PM. Reason: snark

Posting Permissions

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