Floating point in CSH?

I have been trying to run this script, but it gives the error "badly formed number" due to the decimal 0.2224. If I replace it with any integer, the script works fine.

#!/bin/csh -f
set shift="-1 2 "
foreach d ($shift)
@ z = $d * "0.2224" awk -v k=$d 'FNR==3{$1+=4*k;}1' Lat | cat >>head awk -v k=$d '{$2+=k;}1' f1 | cat >>g1 awk -v k=$d '{$2+=3*k;}1' f2 | cat >>g2
cat head g1 g2 > POSCAR-$z
rm head
rm g1
rm g2
3

2 Answers

Shell variables are strings, not numbers. When you type 2 or "0.2224", it is seen as a string. The shell can handle some simple data conversion, so decimal expressions may work; but floating point values will not work.

To do math in the shell, you can pipe the values through bc and combine that with command substitution (the backtick wrappers around the command):

set d = 2
set result = `echo "$d * 0.2224" | bc`
echo $result

The syntax of this example is written for csh/tcsh, but the principle works in bash as well. If you need something more complicated than this, you may want to implement your script in python instead.

The statement " set shift="-1 2 " " causes this error "badly formed number". In csh scripts there should be space between operational characters and variables or number. So it should be " set shift = "-1 2".

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like