-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-root-dir.sh
53 lines (41 loc) · 1.1 KB
/
get-root-dir.sh
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
#!/bin/zsh -l
# I'm not very good with shell, sorry
config="$(jupyter --config-dir)/jupyter_notebook_config.py"
if [ -f "$config" ]; then
line=$(grep "^c.NotebookApp.notebook_dir" $config | tail -1)
# if the line with notebook is found in the config file
if [ "$line" != "" ]; then
# find start and quote type
for (( i=26; i<${#line}; i++ )); do
ch="${line:$i:1}"
if [ "$ch" = \" ]; then
quote=\"
start=$((i + 1))
break
fi
if [ "$ch" = \' ]; then
quote=\'
start=$i
break
fi
done
# find end
for (( i=${#line}; i>$start; i-- )); do
ch="${line:$i:1}"
if [ "$ch" = "$quote" ]; then
end=$i
break
fi
done
root=${line:$start:$(($end-$start))}
else
root=$HOME
fi
else
root=$HOME
fi
# check for the last '/'
if [ "${root:$((${#root}-1)):1}" = "/" ]; then
root="${root:0:$((${#root}-1))}"
fi
echo $root