【问题标题】:Checking the umask in shell script检查 shell 脚本中的 umask
【发布时间】:2017-09-07 20:29:14
【问题描述】:

如何检查 umask 是否阻止设置组位?我的尝试:

#!/bin/sh

out=$(umask)
echo "$out"

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

输出:

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

如果它使事情变得更容易,我可以切换到 bash。

【问题讨论】:

  • 您的问题获得了接近投票,因为它“太广泛了”。与其要求我们编写整个解决方案,不如尽力自己编写代码,如果无法正常工作,请在此处发布您编写的内容并寻求帮助来修复它。
  • 到目前为止,我的尝试是一场灾难。我已经在问题中添加了它。
  • 谢谢。我认为您需要if [ $umask & 070 -ne 0 ] 或只是if [ $umask & 070 ]。用快速谷歌搜索并不难。
  • ./test.sh: line 6: [: missing ] ./test.sh: line 6: 070: command not found 是我之前尝试的结果
  • 您在某处缺少空格。你需要复制我写的内容。

标签: bash sh umask


【解决方案1】:

您需要使用双括号来获得算术评估。见https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs

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

或者您可以将 umask 视为字符串并使用 glob-pattern matching:

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

bash 有很多独特的语法:它根本不像 C/perl。阅读(或至少参考)手册并在此处阅读大量 问题。继续提问。

【讨论】:

猜你喜欢
  • 2018-08-05
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 2014-12-11
相关资源
最近更新 更多