Link to home
Start Free TrialLog in
Avatar of shimsha
shimsha

asked on

Bash script to find out whether the current directory has character '.' at the end

I want a script to find out whether the current directory, has character '.' at the end of each level of subdirectories. e.g.,
-if the current directory is /root/appl./test , it should return true
-if the current directory is /root/appl/test. , it should return true
-if the current directory is /root/appl/test./ , it should return true
-if the current directory is ./root/appl/test./ , it should return false

thanks
shimsha
Avatar of NovaDenizen
NovaDenizen

Uncomment the echo lines to see how it works.

#!/bin/bash

DIR=`pwd`/
#echo DIR is $DIR
echo $DIR | grep -q \\./ -
RES=$?
#echo RES is $RES
exit $RES
Avatar of Tintin
NovaDenizen.

Your code would return true for the last example, which should be false.

Here's a version that works for the sample cases:

#!/bin/sh

pwd | grep -q "^/.*\."

if [ $? -eq 0 ]
then
     echo "cwd has ."
else
     echo "cwd has no ."
fi
Avatar of shimsha

ASKER

Thanks for the scripts.
This script returns true when the directory name has spaces in the middle of th name. I want to check the character '.' only at the end of directory names. So,
-if the current directory is /root/appl.test/test , it should return false
-if the current directory is /root/appl./test , it should return true

thanks,
shimsha
Assuming you're using pwd to get the cwd, then NovaDenizen's suggestion will work fine for you.
What is the deal with the fourth example?
/root/appl./test  true
/root/appl/test. true
/root/appl/test./ true
./root/appl/test./ false

How can the current directory possibly start with a '.' like in the fourth example?

Also, the 'test.' directory of the fourth example ends with a period, so it should return true according to your description.

Your question reads:
> I want a script to find out whether the current directory, has character '.' at the end of each level of subdirectories.
Well, none of your examples has a '.' at the end of each level of subdirectories.  Perhaps you meant to say "any level of subdirectories" instead?
ASKER CERTIFIED SOLUTION
Avatar of da99rmd
da99rmd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
da99rmd

UUOE

echo `pwd`
What ?
/R
Google is your friend.  
Whoops.  I did a search for UUOC (which is directly related), but UUOE only has an obscure reference.

UUOE - Useless Use of Echo

echo `pwd`

is the same as:

pwd
hehe, its a habit i use echo for everything =)

/R