测试文件目录是否可以在bash脚本中写入

时间:2018-04-01 19:48:38

标签: bash shell

我试图查看给定文件的目录是否可写。

fullPATH=$(realpath "$1")
echo "realpath => $fullPATH"
pathDIR=${fullPATH%/*}
echo "pathDIR => $pathDIR"

if [ ! -w "$pathDIR" ]; then
    echo $1 is a file in a directory that is not writeable.
#   return 1
else
    echo writable
fi

我非常确定我在测试中搞砸了格式化来自pathDIR的路径,但我无法弄清楚该做什么。

1 个答案:

答案 0 :(得分:0)

on if statement你必须我们$ fullPATH

if [ ! -w "$fullPATH" ]

试试吧

    # /bin/bash
fullPATH=$(realpath "$1")
echo "realpath => $fullPATH"
pathDIR=${fullPATH%/*}
echo "pathDIR => $fullPATH"

if [ ! -w "$fullPATH" ]
 then
    echo $1 is a file in a directory that is not writeable.
#   return 1
else
    echo writable
fi