Checking the umask in shell script

1.2k views Asked by At

How do I check if the umask prevents the group bits from being set? My attempt:

#!/bin/sh

out=$(umask)
echo "$out"

if (($out & 070) != 0); then
    echo "$out"
    echo "Incorrect umask" > /dev/tty
    exit 1
fi

Output:

./test.sh: line 6: syntax error near unexpected token `!='
./test.sh: line 6: `if (($out & 070) != 0); then'

I'm ok with switching to bash if it makes things easier.

1

There are 1 answers

2
glenn jackman On BEST ANSWER

You need to use double parentheses to get an arithmetic evaluation. See https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

m=$(umask)
if (( ($m & 070) != 0 )); then 
    echo error
fi

Or you could treat the umask as a string and use glob-pattern matching:

if [[ $m == *0? ]]; then
    echo OK
else
    echo err
fi

bash has lots of unique syntax: it's not C/perl-like at all. Read (or at least refer to) the manual and read lots of questions here. Keep asking questions.