-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout
More file actions
73 lines (60 loc) · 1.43 KB
/
Copy pathcheckout
File metadata and controls
73 lines (60 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#Config
##Default length
LEN=5
##Default directory
DEFAULT_DIRECTORY=~/
#-- Uncomment the line below to enable default working directory --#
#cd $DEFAULT_DIRECTORY
#######################
POSITIONAL=()
for i in "$@"
do
case $i in
-l|--length)
LEN="$2"
if [[ ! "$LEN" =~ ^[0-9]+$ ]]; then
echo "please supply a number for the length"
exit
fi
shift # past argument
;;
*:*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ $# -eq 0 ]
then
echo "Usage: checkout [OPTION] PATH:LINENUMBER"
exit
fi
DIR="$(cut -d':' -f1 <<< $1)"
LINE="$(cut -d':' -f2 <<< $1)"
if [[ ! -f "$DIR" ]]; then
echo
echo "\"$DIR\" is not a valid file-name"
echo
echo "Usage: checkout [OPTION] PATH:LINENUMBER"
echo "-------------------------------------------"
echo
echo "Options: -l, --length number of lines printed above and below"
echo
exit
fi
if [[ ! "$LINE" =~ ^[0-9]+$ ]] ; then
echo
echo "\"$LINE\" is not a valid number"
echo
echo "Usage: checkout [OPTION] PATH:LINENUMBER"
echo "-------------------------------------------"
echo
echo "Options: -l, --length number of lines printed above and below"
echo
exit
fi
MIN=$(echo $(expr $LINE - $LEN) | sed -e 's/^-.*/1/' -e 's/^0/1/')
MAX=$(expr $LINE + $LEN)
cat -n $DIR | sed -n "$MIN,$MAX p" | ack --passthru " $LINE "