-1

I want to replace a line that says alpha = -pi/... with the correct calculated value in radians of the angle given ie variable n1

#!/bin/bash

read -p "Angle in degrees : " n1

## Convert angle to radians

printf -v "n2" "%.0f" $(echo | bc | awk "BEGIN {print 180/$n1}")

echo "$n1 Degrees = pi/$n2"

printf -v "n3" "alpha = -pi/$n2;"

echo "${n3}"

cd /home/src/octave_scripts/makemesh_rot


sed  "s/alpha =.*/$n3/" makemesh_rot.m > makemesh_rot.m_temp

Ive had a look around and tried a few variations changing "s/alpha =.*/$n3/" to 's/alpha =.*/"$n3"/' and a few other ways all that does is substitues alpha = -pi/... with $n3

Osher Goldman
  • 21
  • 1
  • 4
  • As you were already told in a previous thread, this sequence doesn't make any sense: `echo | bc | awk "BEGIN {print 180/$n1}"`. – lcd047 Jun 25 '15 at 07:29
  • 1
    Maybe you should rephrase your question to a more precise title, since you are posting with the *bash* tags, which makes people think of *variable* as a bash variable, not a variable assignment in an octave script. Remove all those things that are not necessary to have in your script. In principle you are trying to replace an entire line or variable assignment in a octave file. right? – jhoepken Jun 25 '15 at 07:29

1 Answers1

1

I do this very regularly as follows:

sed -i -e "s/alpha = .*/alpha = ${n3}/g" makemesh_rot.m

The -i parameter replaces things directly in-place, which renders the temporary file obsolete.

jhoepken
  • 1,842
  • 3
  • 17
  • 24