0

Im trying to replace a number in a file with a calculated floating variable in a bash file. So im trying to replace 1.1111 with the value of "km" and save it in the mesh.in file. I keep getting an error on the sed line, I think there may be an issue with the floating variable. Echo "$km" does work so i know that the km is not the issue

#!/bin/bash
read -p "Angle in degrees : " n1
read -p "bcsa : " n2
cd viv_example_se2d
sed s/^bcsa.\*/"bcsa $n2"/ runfile.viv >temp
mv -f temp runfile.viv
cd ../
for i in $(seq 2 0.5 12)
do
    if  [ ! -d U*_$i ];then
        mkdir U*_$i
    fi
    printf -v "km" "%.4f\n" $(echo | bc | awk "BEGIN {print 4*3.14159265359*3.14159265359/($i*$i)}")
    echo "$km"
    cd viv_example_se2d
    sed s/1.1111/$km/g mesh_master.in > temp$i 

    mv -f temp$i mesh.in
    cd ../
    echo $home/lustre/projects/p057_swin/ogoldman/Ellipse_$n1/U*_$i | xargs -n 1 cp viv_example_se2d/*
done;
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Osher Goldman
  • 21
  • 1
  • 4

1 Answers1

1

The problem is the newline in the value of $km. It is confusing sed.

That being said this script is also a bit of a mess.

You should quote your variables when you use them to protect against problems with whitespace and glob characters in the values.

You don't need xargs to cp multiple files that you can expand via a glob. cp will happily take multiple files to copy directly. (Oh, or is that copying multiple files to directories produced via that glob?)

You have a useless echo | bc | bit near the awk command.

Using full/relative paths in sed/etc. is better than cding around generally.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • Didnt mean to have the `-i ` and `| bc - l `. I was playing around trying a few different things and couldnt fix it. Script has been editted. Also dont worry about the `$..../U*_$i` it had a long path so i just removed the beginning which isnt important – Osher Goldman Apr 28 '15 at 15:21
  • 1
    You need to show us **exactly** the code you are working with and **exactly** the problem you are having or we can't help you correctly. – Etan Reisner Apr 28 '15 at 15:24