michael_hartman_cz

FIT SKJ printline

Jun 2nd, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #test if 3 arguments were specified
  4. if [[ $# != 3 ]]
  5. then
  6.     exit 1;
  7. fi
  8.  
  9. #test if $3 is readable file
  10. if [[ ! -r "$3" || ! -f "$3" ]]
  11. then
  12.     exit 2;
  13. fi
  14.  
  15. #test if $1 is positive number
  16. if [[ ! $1 =~ ^([1-9][0-9]*|0)$ ]]
  17. then
  18.     exit 2;
  19. fi
  20.  
  21. #test if $2 is positive number
  22. if [[ ! $2 =~ ^([1-9][0-9]*|0)$ ]]
  23. then
  24.     exit 2;
  25. fi
  26.  
  27. #test if $2 is at least $1
  28. if [ $2 -lt $1 ]
  29. then
  30.     exit 2;
  31. fi
  32.  
  33. #test if file have enough lines
  34. lines=$(wc -l "$3" | awk '{print $1}')
  35. if [ $lines -lt $2 ]
  36. then
  37.     exit 2;
  38. fi
  39.  
  40. #print desired lines
  41. awk -v from=$1 -v to=$2 '{if (NR >= from && NR <= to) print $0;}' "$3"
  42. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment