Mailinglist Archive: opensuse (982 mails)

< Previous Next >
Re: [opensuse] Bashism - Seeking input on filenames that contain non-printing control characters (examples)
On 08/31/2014 12:34 AM, David C. Rankin wrote:
All,

An issue recently came up on StackOverflow.com regarding shell scripts
related to the use of 'ls -opts' to populate a loop structure in a shell
script.
Suffice it to say *in sum*, it is good practice to avoid this use of ls and
to
search for an alternative solution to fit your needs. That being so, there
are
some cases where the loop needs data in a sort order fashion, easily provided
by
ls, but requiring tortured, piped multiple external calls otherwise.

The tweenies (those that 'know not' and 'know not' that they 'know not')
took
the position that simply mentioning ls as a method to feed a loop
automatically
condemned you to roast in hell for eternity. (despite TLDP BASH HowTo being
riddled with examples)

Those that 'know', said wait a minute, it is a portability issue, and the
problems protected against are just those instances where non-printing
control
characters are embedded in the filename, such as a newline, carriage-return
or
null-terminating character inside the filename itself. Taking the position
that
if you know the set of filenames at issue does not contain any embedded
nonsense
(like daily server logs, etc.), then there are no other hidden adverse
effects
from using ls in this capacity. Certainly you would not be condemned to hell
or
have your eyes plucked out by dragons if you did.

So that brings up the question:

"Just which files do have embedded control characters (intentionally, and not
as
the result of some horrible mistake)?"

The only example I have uncovered thus far is the OSX 'icon' file that has
a
'carriage-return' intentionally embedded as part of the filename. What others
have you run across or can think of?

I could think of no better or more experienced group to consult that the
good
old "brain-trust". So just how prevalent are these filenames with embedded
control characters?

Unfortunately, you didn't provide a concrete example.
"Control characters" may not happen so often (or they do, however), but the
point
is that you mentioned that the output is processed further in a loop. For that,
the shell must do some parsing again, and might fall into traps because of these
file names. As an example, the most evil trap combined with the most usual case
are blanks in the file name.

$ f1=file1; f2='file two'; f3=file3
$ files="$f1 $f2 $f3" # bad example
$ for f in $files ; do echo $f ; done # bad example
file1
file
two
file3

Thus said, it's not a matter of which characters the script expect,
but rather how you do the quoting.

Have a nice day,
Berny
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
Follow Ups