[英]Defining variables in bash script
I want to substitute the value of "CCC" in " c " which will be written in POSCAR file. 我想将“ c ”中的“ CCC”值替换为将写入POSCAR文件中的值。 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")
我收到此错误: ./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. 该操作对于变量i和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 问题是您的脚本处理浮点数,bash不支持浮点数学,您可以使用bc
,这是bash计算器来实现浮点数学
Replace the line 14 with below code in your script and it should work 将第14行替换为脚本中的以下代码,即可正常运行
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 有关bc
命令的完整参考,请访问页面https://www.gnu.org/software/bc/manual/html_mono/bc.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.