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

Re: bash variables



* 2011-02-25 14:21 (-0800), Mike McClain wrote:

> I occasionally have problems with bash variables, for instance the
> following command lists (along with everything else) 2 *.deb files in
> /home/mike/
>
> root@/deb40a:~> FIND1="-maxdepth 1 -type f -print -name '*'"; \
>     GREP="-v '\.\(deb\|gz\|tgz\|bz2\|tbz\|zip\)$'"; \
>     find /home/mike/ $FIND1 | grep $GREP ; 
>
> while without variables;
> root@/deb40a:~> find /home/mike/ -maxdepth 1 -type f -print -name '*' |
>     grep -v '\.\(deb\|gz\|tgz\|bz2\|tbz\|zip\)$'
> does not list the 2 *.deb files.
>
> In the same vein this command lists nothing:
> root@/deb40a:~> FIND="-name '*'"; find /root/bin $FIND
> while
> root@/deb40a:~> find /root/bin -name '*'
> lists 25 files.
>
> This only bites me once in a while but when it does it can be very
> frustrating so any hints / tips are welcome.

I think the problem is the single quotes (') in your variables. The
shell does not remove quotes several times. Therefore, "find" and "grep"
commands receive also the single quotes with their arguments. Try
removing the single quotes from hour FIND and GREP variables, or add
another round of evaluation with "eval" command.

Simple demo:

    $ mkdir temp_dir
    $ cd temp_dir
    $ touch new_file

    $ args="x '*'"

    $ echo $args
    x '*'

    $ eval echo $args
    x *

    $ eval eval echo $args
    x new_file

-- 
Feel free to Cc me your replies if you want to make sure I'll notice
them. I can't read all the list mail.


Reply to: