James James - 4 months ago 7x
Bash Question

Get variable from shell script in a perl IF statement

This is a follow-up question from Modify text column based on the column before it

I wanna change the starting index of the line processing, say start from the third line. I notice that in order for perl to use the variable in shell, I must export the variable and use

in perl, see:


export t
perl -e 'print $ENV{t}'
perl -lane '$F[3] += sin($F[2]/10 * 4 * atan2 1, 1) if($ENV{t} .. 4);
print "@F"
'test.txt > test_new.txt

Here test.txt is merely the same with the previous question:

A 0.016333 0.003203 0.472723
A 0.016333 0.035228 0.472723
B 0.016333 0.067253 0.472723
B 0.016333 0.099278 0.472723
C 0.016333 0.131303 0.472723
C 0.016333 0.163328 0.472723

However, the
does not work at all: the line processing still starts from the first line. Maybe in IF statement the usage is different??
What should I do to control which line to start?


It's the range operator that's doing it. The particular rule you are using for (3..4) is

If either operand of scalar ".." is a constant expression, that operand is considered true if it is equal (== ) to the current input line number (the $. variable).


It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated.

When you have a variable for one end point it is being evaluated and is found to be true. So the left end is always true and the operator never gets to be false, and all lines are printed.

As for how to do it, forego the elegance and test explicitly,

if $. >= $ENV{t} and $. <= 4

You can still use the range operator, for a more compact expression

if $.==$ENV{t} .. 4

However, at this point this may be not as clear as a normal test while a possible tiny gain in performance (if any) may not even be measurable. Thanks to ikegami for bringing this up and for further comments.