Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
SpacesInPathNames
Edit
PageHistory
Diff
Info
LikePages
Unix/Linux allows any character in a file/directory name except __/__ or __[NUL]__. However, embedded spaces, newlines and the like can cause problems for carelessly-written [Shell] scripts (or scripts for [carelessly-written shells|CshProgrammingConsideredHarmful]), and cause some command [PipeLine]s to fail completely. To get around this, the [GNU] versions of find(1) and xargs(1) incorporate special extensions that work together. The __-print0__ option to __find__ causes it to output matching pathnames terminated by __NUL__ characters rather than newlines, thereby avoiding confusion with any embedded newlines that might occur in the pathnames. Correspondingly, the __xargs__ command takes the option __--null__ (shorter synonym __-0__) to indicate that its input is delimited in this way; though on my [SUSE] system I also have to specify __--max-lines=1__ (shorter synonym __-L1__). For instance, suppose I have the following files: <pre> Events/Weddings/Freda's Wedding/data.txt Events/Car Race/data.txt Events/New Year Fireworks/data.txt </pre> and I want to count up the number of lines in each "data.txt" file. I can do this as follows: <pre> find Events -name data.txt -print0 | xargs -0 -L1 wc -l </pre> ---- As discussed on the BashNotes page, scripts can cope with arguments that contain spaces if you are careful. <tt>bash</tt> will expand *$@* as all of its arguments, but importantly will expand *"$@"* as each argument surrounded by "quote marks". Eg, if a script is called with arguments <verbatim> ./myscript "argument 1" "argument 2" </verbatim> then a script that did <verbatim> perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' $@ </verbatim> would output <verbatim> .argument. .1. .argument. .2. </verbatim> while a script that did <verbatim> perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' "$@" </verbatim> would output <verbatim> .argument 1. .argument 2. </verbatim>
2 pages link to
SpacesInPathNames
:
LawrenceDoliveiro
BashNotes