Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am teaching myself bash scripting with the book 'Learning the Bash Shell' by Newbam. I'm getting on OK with the book, but am a bit confused by the following script. It is a script which uses a for loop to go through the directories in $PATH, and print out information about them. The script is

IFS=:
for dir in $PATH; do
 if [ -z "$dir" ]; then dir=.; fi
 if ! [ -e "$dir" ]; then
 echo "$dir doesn't exist"
 elif ! [ -d "$dir" ]; then
 echo "$dir isn't a directory"
 else
  ls -ld $dir
done

What I'm confused about is why if dir is zero, or doesn't exist, are we then setting this null value to be the current directory? Or are we changing dir to our current directory (whatever directory we happen to be in) ? The script then seems to test if dir exists, and then tests if it is a directory.

Can anybody shed light on this aspect of the script for me? I would have thought that if [ -z "$dir" ] was true, that this would indicate that the directory doesn't exist, and isn't a directory to begin with?

share|improve this question
1  
To bash "." and the empty field in the PATH are synonym to the "current directory". –  Emmanuel 18 hours ago
    
Thanks Emmanuel. Didn't realise that. –  John D 18 hours ago

3 Answers 3

up vote 4 down vote accepted

The -z test is testing for a zero-length value in $dir, not for anything related to an actual directory. That condition is triggered if you have a :: sequence in your PATH variable, at which point we set the value of the $dir variable to ., meaning the current directory, and we then conduct the standard two tests on it.

share|improve this answer
    
Thanks. Still can't quite see the logic of performing tests on our current directory when the aim of the script was to test specifically directories in PATH –  John D 17 hours ago
    
If there is an empty directory in PATH, it means the current directory is in the PATH. :: in $PATH is shorthand for :.:. –  John 17 hours ago

If $dir is empty (-z test) then $dir is set to . (current directory where you launched the script).

Now guess that your $PATH is malformed like PATH=/bin::/usr/bin:/usr/local/bin At second rank you have an empty field. So when the field is empty, it is replaced by the current directory from where is launched the script (then dir=.).

share|improve this answer
    
Thanks for reply. But aren't we then performing the test on a random directory ( the one we happened to be in when we ran script) rather than a directory in PATH? Sorry for being slow on the uptake with this –  John D 18 hours ago
    
Exact you guess it right ! –  netmonk 18 hours ago

maybe there is a elif (else if) missing.

however, this scripts aims to list $dir contents, but first make sure

  1. $dir exists if ! [ -e "$dir" ]
  2. $dir is a dir, not a file, pipe, dev elif ! [ -d "$dir" ]

thoses test cannot be made with empty value for $dir, so first line ( if [ -z "$dir" ]; then dir=.; fi ) is used to relace empty string by . which is a dir and exists.

empty $dir string can be make two ways

  • PATH=/usr/bin:/sbin::$HOME/bin
  • PATH=/usr/bin:/sbin:$MYAPPBINDIR:$HOME/bin (where $MYAPPBINDIR is empty).
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.