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:

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

The tweenies (those that 'know not' and 'know not' that they 'know not')
the position that simply mentioning ls as a method to feed a loop
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
characters are embedded in the filename, such as a newline, carriage-return
null-terminating character inside the filename itself. Taking the position
if you know the set of filenames at issue does not contain any embedded
(like daily server logs, etc.), then there are no other hidden adverse
from using ls in this capacity. Certainly you would not be condemned to hell
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
the result of some horrible mistake)?"

The only example I have uncovered thus far is the OSX 'icon' file that has
'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
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
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

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

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

< Previous Next >
Follow Ups