bashshell

Set sticky bit permission using bash script


Can anyone guide, how to implement this linux command into a bash script

df --local -P | awk {'if (NR!=1) print $6'} | xargs -I '{}' find '{}' -xdev -type d -perm -0002 2>/dev/null | xargs chmod a+t

Solution

  • Simply

    Put this in a file, add a shebang on 1st line:

    #!/bin/sh
    
    df --local -P |
        awk '{if (NR!=1) print $6}' |
        xargs -I '{}' find '{}' -xdev -type d -perm -0002 2>/dev/null |
        xargs --no-run-if-empty chmod a+t
    

    But as this question is tagged , I would write something like this:

    #!/bin/bash
    
    { read foo; mapfile -t mpoints;} < <(df --local -P)
    TEXTDOMAIN=libc
    exec 2> >(exec grep -v '^find: .*'$"Permission denied"\$ >&2)
    find "${mpoints[@]#*% }" -xdev -type d -perm -0002 -exec chmod a+t {} +
    

    Short, quick and efficient!

    Explanation: