Saved Bookmarks
| 1. |
Solve : Unix Shell script? |
|
Answer» 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 #!/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. |
|