1

I'm trying to run the below code on bash, but each time I got error ( invalid arithmetic operator) could anyone help

#!/bin/bash
   for i in $(seq 20);
    do
            k=$(($i/100))
            a=$((4.18+$k))
            b=$(($a+0.01))

            siesta < BiTeI.fdf > out_file  #do the operation
            lineno=939   # set the line number where the data you want to read is in
            newline=$(sed -n "${lineno}p" "out_file") # asign a veriable for the line from the txt file note to change the file name
            echo $newline >> output.txt  # print the line extracted to an out put file note to generate file before adding to it
            sed -i "30s/$a/${b}/" BiTeI.fdf  # update the input file for new parameters for a specific raw


    done  
asem assaf
  • 11
  • 1
  • 5
  • i have tried this, but each time it says that bc: command not found – asem assaf Nov 13 '18 at 18:01
  • 1
    It's hard to believe you don't have `bc` installed on your system. What is the output of `uname -srv`? Its more likely you have overwritten your normal `$PATH` variable. Start a new terminal, and issue the following cmd `echo $PATH`. Do you see names like `/bin:/usr/bin:/sbin:/usr/sbin`? You should. If you do, *now* see if `bc` is available, ie. `which bc`. If it is now there, then look at your script for where you are resetting PATH, you need `export PATH="/new/dir:$PATH"`. Good luck. – shellter Nov 13 '18 at 18:22

3 Answers3

1

When you get an error in bash, it's time to fall back to print-line debugging. Add a set -x to the top of the script, after the shebang line. Then re-run it. Bash will tell you what it's doing.

line 8: 4.18 + 0: syntax error: invalid arithmetic operator (error token is ".18 + 0")

So it's saying it doesn't know how to parse the parts after the 4: .18..... That's because bash doesn't actually support decimals: Bash C style arithmetic with floating point value

omajid
  • 14,165
  • 4
  • 47
  • 64
  • i understand the error but each time i use bc solution i get bc: command not found – asem assaf Nov 13 '18 at 18:08
  • Well, you need to pick a program that provides math and decimal support, install it and then use it. If you need `bc`, install it. If you opt for `python`, install that. If you can't install any programs, find an installed tool that supports decimals. You must have *some* programming language on the machine (`perl`, `python`, `php`, `ruby`...), use that if you can't find anything else. – omajid Nov 13 '18 at 18:12
0

i have tried this solution and it works for me

#!/bin/bash

echo $result

   for i in $(seq 20);
    do
            k=$(python -c "print($i/100)")
            a=$(python -c "print(4.18+$k)")
            b=$(python -c "print($a+0.01)")
            echo $k
            echo $a
            echo $b
            siesta < BiTeI.fdf > out_file  #do the operation
            lineno=939   # set the line number where the data you want to read is in
            newline=$(sed -n "${lineno}p" "out_file") # asign a veriable for the line from the txt file note to change the file name
           echo $newline >> output.txt  # print the line extracted to an out put file note to generate file before adding to it
            sed -i "30s/$a/${b}/" BiTeI.fdf  # update the input file for new parameters for a specific raw


    done  

thanks

asem assaf
  • 11
  • 1
  • 5
0

As much as I dislike awk, it can save you some headache.

$: cat x
for i in $(seq 20)
do read k a b <<< "$( echo "$i" | awk '{
      k = $1 / 100;
      a = 4.18 + k;
      b = a + 0.01;
      printf "%.2f %.2f %.2f\n", k, a, b;
   }' )"
   echo "i=$i k=$k a=$a b=$b"
done

$: ./x
i=1 k=0.01 a=4.19 b=4.20
i=2 k=0.02 a=4.20 b=4.21
i=3 k=0.03 a=4.21 b=4.22
i=4 k=0.04 a=4.22 b=4.23
i=5 k=0.05 a=4.23 b=4.24
i=6 k=0.06 a=4.24 b=4.25
i=7 k=0.07 a=4.25 b=4.26
i=8 k=0.08 a=4.26 b=4.27
i=9 k=0.09 a=4.27 b=4.28
i=10 k=0.10 a=4.28 b=4.29
i=11 k=0.11 a=4.29 b=4.30
i=12 k=0.12 a=4.30 b=4.31
i=13 k=0.13 a=4.31 b=4.32
i=14 k=0.14 a=4.32 b=4.33
i=15 k=0.15 a=4.33 b=4.34
i=16 k=0.16 a=4.34 b=4.35
i=17 k=0.17 a=4.35 b=4.36
i=18 k=0.18 a=4.36 b=4.37
i=19 k=0.19 a=4.37 b=4.38
i=20 k=0.20 a=4.38 b=4.39
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36