0

The main problem:

Write a script that determines a path's depth relative to the filesystem's root. The path is received as an argument on the command line. If no argument is received, the current working directory is considered as the path. If the argument is not a valid path, then an error message is printed to stderr and a non-zero exit status is returned. Other error conditions, such as insufficient permissions to read the given path should be treated in a similar manner. If successful, the script should print the depth to stdout and return 0.

I know how to deal with the error cases, but my main problem is to find the depth of the path. I was thinking to save the given path as a string and to count the number of '/' apparitions. I saw that I can do that using this command:

grep -o "/" $PWD | wc -l

But this displays 1 even if I change multiple directories. I am still learning about shell scripting, so can you give me details about the solutions?

4 Answers4

1

grep -o "/" $PWD would take the contents of PWD as the name of a file to read. That looks like it should give you an error since on many systems, you can't read a directory as if it was a file. (Except maybe in some systems you still can, but the contents aren't going to be very useful here.)

Instead use printf "%s\n" "$PWD" | grep -o / | wc -l. In ksh/Bash/zsh/yash, you could use the built-in substitution operator to remove anything but the slashes, and then take the count of characters: s=$PWD; s=${s//[^\/]}; num=${#s}

With zsh, you can shorten it to num=${#PWD//[^\/]}.

Or, using standard tools: printf %s "$PWD" | LC_ALL=C tr -dc / | wc -c.

ilkkachu
  • 138,973
0
grep -o "/" <<< "$PWD" | wc -l

You need to redirect your working directory back into grep and then print the number of lines.

  • (assuming a shell like zsh/ksh/yash/bash with that <<< here-string operator. You could use a here-document or printf '%s\n' "$PWD" | grep -o / | wc -l to make it more portable. In any case, -o is a GNU grep extension). – Stéphane Chazelas Jan 26 '18 at 15:15
0

To count the number of "/" in path Just use below command. As Tested it worked fine

command

echo "$PWD" | awk '{print  gsub("/","",$0)}'

output

@praveen t1]#

 pwd
/tmp/p1/t1

[root@praveen t1]# echo "$PWD" | awk '{print gsub("/","",$0)}'

3

[root@praveen

0

The command in your post (grep -o "/" $PWD | wc -l) is going to try to grep using $PWD as a target, rather than the string of characters that composes that path.

In order to achieve your intended effect, you could do echo $PWD | grep -o "/" | wc -l.

As an alternative, since the objective is to find the depth of the path, you could count the number of directories in that path instead of the number of forward slashes.

For instance:

echo "$PWD" | tr " /" "- " | wc -w

tr translates all spaces to the "-" character, and all "/" to spaces. wc -w then counts the number of words in the previous output. This makes sure each directory counts only as one word, even if they contain spaces in their name. Furthermore, since we're not counting the number of "/", checking your depth at the root directory will indicate a depth of 0.

  • Example 1: $ pwd /tmp/test directory $ echo "$PWD" | tr " /" "- " | wc -w 2

  • Example 2: $ pwd / $ echo "$PWD" | tr " /" "- " | wc -w 0

simoesf
  • 16
  • 1
    space is not the only character that separates words for wc -w, and the list varies with the wc implementation and the locale. – Stéphane Chazelas Jan 26 '18 at 15:22
  • Also note that it returns 0 for PWD=/ (may be seen as an advantage as / could be considered as depth 0). More generally, while it's usuful to get a directory depth, it can't be used to count the number of occurrences of a character (for instance on ///foo///bar///, it would give 2 instead of 9, and would give 1 instead of 0 for foo). – Stéphane Chazelas Jan 26 '18 at 15:38
  • 1
    See pwd | LC_ALL=C tr '/[:space:]' ' [/*]' | LC_ALL=C wc -w for a more foolproof version. – Stéphane Chazelas Jan 26 '18 at 15:43