Unix questions - Newbie learning shell scripting

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > February 2006 > Newbie learning shell scripting





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Newbie learning shell scripting
Jonathan

2006-02-22, 6:04 pm

I have 4 files in my home directory.


files are:

Unix Shell Scripting.foo
Unix in a Nutshell.foo

UnixShellScripting.bar
UnixinaNutshell.bar

(See, the latter two don't have blank spaces between each word).


I'm trying to count lines of all my files (including those that possibly
reside in subdirectories) that have word 'Unix' in the beginning of the
names

It goes like this ...


sh-3.00$ wc -l $(find . -type f -name 'Unix*')
68 ./UnixShellScripting.bar
wc: ./Unix: No such file or directory
wc: Shell: No such file or directory
wc: Scripting.foo: No such file or directory
wc: ./Unix: No such file or directory
wc: in: No such file or directory
wc: a: No such file or directory
wc: Nutshell.foo: No such file or directory
4 ./UnixinaNutshell.bar
72 total
sh-3.00$

Script cannot cope with filenames that consist of several separated
words, because 'find' pipes it's results unquoted to 'wc'.

I guess I should add some escaped quotation marks (\') somehwere but
I've tried all places that I can possibly come up with.

Does anyone know the solution to the problem?


J.S.
Stephane Chazelas

2006-02-22, 6:04 pm

On Wed, 22 Feb 2006 14:20:06 +0000 (UTC), Jonathan wrote:
> I have 4 files in my home directory.
>
>
> files are:
>
> Unix Shell Scripting.foo
> Unix in a Nutshell.foo
>
> UnixShellScripting.bar
> UnixinaNutshell.bar
>
> (See, the latter two don't have blank spaces between each word).
>
>
> I'm trying to count lines of all my files (including those that possibly
> reside in subdirectories) that have word 'Unix' in the beginning of the
> names
>
> It goes like this ...
>
>
> sh-3.00$ wc -l $(find . -type f -name 'Unix*')
> 68 ./UnixShellScripting.bar
> wc: ./Unix: No such file or directory
> wc: Shell: No such file or directory
> wc: Scripting.foo: No such file or directory
> wc: ./Unix: No such file or directory
> wc: in: No such file or directory
> wc: a: No such file or directory
> wc: Nutshell.foo: No such file or directory
> 4 ./UnixinaNutshell.bar
> 72 total
> sh-3.00$
>
> Script cannot cope with filenames that consist of several separated
> words, because 'find' pipes it's results unquoted to 'wc'.


It's more because the shell splits the output of find according
to spaces, tabs and newlines by default (and also performs
filename generation).

You could do:

set -f # disable filename generation
IFS='
' # split on newline only

wc -l $(find . -type f -name 'Unix*' -print)

but the correct way to do it is:

find . -type f -name 'Unix*' -exec wc -l {} +

Which will also work for filenames with newline characters in
them and work even if the size of the list of files exceeds the
limitation on the number of argument of the execve system call.

> I guess I should add some escaped quotation marks (') somehwere but
> I've tried all places that I can possibly come up with.


No, the word splitting (one of the things that happen when you
leave a variable or command substitution ($(...) or `...`
unquoted in list contexts) doesn't know of any escape mechanism.

--
Stephane
Bruce Barnett

2006-02-22, 8:51 pm

Stephane Chazelas <stephane_chazelas@yahoo.fr> writes:

> but the correct way to do it is:
>
> find . -type f -name 'Unix*' -exec wc -l {} +


However the OP said

>I have 4 files in my home directory.


so
wc Unix*

should also work. I guess it depends if he wants to recurse.

--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com