简体   繁体   中英

Defining variables in bash script

I want to substitute the value of "CCC" in " c " which will be written in POSCAR file. But somehow the operation is not working. I get this error: ./script-cb-ratio.sh: line 14: 3.24*4.78: syntax error: invalid arithmetic operator (error token is ".24*4.78")

The operation does work well for variable i and j. Please give some suggestions. Thanks in advance!

#!/bin/bash

for i in 3.24 3.26
do
        mkdir 'a_'$i
        cd 'a_'$i
        for j in 4.78 4.80 4.82 4.84 4.86
                do
                mkdir 'b_'$j
                cd 'b_'$j

                for k in 70.459 72.000
                do
                CCC = "echo $'((($k)/$(($i*$j))))'"
                cp ../../POSCAR_default .
                sed 's/_a_/'$i'/g' POSCAR_default > POSCAR1
                sed 's/_b_/'$j'/g' POSCAR1 > POSCAR2
                sed 's/_c_/'CCC'/g' POSCAR2 > POSCAR
                rm POSCAR_default
                rm POSCAR1
                rm POSCAR2
                done
                cd ..
                done
cd ..
done

The problem is your script handles floating point numbers, bash does not support floating point math , you can use bc which is the bash calculator to achieve the floating point math

Replace the line 14 with below code in your script and it should work

CCC=$(echo "scale=3;$k/($i*$j)" | bc)

for complete reference for bc command visit the page https://www.gnu.org/software/bc/manual/html_mono/bc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM