7

I have a file which contains only one large number. I need to save this number in an integer variable in order to use it in mathematical operations.

I've tried: var=$(<filename) but it saves it as string.

5
  • I don't understand what the problem is. One thing you can do is declare -i var=$(<file) (assuming bash), but that does not gain you a whole lot. Commented Apr 10, 2015 at 19:44
  • I've tried that but it didn't work.. I need to check if the number is prime and some other operations so I need to have it as integer so i can do "$var+2" as a simplest case Commented Apr 10, 2015 at 21:22
  • And what have you tried that doesn't work? Commented Apr 11, 2015 at 0:52
  • Can you provide a link to where you got the notion there is an integer variable in shells (bash e.g.)? Commented Apr 12, 2015 at 3:57
  • @Rayya bash has no 'typed' variables, only ways to tell the interpreter how you wish to use it. If you have a number in filename, you get it in var. You can confirm with printf " int: '%d' string: '%s' var-1 : '%d'\n" "$var" "$var" $((var - 1)) Commented Apr 12, 2015 at 4:01

3 Answers 3

13

There are no integer variables in common shellscript (bash, sh) -- all variables are either strings or arrays. So just use the variable normally:

$ echo 2 > test
$ X=$(< test)
$ Y=$(($X + 2))
$ echo $Y
4
Sign up to request clarification or add additional context in comments.

2 Comments

I need to use it inside a new var not inside "echo".. when I use inside this code to check if it's prime or not it stops at "expr" because it's a string even if I wrote it as $(($var/2)) for example i=2 f=0 while [ $i -le expr $num / 2 ] do if [ expr $num % $i -eq 0 ] then f=1 fi i=expr $i + 1 done if [ $f -eq 1 ] then echo "The number is composite" else echo "The number is Prime" fi
echo or var does not matter. I just updated the example to show how to use it in a var. I cannot read your sample code as it lost all formatting, but your problem must be elsewhere.
1

If I understand you are attempting to read the value from a file and check whether it is prime or not, your problem isn't the value in the file. The problem is your shell syntax. Following along from your questions and comments, it looks like you are trying to do the following:

#!/bin/sh

num=$(<"$1")

i=2
f=0

while [ $i -le $(expr $num / 2) ]; do 
    if [ $(expr $num % $i) -eq 0 ]; then 
        f=1
    fi 
    i=$(expr $i + 1)
done 

if [ $f -eq 1 ]; then 
    echo "The number is composite" 
else 
    echo "The number is Prime" 
fi

Use/Output

$ cat dat/pnumber.txt
31

$ sh prime.sh dat/pnumber.txt
The number is Prime

$cat dat/npnumber.txt
32

$ sh prime.sh dat/npnumber.txt
The number is composite

The problem was you failed to recognize that expr is a separate command and must be enclosed in either backticks or $() to return the value of the expression to a variable or use it in test constructs.

If you would rather use the older backtick command substitution syntax, then the following is equivalent to what is shown above:

while [ $i -le `expr $num / 2` ]; do 
    if [ `expr $num % $i` -eq 0 ]; then 
        f=1
    fi 
    i=`expr $i + 1`
done 

Comments

-1

I had to use python calculations because float are not supported in bash arithmetics:

CPU_TEMPERATURE_fOffset=/sys/bus/iio/devices/iio:device0/in_temp0_offset
CPU_TEMPERATURE_fScale=/sys/bus/iio/devices/iio:device0/in_temp0_scale
alias QA_READ_ZINQ_TEMP="echo -n "fRaw="; sudo cat $CPU_TEMPERATURE_fRaw;echo -n "fOffset=";sudo cat $CPU_TEMPERATURE_fOffset;echo -n "fScale=";sudo cat $CPU_TEMPERATURE_fScale;"
F_QA_ZINQ_TEMP_DIAG() 
{ 
    QA_READ_ZINQ_TEMP 
    echo "Calculating temperature using formula:  ((raw + offset) * scale) / 1000"
    raw=$(< $CPU_TEMPERATURE_fRaw)
    offset=$(< $CPU_TEMPERATURE_fOffset)
    scale=$(< $CPU_TEMPERATURE_fScale)
    echo -n "Temperature is: "
    python_command="print((($raw+$offset)*$scale)/1000)"
    python3 -c $python_command
}
alias QA_ZINQ_TEMP_DIAG=F_QA_ZINQ_TEMP_DIAG


Using it worked for me:


$ QA_ZINQ_TEMP_DIAG
fRaw=2536
fOffset=-2219
fScale=123.040771484
Calculating temperature using formula:  ((raw + offset) * scale) / 1000
Temperature is: 39.742169189332



1 Comment

This looks like the answer to a completely different question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.