Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

csh inline math

Tags:

linux

shell

csh

I need to do some integer math in csh (and no, other shells are not an option, nor is bc, nor is perl, nor is python, period).

In bash my task would look like

seq 1 1 10 > m.txt  #supplied from elsewhere
a=2                 #supplied from elsewhere
b=3                 #supplied from elsewhere
head -n $[$a*$b] m.txt # the line in question

then the question is Is there an expression in csh that computes $[$a*$b] inline?
I know that I can do @ c = $a * $b in csh, but that's not inline. I did a little bit of googling and searching SO, but no success so far, so any help is greatly appreciated!

like image 693
alexey Avatar asked Mar 17 '26 01:03

alexey


2 Answers

Are your use of square-brackets meant to indicate an array notation or matrix math? csh has no such built-in features.

ELSE, if you mean like bash $(($a * $b)), you can use csh cmd-substitution with backquotes to give you

head -n `expr $a \* $b` m.txt

Note that if your goal was to avoid spawning extra processes, this does not meet your goal, but it is "in-line"

Edit I see I mistyped as $( $a * $b ), see inline correction above.

IHTH.

like image 129
shellter Avatar answered Mar 19 '26 03:03

shellter


Without using something outside of the shell, no.

The usual culprit for math from old school shell scripts is expr:

head -n `expr $a \* $b` m.txt

but if that's just as verboten as bc et al, then you're out of luck. Period.

like image 21
Mark Reed Avatar answered Mar 19 '26 02:03

Mark Reed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!