GTU OS Program - 13 Date Validator

13. Write a shell script to validate the entered date. (eg. Date format is : dd-mm-yyyy).

Complete code of the program

  1. echo "Date validator"
  2. #Initializing values of date, month and year
  3. dd=0
  4. mm=0
  5. yy=0
  6.  
  7. #initializing no of days in a month
  8. days=0
  9.  
  10. read -p "Enter day (dd) : " dd
  11. read -p "Enter Month (mm) : " mm
  12. read -p "Enter Year (yyyy) : " yy
  13.  
  14. #checking for invalid month
  15. if [ $mm -le 0 -o $mm -gt 12 ]
  16. then
  17. echo "$mm is invalid month. "
  18. exit 1
  19. fi
  20.  
  21. #finding out no. of days in a month
  22. case $mm in
  23. 1 | 3 | 5 | 7 | 8 | 10 | 12)
  24. days=31
  25. ;;
  26. 2)
  27. days=28
  28. ;;
  29. 4 | 6 | 9 | 11)
  30. days=30
  31. ;;
  32. *)
  33. days=-1
  34. ;;
  35. esac
  36.  
  37. #checking for leap year
  38. if [ $mm -eq 2 ]
  39. then
  40. a=`expr $yy % 4`
  41. b=`expr $yy % 100`
  42. c=`expr $yy % 400`
  43.  
  44. if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
  45. then
  46. days=29
  47. else
  48. break
  49. fi
  50. fi
  51.  
  52. if [ $dd -le 0 -o $dd -gt $days ]
  53. then
  54. echo "$dd day is invalid "
  55. exit 3
  56. fi
  57.  
  58. #No error means date is valid
  59. echo "$dd/$mm/$yy is a Valid Date"
  60.  

Interpreting the date.sh file and executing it in terminal

  1. tkanu025@hp:~/lab_solutions$ chmod +x date.sh
  2. tkanu025@hp:~/lab_solutions$ ./date.sh
Output
gtu study material, free gtu study material for engineering students

gtu os lab manual,

os GTU Practical,

operating system Programs,

Operating system shellscript programs,

Gtu study material 3140705,

OS 3140702,

os practical list,

shell script programming,

gtu os practical,

shell script to validate the entered date. (eg. Date format is : dd-mm-yyyy).

os lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OS Program - 11 Filters

GTU OOP Program - 17 tic-tac-toe