-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-path
More file actions
executable file
·64 lines (47 loc) · 1.36 KB
/
add-path
File metadata and controls
executable file
·64 lines (47 loc) · 1.36 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
#!/usr/bin/tclsh
# This tool can be used to complete a string comprising a list of paths
# separated by colon, such as the PATH environment variable. For example,
# if you want to add your $HOME/.local/bin path to your PATH variable, use
# the following:
# PATH=`add-path $PATH $HOME/.local/bin`
# What makes it different to "PATH=$HOME/.local/bin:$PATH" is that it will
# never leave a loose : in the value and will never add given path multiple times.
set args [lassign $argv pathcontainer]
if { $pathcontainer == "" } {
puts stderr "Usage: [file tail $argv0] <existing-pathcontainer> <path-to-add>..."
puts stderr "Options:"
puts stderr "\t--end: put given paths at the end of list (lower priority)\n"
puts stderr "(The resulting path is printed on stdout)"
exit 1
}
# Supported flags
set flags(--end) 0
foreach a $args {
if { [string match --* $a] } {
set flags($a) 1
} else {
lappend newpaths $a
}
}
set atend $flags(--end)
set pathitems [split $pathcontainer :]
# Make the path unique in the first place
set target ""
foreach p $pathitems {
if { [lsearch $target $p] != -1 } {
continue
}
lappend target $p
}
set pathitems $target
# Ok, let's add the paths now
foreach p $newpaths {
if { [lsearch $pathitems $p] == -1 } {
if { $atend } {
lappend pathitems $p
} else {
set pathitems [linsert $pathitems 0 $p]
}
}
}
puts [join $pathitems :]