-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_functions.sh
More file actions
80 lines (70 loc) · 1.84 KB
/
bash_functions.sh
File metadata and controls
80 lines (70 loc) · 1.84 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
# Colors
GREEN='\033[0;32m'
NOCOLOR='\033[0m'
# Return a 0 status if on a Mac OS.
isMacOS() {
case `uname -s` in
Darwin) return 0 ;;
*) return 1 ;;
esac
}
# Print a message in green
printGreen() {
local message="$1"
printf "${GREEN}[+] ${message}${NOCOLOR}\n"
}
# If the destination file does not exist download from origin to destination
checkOrDownload() {
local origin="$1" destination="$2"
if [ ! -f "$destination" ]; then
printGreen "Could not find $destination. Installation..."
# Use wget or curl
if [ $(command -v wget) ]
then wget "$origin" -O "$destination"
else curl -o "$destination" "$origin"
fi
fi
}
# Check if a directory exists or create the full path
checkOrMakeDir() {
local directory="$1"
[ -d $directory ] || mkdir -p $directory
}
# Make a symlink to origin in destination if this does not exist
makeSymLink() {
local origin="$1" destination="$2"
if [ ! -f "$destination" ]
then
ln -sn "$origin" "$destination"
printGreen "Linked $origin to $destination."
else printGreen "The file $destination already exists."
fi
}
# Return 1 if it can't find a command.
# https://stackoverflow.com/a/26759734/7874784
commandNotFound() {
local _command="$1"
[ ! -x "$(command -v $_command)" ]
}
# Return 1 if it can't find a file.
fileNotFound() {
local _file="$1"
[ ! -f $_file ]
}
# Return 1 if it can't find a directory.
dirNotFound() {
local _file="$1"
[ ! -d $directory ]
}
# Append a string to a file, skip if the string already exists.
appendStringToFile() {
local string="$1"
local filename="$2"
if [[ ! $(grep "$string" "$filename") ]]; then
echo "$string" >> "$filename"
fi
}
# Fuzzy-search git branches
gch() {
git checkout "$(git branch --all | fzf | tr -d '[:space:]')"
}