PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > how to detect the dir to cd to begins with a dot
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

how to detect the dir to cd to begins with a dot

Réponse
 
LinkBack Outils de la discussion
Vieux 18/05/2007, 02h14   #1
paintedjazz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to detect the dir to cd to begins with a dot

Is there a way in a bash script to detect if the directory one is
about to cd into is a directory that begins with a dot?

e.g.

find . -type d |
while read dir
do
if [ $dir begins with a dot ]
then
continue
else
do stuff
fi
done

Thanks much.

  Réponse avec citation
Vieux 18/05/2007, 02h38   #2
paintedjazz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

On May 17, 6:14 pm, paintedj...@gmail.com wrote:
> Is there a way in a bash script to detect if the directory one is
> about to cd into is a directory that begins with a dot?
>
> e.g.
>
> find . -type d |
> while read dir
> do
> if [ $dir begins with a dot ]
> then
> continue
> else
> do stuff
> fi
> done
>
> Thanks much.


This seems to work:

find . -type d |
while read DIR ; do
if echo "$DIR" |grep "^\." ; then
continue
else
cd "$DIR"
fi
done

  Réponse avec citation
Vieux 18/05/2007, 03h59   #3
James Michael Fultz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

* paintedjazz@gmail.com <paintedjazz@gmail.com>:
> On May 17, 6:14 pm, paintedj...@gmail.com wrote:
>> Is there a way in a bash script to detect if the directory one is
>> about to cd into is a directory that begins with a dot?

[ ... ]
> This seems to work:
>
> find . -type d |
> while read DIR ; do
> if echo "$DIR" |grep "^\." ; then
> continue
> else
> cd "$DIR"
> fi
> done


You can save a call to an external program by using case:

find . -type d -print |
while IFS= read -r DIR ; do
case "$DIR" in
.*)
continue
;;
*)
cd "$DIR"
;;
esac
done

Alternately, you could move the test upto find itself:

find . -type d ! -name '.*' -print |
while IFS= read -r DIR ; do
cd "$DIR"
done

--
James Michael Fultz <xyzzy@sent.as.invalid>
Remove this part when replying ^^^^^^^^
  Réponse avec citation
Vieux 18/05/2007, 04h33   #4
Harry331
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

paintedjazz@gmail.com wrote...
>
>Is there a way in a bash script to detect if the directory one is
>about to cd into is a directory that begins with a dot?
>
>e.g.
>
>find . -type d |
> while read dir
> do
> if [ $dir begins with a dot ]
> then
> continue
> else
> do stuff
> fi
> done


everything from
find . -type d
starts with a dot.

e.g.

$ cd /cygdrive/c/cygwin
$ find . -type d
..
../etc
../etc/setup
../etc/postinstall
../etc/defaults
../etc/defaults/etc
[snip]

Isn't it?

  Réponse avec citation
Vieux 19/05/2007, 02h58   #5
James Michael Fultz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

* Harry331 <harryooopotter@hotmail.co_>:
> paintedjazz@gmail.com wrote...
>>
>>Is there a way in a bash script to detect if the directory one is
>>about to cd into is a directory that begins with a dot?
>> [ ... ]

>
> everything from
> find . -type d
> starts with a dot.
>
> e.g.
>
> $ cd /cygdrive/c/cygwin
> $ find . -type d
> .
> ./etc
> ./etc/setup
> ./etc/postinstall
> ./etc/defaults
> ./etc/defaults/etc
> [snip]
>
> Isn't it?


You are quite right! Something I overlooked too. I suppose the
simplest solution using find is to have find itself test for and exclude
directories beginning with a dot.

$ ls -A
..quux bar baz foo

$ find . -type d ! -name '.*' -print
../foo
../bar
../baz

--
James Michael Fultz <xyzzy@sent.as.invalid>
Remove this part when replying ^^^^^^^^
  Réponse avec citation
Vieux 19/05/2007, 03h23   #6
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

On 2007-05-18, James Michael Fultz wrote:
> * paintedjazz@gmail.com <paintedjazz@gmail.com>:
>> On May 17, 6:14 pm, paintedj...@gmail.com wrote:
>>> Is there a way in a bash script to detect if the directory one is
>>> about to cd into is a directory that begins with a dot?

> [ ... ]
>> This seems to work:
>>
>> find . -type d |
>> while read DIR ; do
>> if echo "$DIR" |grep "^\." ; then
>> continue
>> else
>> cd "$DIR"
>> fi
>> done

>
> You can save a call to an external program by using case:
>
> find . -type d -print |
> while IFS= read -r DIR ; do
> case "$DIR" in


Strip the leading ./:

case "${DIR#./}" in

> .*)
> continue
> ;;
> *)
> cd "$DIR"


And isolate the cd in a subhsell:

(
cd "$DIR"
....
)

> ;;
> esac
> done
>
> Alternately, you could move the test upto find itself:
>
> find . -type d ! -name '.*' -print |
> while IFS= read -r DIR ; do
> cd "$DIR"
> done
>



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 20/05/2007, 11h57   #7
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

2007-05-17, 18:14(-07), paintedjazz@gmail.com:
> Is there a way in a bash script to detect if the directory one is
> about to cd into is a directory that begins with a dot?
>
> e.g.
>
> find . -type d |
> while read dir
> do
> if [ $dir begins with a dot ]
> then
> continue
> else
> do stuff
> fi
> done

[...]

case ${dir##*/} in
(.*) ...
esac

Note that find's output is not post-processable unless you use
some trick or can guarantee that none of the file names contain
newline characters. find has a -exec predicate, there should be
no need to use a while loop.

read alone is for something very special. If you want to read a
line of input, it's IFS= read -r dir.

Note that there's no guarantee that the while loop will or will
not be run in a subshell in the code above. So if for instance
you do a cd in that loop, it may only change the current
directory of that subshell.


--
Stéphane
  Réponse avec citation
Vieux 21/05/2007, 17h52   #8
paintedjazz@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to detect the dir to cd to begins with a dot

On May 20, 3:57 am, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2007-05-17, 18:14(-07), paintedj...@gmail.com:
> Is there a way in a bash script to detect if the directory one is
> > about to cd into is a directory that begins with a dot?

>
> > e.g.

>
> > find . -type d |
> > while read dir
> > do
> > if [ $dir begins with a dot ]
> > then
> > continue
> > else
> > do stuff
> > fi
> > done

>
> [...]
>
> case ${dir##*/} in
> (.*) ...
> esac
>
> Note that find's output is not post-processable unless you use
> some trick or can guarantee that none of the file names contain
> newline characters.


Do you mean using eval as the "some trick"?
.... for example:

eval 'find . -type d' -exec ...

Can a case statement follow a -exec ?
Does it require a special format because
of the double semi-colons and esac?

Also, why is it that find's output is not post-processable?
Is it just because the filename may contain a space, tab,
newline, backslash, single or double quote characters?
[just read your April 17 post when I searched the net
for post-processable and find] Other than that,
find doesn't write to stdout differently than any
other program, I hope.

Also, I learned about using eval in the manner above
on this ng a while back. May I ask what eval does to make
such a find command succeed instead of fail?


> find has a -exec predicate, there should be
> no need to use a while loop.
>
> read alone is for something very special. If you want to read a
> line of input, it's IFS= read -r dir.
>
> Note that there's no guarantee that the while loop will or will
> not be run in a subshell in the code above.


If I may ask one more question, why is a subshell created?
Because of the pipe?

> So if for instance
> you do a cd in that loop, it may only change the current
> directory of that subshell.
>
> --
> Stéphane



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22976 seconds with 16 queries