tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Proposal to add /usr/bin/shquote

On Sat, 06 Sep 2008, David Laight wrote:
> > The underlying problem is that the shell doesn't have a "list of
> > strings" data type, except in the special case of "$@".
> Ah... In that case you need something like:
> save_args() {
>     local a c n
>     c=0; n=$1; shift
>     for a; do
>       c=$(($c+1))
>       eval $n=\"\$$n \\\"\\\$${n}_\$c\\\"\"
>       eval ${n}_$c=\"\$a\"
>     done
> }
> # use:
> save_args list -foo bar "arg with spaces" "don't panic" '"Help!"'
> # later:
> eval set -- $list

Nice!  And I see that it allows for easy concatenation of lists.

However, it requires the save_args function to be cut and pasted into
every script that wants it (or made available via a library of shell
functions).  Also, it works only where the code that constructs the list
and the code that uses the list are able to share all those list_1,
list_2, ... variables; it doesn't help in the common case of one script
passing an arg to another command, where the arg will eventually be
executed via system(3) or "sh -c" or similar.

Here's another example to illustrate the problem:

        remote_file="file name with spaces 'quotes & dollar$"
        remote_cmd="$( shquote rm -f "$remote_file" )"
        ssh_cmd="$( shquote ssh user@host "$remote_cmd" )"
        somescript --what-to-do="${ssh_cmd}"

I found it much easier to write the above than to figure
out how many backslashes to use in

        somescript --what-to-do="ssh user@host rm -f INSERT_MESS_HERE"

--apb (Alan Barrett)

Home | Main Index | Thread Index | Old Index