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 > Setting IFS to the null character
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Setting IFS to the null character

Réponse
 
LinkBack Outils de la discussion
Vieux 21/05/2007, 23h59   #1
David Wake
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Setting IFS to the null character


The "find" utilityQ offers the "-print0" option which can be used in conjunction
with "xargs". For example:

find -name "*.jar" -print0 | xargs -0 unzip -l

This uses the null character, instead of whitespace, as the field
separator and so works correctly even if some files contain whitespace
in their paths.

I'm trying to find a way to do something similar using a "for" loop.
My aim is to make the "for" loop split on null characters. The manual
says that the way to do this is to set the IFS variable, so I'm trying
to make IFS be the null character but I'm not having much luck:

> touch 1 2 3 4 5


> export IFS=$(echo -en '\0'); for file in $(find -type f -print0); do echo $file; echo "next"; done

../2./4./5./3./1
next

> export IFS=\0; for file in $(find -type f -print0); do echo $file; echo "next"; done

../2./4./5./3./1
next

I'd appreciate any !

Thanks,

David
  Réponse avec citation
Vieux 22/05/2007, 01h01   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting IFS to the null character

On 2007-05-21, David Wake wrote:
>
> The "find" utilityQ offers the "-print0" option which can be used in conjunction
> with "xargs". For example:
>
> find -name "*.jar" -print0 | xargs -0 unzip -l
>
> This uses the null character, instead of whitespace, as the field
> separator and so works correctly even if some files contain whitespace
> in their paths.
>
> I'm trying to find a way to do something similar using a "for" loop.
> My aim is to make the "for" loop split on null characters. The manual
> says that the way to do this is to set the IFS variable, so I'm trying
> to make IFS be the null character but I'm not having much luck:
>
>> touch 1 2 3 4 5

>
>> export IFS=$(echo -en '\0'); for file in $(find -type f -print0);do echo $file; echo "next"; done


So long as the filenames do not contain newlines:

find -type f |
while IFS= read -r
do
printf "%s\n" "$file"
echo "next"
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 22/05/2007, 01h19   #3
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting IFS to the null character

On 21 May 2007 15:59:48 -0700, David Wake
<dwake@stanfordalumni.org> wrote:
>
>
>
> The "find" utilityQ offers the "-print0" option which can be used in
> conjunction with "xargs". For example:
>
> find -name "*.jar" -print0 | xargs -0 unzip -l
>
> This uses the null character, instead of whitespace, as the field
> separator and so works correctly even if some files contain whitespace
> in their paths.
>
> I'm trying to find a way to do something similar using a "for" loop.
> My aim is to make the "for" loop split on null characters. The manual
> says that the way to do this is to set the IFS variable, so I'm trying
> to make IFS be the null character but I'm not having much luck:
>

Good luck, but I don't think it's possible.


--
Nothing is but what is not.
  Réponse avec citation
Vieux 22/05/2007, 01h47   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting IFS to the null character

On 2007-05-22, Chris F.A. Johnson wrote:
> On 2007-05-21, David Wake wrote:
>>
>> The "find" utilityQ offers the "-print0" option which can be used in conjunction
>> with "xargs". For example:
>>
>> find -name "*.jar" -print0 | xargs -0 unzip -l
>>
>> This uses the null character, instead of whitespace, as the field
>> separator and so works correctly even if some files contain whitespace
>> in their paths.
>>
>> I'm trying to find a way to do something similar using a "for" loop.
>> My aim is to make the "for" loop split on null characters. The manual
>> says that the way to do this is to set the IFS variable, so I'm trying
>> to make IFS be the null character but I'm not having much luck:
>>
>>> touch 1 2 3 4 5

>>
>>> export IFS=$(echo -en '\0'); for file in $(find -type f -print0);do echo $file; echo "next"; done

>
> So long as the filenames do not contain newlines:
>
> find -type f |
> while IFS= read -r


Missing var name; fixed below:

while IFS= read -r file

> do
> printf "%s\n" "$file"
> echo "next"
> 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 22/05/2007, 09h00   #5
Martin Krischik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting IFS to the null character

David Wake schrieb:
> The "find" utilityQ offers the "-print0" option which can be used in conjunction
> with "xargs". For example:
>
> find -name "*.jar" -print0 | xargs -0 unzip -l
>
> This uses the null character, instead of whitespace, as the field
> separator and so works correctly even if some files contain whitespace
> in their paths.
>
> I'm trying to find a way to do something similar using a "for" loop.
> My aim is to make the "for" loop split on null characters. The manual
> says that the way to do this is to set the IFS variable, so I'm trying
> to make IFS be the null character but I'm not having much luck:
>
>> touch 1 2 3 4 5

>
>> export IFS=$(echo -en '\0'); for file in $(find -type f -print0); do echo $file; echo "next"; done

> ./2./4./5./3./1
> next
>
>> export IFS=; for file in $(find -type f -print0); do echo $file; echo "next"; done

> ./2./4./5./3./1
> next
>
> I'd appreciate any !


Am I glad that I discovered Z-Shell:

--------------------------------------------------
>touch 1 2 3 4 5 'with space' 'with

quote>linefeed'
>setopt Extended_Glob;
>for i in *(.); do

for> echo "${i}";
for> echo "next";
for>done
1
next
2
next
3
next
4
next
5
next
with
linefeed
next
with space
next
--------------------------------------------------

No find, no pipe, no IFS twisting, no endless trying out different
variations, just a for loop which works first time.

And if you need recursive search then use "./**/*(.)" as pattern.

Martin
--
Martin Krischik
  Réponse avec citation
Vieux 22/05/2007, 12h51   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Setting IFS to the null character

2007-05-21, 15:59(-07), David Wake:
>
> The "find" utilityQ offers the "-print0" option which can be used in conjunction
> with "xargs". For example:
>
> find -name "*.jar" -print0 | xargs -0 unzip -l
>
> This uses the null character, instead of whitespace, as the field
> separator and so works correctly even if some files contain whitespace
> in their paths.
>
> I'm trying to find a way to do something similar using a "for" loop.
> My aim is to make the "for" loop split on null characters. The manual
> says that the way to do this is to set the IFS variable, so I'm trying
> to make IFS be the null character but I'm not having much luck:
>
>> touch 1 2 3 4 5

>
>> export IFS=$(echo -en '\0'); for file in $(find -type f -print0); do echo $file; echo "next"; done

> ./2./4./5./3./1
> next
>
>> export IFS=

> ./2./4./5./3./1
> next
>
> I'd appreciate any !

[...]

Only zsh can have a NUL byte in its variables. Environment
variables can't have NUL bytes in them as they are NUL
terminated strings.

But:

IFS=$'\0'
for file in $(find . -type f -print0); do
...
done

can actually be written in zsh as

for file in ./**/*(ND.oN); do
...
done

Has zsh has find features as part of its globbing.

Anyway, the best way to perform actions for every file found by
find is to use find's -exec predicate.

--
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 17h20.


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14134 seconds with 14 queries