[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [patch] new special variable: test argument
From: |
Piotr Grzybowski |
Subject: |
Re: [patch] new special variable: test argument |
Date: |
Sat, 19 Dec 2015 00:58:41 +0100 |
Thanks for taking time to answer. Of course it is the issue of
programming style (isnt it true that most things can be brought down
to the programming style?), two real-life examples (stripped-down of
everything):
#1
if [ -f ${output}/${branch}/${index}/${current} ]; then
let current--;
for((i=0;i<${branch};i++)) do
index=`new_index ${current} ${index}`;
done;
echo "$^ already filled, regenerating index ${index}/${current}";
find ${backup}/`basename $^` -type f -exec cp `dirname "$^"/* [..]`
rebuilt ${output} -previous "$^" -branch ${branch} ${index}/${current};
fi;
lets assume that for some wicked reason you do not need another local
variable l=${output}/${branch}/${index}/${current}, besides you would
need to remember that it consists of many parts, assemble and
reassemble it, remembering some previous values, also for some reasons
you cant refactor the above code (which of course always can remove
the need for any special variables): lets say that it reflects some
logic. Without $^ it begins to look terrible.
#2
touch /tmp/`date +%s`; if [ -f /tmp/`date +%s` ]; then echo "ok: $^"; fi;
just a quick example (the real one was much more complicated in terms
of `` and the latter use of $^) of auto-generated code whos author
really did not want to use another variable, as it would be a waste of
space in his generating code. Without $^ the above requires more care.
As for the all-purpose perl-alike $_ of course I have some ideas,
just let me think, and lets just hope I will not retreat into myself
after you say that $^ is useless ;-)
cheers,
pg
On Wed, Dec 16, 2015 at 8:12 PM, Chet Ramey <chet.ramey@case.edu> wrote:
> On 12/16/15 10:03 AM, Piotr Grzybowski wrote:
>> Dear All,
>>
>> one thing I missed for some time now, is the ability to access the
>> argument passed to test, or any argument on the right hand side.
>> I needed it so I made a quick hack, which I attach as a reference.
>> It allows to access arg in the the -f $arg easily, e.g.:
>>
>> [ -f /tmp/myfile ] && { echo "$^ is here"; head -1 "$^"; }
>
> This seems like an issue of programming style more than anything else.
> If you have a string or similar that you would like to refer to more
> than once, use a variable for it. It would take perhaps half a dozen
> more characters to use an explicit variable here.
>
> If you want something that is similar to perl's `$_', I'd be interested
> in your thoughts, but this is of limited usefulness as it stands.
>
> Chet
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
> ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRU chet@case.edu http://cnswww.cns.cwru.edu/~chet/
Re: [patch] new special variable: test argument, konsolebox, 2015/12/16