Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Variable Declarations!
  4. word_count=$(wc -l $3 | cut -d ' ' -f 1)
  5.  
  6. #Error Checking!
  7. # check if there are exactly 3 arguments given
  8. if [[ "$#" -ne 3 ]]; then
  9. echo "Error: "$0" needs 3 arguments!"
  10. echo "usage: "$0" int int file_name"
  11. exit
  12. elif [[ "$#" -eq 3 ]]; then
  13.  
  14. # check if file exists
  15. if [[ -e "$3" ]]; then
  16.  
  17. # check if the first num is less than or equal to the second num
  18. if [[ "$1" -le "$2" ]]; then
  19.  
  20. # check if the number of lines in the file is greater than the last line to be printed
  21. if [[ $word_count -gt $2 ]]; then
  22. head -"$1" "$3"| tail -"$2"
  23. else
  24. echo "Error! File has less lines than the last line to be printed, print less lines!"
  25. exit
  26. fi
  27.  
  28. else
  29. echo "Error! Invalid line paramaters: first argument must be less than the second!"
  30. exit
  31. fi
  32.  
  33. else
  34. echo "Error: File does not exist!"
  35. exit
  36. fi
  37.  
  38. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement