如何为目录和退出位置设置 ummask



我正在编写一个脚本,该脚本正在使用以下命令创建要使用的目录mktemp -d

我必须向以上述方式创建的目录添加一个掩码。我必须将其添加到代码的退出条件中。

下面是示例代码:

DIR1=$(mktemp -d)
wget_output=$(wget -q -P "$DIR1" "$CERT1")
if [ $? -ne 0 ]; then
echo "Certificates NOT Found OR Saving the certificates in directory failed."
exit
fi

怎么办?

更好的答案可能是外壳,因为掩码保持在外壳级别。

$SHELL -c "umask $mask; mktemp -d"

这可确保当前脚本退出时不会修改当前 umask。

我已经找到了解决方案。

在文件记录的开头 原始掩码

umask=$(umask)

然后在创建目录之前设置该值。

umask 077
DIR1=$(mktemp -d)
wget_output=$(wget -q -P "$DIR1" "$CERT1")
if [ $? -ne 0 ]; then
echo "Certificates NOT Found OR Saving the certificates in directory failed."
exit
fi

然后最后使用

umask ${umask}

因此,更新后的代码变为:

umask=$(umask) #record umask
.
.
.
umask 077 # set umask value
DIR1=$(mktemp -d)
wget_output=$(wget -q -P "$DIR1" "$CERT1")
if [ $? -ne 0 ]; then
echo "Certificates NOT Found OR Saving the certificates in directory failed."
exit
fi
umask ${umask} # restore umask

你可以试试这个:

umask 0700
DIR1="$(mktemp -d)"

将 DIR1 的权限设置为 0700 (drwx------(

最新更新