John H. Robinson, IV wrote:
> Dexter Filmore wrote:
>> Well, shouldn't \ do th job as well?
>> Thing is, even if I pass the filename 
>> foo\ bar
>> echo tells me the name is 
>> foo bar
> 
> echo is a bad tool to use to determine where the shell is breaking the
> words.
> 
> compare:
> 
> a="moo cow"
> echo \""$a"\"
> echo \"$a\"
> 
> They both provide the same results, but one is split and the other is
> not.
> 
> This is why I offered you ls -b. You can also use test -f.
> 
> sh-3.1$ touch "moo cow"
> sh-3.1$ a=moo\ cow
> sh-3.1$ test -f $a
> sh: test: moo: binary operator expected
> sh-3.1$ test -f "$a"
> 
> $a got split, and "$a" remained as a single word. You can also play with
> set.
> 
> sh-3.1$ a="moo cow"; set - $a; echo $#; set - "$a" ; echo $#
> 2
> 1
> 
> 
> Stop trying to count the number of arguments with echo: it does not
> work.

The last sentence is right-on!

I provided a script that can be used instead of echo, that has helped me
 with this issue. I guess I didn't explain very well what it's purpose
was or how to use it.

Here it is again:
- - -
 #!/bin/sh
echo "$# args: ($@)"
ii=0
while [ $# -gt 0 ]
do
    echo "arg $((++ii))='$1'"
    shift
done
#===eof===
- - -

create an executable script, say
 /usr/local/bin/argview

and just substitute argview for echo in your testing.

I heartily recommend spending 5 minutes playing on the command line
giving it a variety of arguments, quoted, escaped, whatever you want.

try
 argview abc d e f
 argview abc "d e" f
 argview abc 'd e' f
 argview abc d\ e f
and so on

I believe this directly addresses the problems you are having, and would
help you discover the right practices.

As jhriv says, the problem is in grasping how the command line gets
parsed by the shell in executing a command. A further complication is
that your command is being constructed _within_ a shell script. But once
you construct the command line, (something like) argview will show you
how it gets parsed.

Perhaps someone else can help if my explanation is still inadequate.

Regards,
..jim



-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to