using bc in a script

Hello. I am trying to write a simple script (one of my attempts is below) to multiply the contents of a file by -1. I have a folder filled with many files and these txt files are each a single column of numbers and I just need to flip the signal to negative numbers and save out the inverted version of each. I am unable to write a cygwin script to do it properly. Any assistance is appreciated.

for file in *txt
do
$file | bc -q
$file *-1 > $file-inverted.txt
done
echo "files inverted"

You can experiment a little with how you want your data inside double-quotes, but you need to make sure the unix environment does not attempt to interpret your text. Think that a -1 is very similar to an option in appearance. [For example, the command ls -1 would list files in one column.]

> echo "5" "*" "-1" | bc
-5
> echo "-55" "*" "-1" | bc
55

Thank you for the response. I have experimented with the quotes and it still isn't working properly. It seems to be having trouble with the use of $file in conjunction with bc.

I am fairly new to unix scripts, so forgive me if I'm missing something obvious.:slight_smile:

An alternative with awk:

for file in *txt
do
  awk '{$0*=-1}1' ${file} > ${file}-inverted.txt
done
echo "files inverted"

input file

> cat file161
10
-13
1962
-1991

script

> cat play161.sh
#! /usr/bin/bash

while read NUM_IN
   do
   INITIAL=`echo ${NUM_IN}`
   NEW_VAL=`echo "${NUM_IN} * -1" | bc`

   echo ${INITIAL} " and " ${NEW_VAL}
done <file161

script execution

> play161.sh
10  and  -10
-13  and  13
1962  and  -1962
-1991  and  1991

the awk alternative works great. thanks for the tip, I've never used awk for math before.