Unix shell script problem

March 1, 2011 at 11:26:24
Specs: UNIX
Here is my script homework assignment. Its suppose to create a file student.txt, then record the last name, first name and grade for 5 students. Then display that file. Any help would be welcome

#!/bin/bash
if [ -s $student.txt ] #True if the file exists and is not a directory
then rm student.txt #to remove file "to remove a file"
fi
count=1
while[ $count -le 5]
do echo Enter the first name # for displying string "Enter the first name"
read fname #read first name from key board
echo Enter the last name
read lname #read last name from key board
flag=1
while [ $flag -ne 0 ] #if the grade is not properly entered(grade should be 0-100) , then again grade shold entered correctly
do echo Enter the Grade, Grade should be 0-100 read grade
#read grade name from key board
if [ `expr $grade` -ge 0 && `expr $grade` -le 100 ] #checks grade value
then
cat student.txt >> $fname $lname $grade #create student.txt file and then insert/append fname,lname,grade to that file
flag=0 #change the flag value for end the loop
fi done
done
cat student.txt #display the student.txt


See More: Unix shell script problem

Report •

#1
March 1, 2011 at 12:41:13
I will give you some pointers to get you started:

First, student.txt is probably a file name and not a variable as you have defined: $student.txt

Second, although not necessary, I would place the first statement after the if statement on it's own line:

#!/bin/bash

if [ -s student.txt ] #True if the file exists and is not a directory
then 
   rm student.txt #to remove file "to remove a file"
fi

Third, when echo'ing a string, surround it by quotes.
echo "Enter the grade"

Fourth, what is the expr all about:

if [ `expr $grade` -ge 0 && `expr $grade` -le 100 ]

# expr is an obsolete method for doing shell arithmetic.

if you want to compare $grade, this should work:

# untested
read grade

if [ $grade -ge 0 && $grade -le 100 ]

Fifth:

If you are trying to create a student.txt file, this is incorrect:
cat student.txt >> $fname $lname $grade

Try something like this:

echo "fname $lname $grade" >> student.txt


Report •

#2
March 1, 2011 at 14:27:47
Nails thanks for the reply :)

Based on suggestions I fixed and here it is, but when I run the script, I get this after this script runs

$ $ > > > $ $ ksh: while[$count -le 5] not found
$ Enter the first name
$ $ $ > > > > > > > > > xt
>

______________________________________________________
Here is my fixed script

#!/bin/bash

if [ -s student.txt ] #True if the file exists and is not a directory
then
rm student.txt #to remove file "to remove a file"
fi
count=1
while[ $count -le 5]
echo Enter the first name # for displaying string "Enter the first name"
read fname #read first name from key board
echo Enter the last name
read lname #read last name from key board
flag=1
while [ $flag -ne 0 ] #if the grade is not properly entered(grade should be 0-100) , then again grade should entered correctly
echo "Enter the Grade", Grade should be 0-100 read grade
#read grade name from key board
if [ $grade -ge 0 && $grade -le 100 ] #checks grade value
then
echo $fname $lname $grade >> student.txt file and then insert/append fname,lname,grade to that file
flag=0 #change the flag value for end the loop
fi

cat student.txt #display the student.txt


Report •

#3
March 1, 2011 at 14:36:46
while loops need a do clause and they need to be terminated with done:

while [ $count -le 5 ]
do 
   .
   .
done

Also, the spaces around the brackets are required.


Report •

Related Solutions

#4
March 1, 2011 at 15:25:25
Your the best Nails thank you

Report •

#5
March 1, 2011 at 17:36:58
Your welcome, and thank you for the kind words. I gotta have you talk to my boss :)

Report •

Ask Question