Can't assign float value into another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't assign float value into another variable
# 1  
Old 07-26-2012
Can't assign float value into another variable

hello
I have write one script which calculate result of student and takes input from file named "student.txt". This file has filed name rollno, name, mark1,mark2 and mark3.

My problem is that i want percentage with 2 floating point. I have write whole shell script but it can't work. Code is here.
Code:
echo "\n\n\tSTUDENT MARKSHEET"
echo "\t=================\n"
echo "NAME \t \t TOTAL \t \t PERCENTAGE \t GRADE "
echo "====================================================="
while [ $i -le $len ]
do
 record=`head -n $i student.txt | tail -n 1`
 total=0
 for (( j=3 ; $j <=5 ; j++ ))
 do
  marks=`echo $record | cut -d " " -f $j`
  total=`expr $total + $marks`
 done
 name=`echo $record | cut -d " " -f 1`
 per=`echo "scale=2 ; $total/3" | bc`                                         #this statement can't work
# per=`expr $total / 3`
if [ $per -ge 85 ] && [ $per -le 100 ] ; then
    grade="AA"
 elif [ $per -ge 75 ] && [ $per -le 84 ] ; then
    grade="AB"
 elif [ $per -ge 65 ] && [ $per -le 74 ] ; then
    grade="BB"
 elif [ $per -ge 55 ] && [ $per -le 64 ] ; then
    grade="BC"
 elif [ $per -ge 45 ] && [ $per -le 54 ] ; then
    grade="CC"
 elif [ $per -ge 0 ] && [ $per -le 44 ] ; then
    grade="FAIL"
else
    grade="-"
 fi

 echo "$name  $total  $per %  $grade"
 i=`expr $i + 1`
done

Errors like
13: line 21: [: 66.00: integer expression expected
13: line 23: [: 66.00: integer expression expected
13: line 25: [: 66.00: integer expression expected
13: line 27: [: 66.00: integer expression expected
13: line 29: [: 66.00: integer expression expected
13: line 31: [: 66.00: integer expression expected
# 2  
Old 07-26-2012
can you try with ksh

Code:
 
$ bash b.sh
66.00
b.sh: line 6: [: 66.00: integer expression expected

$ ksh b.sh
66.00
BB

$ cat b.sh
#!/bin/sh
a=66.00;
echo $a
if [ $a -ge 65 ] && [ $a -le 74 ] ; then
echo "BB"
fi

# 3  
Old 07-26-2012
No i am using Bash shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

4. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

5. Shell Programming and Scripting

Assign this to a variable....

bash-3.00$ /usr/bin/netstat -an -f inet | awk -F' ' '{if ($1$4 == "tcp*.21")print $5}' *.* bash-3.00$ A=` /usr/bin/netstat -an -f inet | awk -F' ' '{if ($1$4 == "tcp*.21")print $5}'` bash-3.00$ echo $A db2_lastdone.bkp As you can see ,after running command i get *.* in return but the same... (5 Replies)
Discussion started by: ak835
5 Replies

6. Solaris

i cannot assign float point numbers to an array in solaris

total=0 declare -a sum limit=`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g' | wc -l` echo "Limit is equal to $limit" ara="`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g'`" for (( i=1; i<=$limit; i++ )) do sum=`echo $ara | cut -d " " -f $i` echo ${sum}... (11 Replies)
Discussion started by: naree
11 Replies

7. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

8. Shell Programming and Scripting

assign a value to a variable

I have a list of names in a file. i want to assign those names to a variable in such a manner eg: $cat file.txt pete lisa john var=pete-lisa-john how do i do this in shell scripting? (10 Replies)
Discussion started by: Shivdatta
10 Replies

9. Shell Programming and Scripting

Assigning a shell variable as a float

I'm confused as to how to handle floating point numbers in shell scripts. Is there a way to convert a number (string) read into a shell variable so that it can be used as a floating point decimal for calculation purposes? Or am I stuck with integrating C or Perl into my script? Ex: --input ... (3 Replies)
Discussion started by: spieterman
3 Replies

10. UNIX for Dummies Questions & Answers

assign to variable

why i can't use this command: echo $arg | cut -c 1,2 | read remainArg or echo $arg | cut -c 1,2 | read $remainArg so that the result will be assign to remainArg. Anyway to do this? :) (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question