Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Unix Shell script  (Read 4620 times)

0 Members and 1 Guest are viewing this topic.

Tinashell.script

  • Guest
Unix Shell script
« on: March 01, 2011, 12:15:31 PM »

 I wrote this shell script as part of a homework assignment but having problems with it running, any insight on this would be welcomed.   

 This script is suppose to create a file called student.txt and then capture the names and grades of 5 students and then display the contents of the file.

#!/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

quirkasaurus



    Greenhorn

    Thanked: 1
    • Experience: Beginner
    • OS: Unknown
    Re: Unix Shell script
    « Reply #1 on: March 10, 2011, 07:43:44 AM »

    #!/bin/bash
    #----------------------------------------------------------------------#
    # True if the file exists and is not a directory                       #
    #----------------------------------------------------------------------#
    if [ -s student.txt ]; then

    #----------------------------------------------------------------------#
    # remove file                                                          #
    #----------------------------------------------------------------------#
      rm student.txt
    fi



    count=1
    while [ $count -le 3 ]; do

    #----------------------------------------------------------------------#
    # for displying string "Enter the first name"                          #
    #----------------------------------------------------------------------#
      echo Enter the first name
      read fname
      echo Enter the last name
      read lname
      flag=1

    #----------------------------------------------------------------------#
    # if the grade is not properly entered(grade should be 0-100),         #
    # then again grade shold entered correctly                             #
    #----------------------------------------------------------------------#
      while [ $flag -ne 0 ]; do

        echo Enter the Grade, Grade should be 0-100 read grade
        read grade

    #----------------------------------------------------------------------#
    # checks grade value                                                   #
    #----------------------------------------------------------------------#
        if [ $grade -ge 0 -a $grade -le 100 ]; then

    #----------------------------------------------------------------------#
    # create student.txt file and then insert/append fname,lname,grade to  #
    #----------------------------------------------------------------------#
          echo $fname $lname $grade >> student.txt

    #----------------------------------------------------------------------#
    # change the flag value for end the loop                               #
    #----------------------------------------------------------------------#
          flag=0
        fi
      done

    #----------------------------------------------------------------------#
    # increment counter.                                                   #
    #----------------------------------------------------------------------#
      (( count = count + 1 ))

    done

    #----------------------------------------------------------------------#
    # display the student.txt                                              #
    #----------------------------------------------------------------------#
    cat student.txt






    lots of minor mistakes.
    modified your syntax to look more like other languages such as perl, C, awk, etc...
    this'll make learning new languages easier in the long run.
    Also, added the comment block style. The extra characters are nothing for today's computers,
    and hopefully you see how well the comments "pop" out of the programming text this way.

    You may want to do a "diff your_script my_script" to see all the differences.
    Some were subtle.