0

I have an unknown path given by the user where I would create a file. Since I don't have write permissions I need to find the first existing dir in the path going from inside to outside and check for write permissions.

For instance foo/moo/doo

I've tried

for d in "$dirpath"/ ; do
    "dir=$d"
done

But it doesn't seems to work

So I must loop trough the path whether it's absolute or relative check each node whether it exist and if it's a directory indeed and if so return it

or if not return

  • perhaps top directory if the path is absolute ( not sure if there's always a concrete dir in unix on top of absolute path)
  • Current directory to the relative path

Any ideas appreciate your kind help

12
  • "dir=$d" does not do what you expect. you need to quote just the value, not the entire assignment.
    – l0b0
    Commented Sep 28, 2018 at 20:45
  • @l0b0 how do I get just the value?
    – Jocky Doe
    Commented Sep 28, 2018 at 20:49
  • 1
    I don't know what you mean. Could you please update your question to include an example?
    – l0b0
    Commented Sep 28, 2018 at 20:50
  • 1
    "to create the rest non existing" – see mkdir -p. Commented Sep 28, 2018 at 21:05
  • 1
    If you don't have write permissions, you can't "create the rest non existing" anyway, can you? Maybe unless the non-writable directory is empty and you can remove it; or it's your directory and you can give yourself permissions. If I were you, I would KISS. If mkdir -p returns "permission denied", assume there's a reason the permissions are what they are and the best your script can do is throw an error; then you investigate by hand. Any cumbersome script logic may backfire. Commented Sep 28, 2018 at 21:23

2 Answers 2

1

Just a quick solution

#!/bin/bash

dir=$(realpath "$1")
stop=no
while [ $stop = no ] ; do
    if touch "$dir/this.$$" ; then
        rm "$dir/this.$$"
        echo "You can create in $dir!"
        stop=yes
    else
        dir=${dir%/*}
        if [ "$dir" = "" ] ; then
            echo "You are not allowed to write anywhere."
            stop=yes
        fi
    fi
done
2
  • 10x your answered helped me to make a function that does the job
    – Jocky Doe
    Commented Oct 6, 2018 at 16:37
  • If this solved your problem, please mark the solution as such. Commented Oct 6, 2018 at 17:14
0

Here's a function that finds the first existing directory in a path starting from the deepest location in the path.

function findConcreteDirInPath() {
  local dirpath="$1"
  local stop="no"
  while [ $stop = "no" ] ; do
    if [ -d "$dirpath" ]; then
      local stop="yes"
    else
      local dirpath=$(dirname "$dirpath")
      if [ "$dirpath" = "" ] ; then
        local stop="yes"
        exit 1;
      fi
    fi
  done

  echo "$dirpath"
}

Here's an usage example

aPath="/var/doo/moo"
concreteDir=$(findConcreteDirInPath $aPath)

if [ $concreteDir != "." ]; then
  echo -e "First concrete dir in \"$aPath\" path is: \"$concreteDir\""
  # Check whether current user have write permissions for the directory
  if [ -w $concreteDir ]; then
    echo -e "\"$(whoami)\" user can write in \"$concreteDir\""
  else
    echo  -e "\"$(whoami)\" user can NOT write in \"$concreteDir\""
  fi
else
  echo -e "No concrete dir for the given \"$aPath\" path"
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .