简体   繁体   中英

Checking the umask in shell script

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM