-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmark.sh
More file actions
112 lines (100 loc) · 3.22 KB
/
bmark.sh
File metadata and controls
112 lines (100 loc) · 3.22 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
#- =======================================================
#- create bookmark to current working folder
# to allow to cd to folder, this script has to be a function
# that has to be sourced in your .bashrc (or private,...)
# e.g.:
# $ cat ~/.bashrc
# mystuff
# source ~/my/folder/to/bmark.sh
# morestuff
#
# Once set up, you can view the options by running:
# bmark -h
#
#- =======================================================
function bmark() {
#- =======================================================
#- set library and default action
bmarkfile=$(echo $(dirname $(which bmark.sh))/.bookmarks)
[ ! -f $bmarkfile ] && touch $bmarkfile
action='open'
#- =======================================================
#- read options
local OPTIND OPT h s o r l
while getopts hsorl OPT; do
case $OPT in
s) action='set';;
o) action='open';;
r) action='remove';;
l) action='list';;
h) cat << EOM
program: $(basename $(which bmark.sh))
author: nicolas celli 2020
purpose: create, set and change bookmarks for quick access
folders. It will always bookmark the current
directory.
syntax:
bmark <action> my_bookmark_name
action options:
-h -> prints this help
-s -> adds/replaces a bookmark to current directory
-o -> opens (go to) bookmarked directory [default]
-r -> removes current bookmark from library
-l -> lists bookmarks currently in the library
library location:
$(realpath $bmarkfile)
current bookmarks:
$(cat $bmarkfile)
EOM
return;;
esac
done
#- =======================================================
#- read args
shift $((OPTIND - 1))
bname=${1}
case $action in
#- =====================================================
#- save/replace new bookmark
set)
#- get working directory
wd=`realpath . | tr -d '\n'`
#- replace
if [ $(grep -w $bname $bmarkfile | wc -l) -gt 0 ]; then
(grep -v -w $bname $bmarkfile; printf "%-10s %-100s\n" $bname $wd) > $(dirname $bmarkfile).bmarktmp
mv $(dirname $bmarkfile).bmarktmp $bmarkfile
echo "replacing bookmark $bname:"
echo "old bookmark: "$(awk -v a=$bname '{if($1==a) print $2}' $bmarkfile)
echo "new bookmark: "$wd
#- set
else
printf "%-10s %-100s\n" $bname $wd >> $bmarkfile
echo "setting new bookmark $bname: "$wd
fi;;
#- =====================================================
#- go to bookmark
open)
#- get bookmark from directory and go
if [ $(grep -w $bname $bmarkfile | wc -l) -gt 0 ]; then
targetdir=$(awk -v a=$bname '{if($1==a) print $2}' $bmarkfile)
cd $targetdir
else
echo "Error: no bookmark $bmark found; available bookmarks:"
cat $bmarkfile
return
fi;;
#- =====================================================
#- remove bookmark from list
remove)
echo "removing bookmark $bname: " $(awk -v a=$bname '{if($1==a) print $2}' $bmarkfile)
grep -v -w $bname $bmarkfile > $(dirname $bmarkfile).bmarktmp
mv $(dirname $bmarkfile).bmarktmp $bmarkfile;;
#- =====================================================
#- list bookmarks
list)
echo "Existing bookmarks in library:"
cat $bmarkfile
echo "";;
esac
}