-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.mac
More file actions
131 lines (112 loc) · 2.96 KB
/
functions.mac
File metadata and controls
131 lines (112 loc) · 2.96 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#------------------------------------------------------------------------------#
#Title :Functions
#Author :Ivan Batistic
#Date :01/2023
#Version :0.1
#Bash_version :5.1.16(1)-release
#Description :My collection of bash shell functions
#------------------------------------------------------------------------------#
# Function used to check number of arguments
# Arguments: number of arguments, required number of arguments
# Returns: 1 if number of arguments not match required number
checkArgNum() {
if [[ $1 -ne $2 ]]; then
echo "$2 argument required, $1 provided" >&2
return 1;
fi
}
# Save working directory path, use wd to relocate terminal on it
# Arguments: None
# Outputs: Writes location to $HOME/.saved_dir
swd() {
checkArgNum $# "0"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
cwd=$(pwd)
echo $cwd > $HOME/.saved_dir
echo "Saved current working dir: $cwd"
}
# Change to directory saved using swd() function
# Arguments: None
# Outputs: Change terminal location to location saved in $HOME/.saved_dir
wd() {
checkArgNum $# "0"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
if [ -f $HOME/.saved_dir ]; then
cwd=$(<$HOME/.saved_dir)
cd $cwd
fi
}
# See the most used commands in terminal
# Arguments: None
# Outputs: Prints most used commands
printComandsUsage () {
checkArgNum $# "0"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -rn
}
# Find file and grep expression through it
# Arguments: file name and expression
# Outputs: locations of expression for given file name
findGrep () {
checkArgNum $# "2"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
find . -name "$1" | xargs -I {} grep -n --color "$2" {}
}
# Run emacs always in backgroud
# Globals: None
# Arguments: None
# Outputs: Run emacs & disown
#emacs () {
# /usr/bin/emacs "$@" &
# disown;
#}
# This makes it easier to separate normal status from actual issues.
# https://google.github.io/styleguide/shellguide.html
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
# Convert pdf to version which can be rendered on windows os
# Globals: None
# Arguments: old_version.pdf new_version.pdf
# Outputs: new_version.pdf
convertPdfVersion () {
checkArgNum $# "2"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
pdftocairo -pdf "$1" "$2"
}
# Convert pdf to svg
# Globals: None
# Arguments: name.pdf
# Outputs: svg.pdf
convertPdfToSVG () {
checkArgNum $# "1"
returnValue=$?
if [[ $returnValue -eq 1 ]]
then
return 1;
fi
output=$(echo "$1" | cut -f 1 -d '.')
pdftocairo -svg "$1" $output.svg
}
#------------------------------------------------------------------------------#