-1

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.

Jean
  • 21,665
  • 24
  • 69
  • 119
  • 1
    Your question is getting close votes because it is "too broad". Instead of asking for us to write the entire solution, you should do your best to code it yourself and, if you can't get it working, then post what you have written here and ask for help to fix it. – Borodin Sep 07 '17 at 20:42
  • My attempt so far is a disaster. I have added it with the question. – Jean Sep 07 '17 at 21:02
  • Thank you. I think you need `if [ $umask & 070 -ne 0 ]` or just `if [ $umask & 070 ]`. It's not hard to find with a quick google. – Borodin Sep 07 '17 at 21:06
  • `./test.sh: line 6: [: missing ] ./test.sh: line 6: 070: command not found` was what I got when I tried that earlier – Jean Sep 07 '17 at 21:21
  • You're missing a space somewhere. You need to copy what I wrote. – Borodin Sep 07 '17 at 21:26
  • I don't think so. Here's a fiddle http://www.tutorialspoint.com/execute_bash_online.php?PID=0Bw_CjBb95KQMV2tPMzJ4NVRmUlU – Jean Sep 07 '17 at 21:40
  • You are using the wrong shebang; should be `/bin/bash` (or something with `bash`), not `/bin/sh`. – chepner Sep 07 '17 at 21:49
  • `0970:dir> ls -lrt /bin/sh lrwxrwxrwx. 1 root root 4 Oct 15 2016 /bin/sh -> bash` How about now? – Jean Sep 07 '17 at 22:03
  • Thank you for adding code ("disaster" or not :). I've removed my close vote. – zdim Sep 07 '17 at 22:56
  • @Jean, When `bash` is invoked as using `sh`, it tries to emulate `sh`. – ikegami Sep 08 '17 at 01:07
  • @Boridin, `if [ $umask & ...` means run `if [ $umask` in the background, then execute `...` as a separate command. – ikegami Sep 08 '17 at 01:11

1 Answers1

4

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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352