eNgE
eNgE

Reputation: 1

Testing if the directory of a file is writable in bash script

I'm trying to check to see if the directory of a file given is writable or not.

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

I'm pretty sure I'm messing things up in the test as far as formatting the path that comes from pathDIR but I can't figure out what to do there.

Upvotes: 0

Views: 4426

Answers (1)

pedram shabani
pedram shabani

Reputation: 1680

on if statement you must us $fullPATH

if [ ! -w "$fullPATH" ]

try it

    # /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

Upvotes: 1

Related Questions