0

I'm trying to create simple bash script which stores in x variable number of lines of abc.csv file and then stores in a, b and c variables results of some arithmetic operations. When I run the script I get errors:

let: not found

and

arithmetic expression: expecting EOF: "0.2*"

The script:

let x=$(wc -l abc.csv | awk '{print $1}')
let a_size= $((0.2*$x))
let b_size=$((2*0.2*$x))
let c_size=$((2*0.2*$x+1))

How to perform arithmetic operations properly in this case? Any help would be greatly appreciated.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
appendix
  • 119
  • 1
  • 2
  • 8
  • [Q&A from sister site unix.stackexchange.com](https://unix.stackexchange.com/a/40787/234539) discussing options for performing (math) calculations in `bash` – markp-fuso Mar 28 '21 at 16:07

1 Answers1

1

If you ensure that you are running the script with bash (by for example adding a shebang)

#! /usr/bin/env bash

Does that fix it?

Holp
  • 283
  • 1
  • 5
  • I got error: "syntax error: invalid arithmetic operator (error token is ".2*10000")" – appendix Mar 28 '21 at 15:45
  • I omitted the problem by "2*2/10" instead of "2*0.2", turns out that bash can't handle float numbers, weird. – appendix Mar 28 '21 at 15:50
  • @appendix while `2*2/10` will get rid of the error message, are you sure your variable does in fact contain the correct value? – markp-fuso Mar 28 '21 at 16:01