[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-05
11
Medium Priority
?
589 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:shimsha
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 12510578
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
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12518873
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
0
 

Author Comment

by:shimsha
ID: 12523437
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
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 48

Expert Comment

by:Tintin
ID: 12528646
Assuming you're using pwd to get the cwd, then NovaDenizen's suggestion will work fine for you.
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 12528722
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?
0
 
LVL 8

Accepted Solution

by:
da99rmd earned 200 total points
ID: 12531156
I think this solved your problem, I devided the regexp in 2 to make it easier to understand.

#!/bin/bash
echo `pwd` |  egrep -q '^[^\.]' |  egrep -q '\.(/|$)'
if [ $? -eq 0 ]
then
     echo "cwd has ."
else
     echo "cwd has no ."
fi
#end

/R
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12539595
da99rmd

UUOE

echo `pwd`
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 12541851
What ?
/R
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12549861
Google is your friend.  
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12549878
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
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 12552920
hehe, its a habit i use echo for everything =)

/R
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question