-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·303 lines (273 loc) · 7.55 KB
/
functions.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
if [[ ${SKIP} == 1 ]]; then
return 1
fi
#return 1
function wait-for-key-press() {
echo "Press any key to continue"
while [ true ] ; do
read -t 1 -n 1
if [ $? = 0 ] ; then
exit ;
else
continue # echo "waiting for the keypress"
fi
done
}
function get-ip() {
interface=${1:-""}
silent=${2:-""}
#got_ip=$(ifconfig ${interface} | sed -En 's/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
got_ip=$(ifconfig ${interface} | sed -En 's/127.0.0.1//;s/172.17.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
if [ -z "$silent" ]; then # If it is empty: add current field
echo $got_ip
fi
}
function kill-tmux-gz() {
tmux kill-session -t test
kg
}
function mount-image() {
image=${1:-""}
if [[ -z "${image}" ]]; then
printf "${RED_TXT}Image file not specified.${NC}\n"
else
printf "Mounting ${LIGHT_BLUE_TXT}$image ${NC}.\n"
dirname=${image}.d
mkdir $dirname
sudo mount -o loop $image $dirname
# echo $lstring >$LAUNCH_FILE
fi
}
function set-test-launch() {
lstring=${1:-""}
if [[ -z "${lstring}" ]]; then
printf "${RED_TXT}ROS launch file not specified.${NC}\n"
else
printf "Saving the launch ${LIGHT_BLUE_TXT}$lstring${NC} for tests.\n"
echo $lstring >$LAUNCH_FILE
fi
}
function get-test-launch() {
curr_test_launch=$(cat $LAUNCH_FILE)
}
function test-launch() {
get-test-launch
printf "Launching ${LIGHT_BLUE_TXT}$curr_test_launch${NC}\n"
tmux new -s test -d "${curr_test_launch}"
}
function s() {
lt=${1:-100}
usr=${2:-nvidia}
ssh_host="${usr}@192.168.131.${lt}"
printf "${BLUE_TXT}Initiating ssh connection to ${ssh_host}.${NC}\n"
ssh ${ssh_host}
}
function mountTX() {
bird_num=${1:-""}
if [[ -z "${bird_num}" ]]; then
printf "${RED_TXT}Bird number not specified.${NC}\n"
else
printf "${BLUE_TXT}Specified bird number: ${bird_num}.${NC}\n"
cmd="sshfs [email protected].${bird_num}:/home/nvidia /home/${USER}/TX"
printf "Executing: ${cmd}\n"
${cmd}
fi
}
function unmountTX() {
sudo umount /home/yossi/TX
}
# QuickEdit files
QE_EDITOR="atom "
QE_FILES_LIST="/home/yossi/Dropbox/zis_iz_backup/qt-files.txt"
function qe-file() {
get_files_list
ask_for_num $cnt
for i in "${fs_arrIN[@]}"; do
c=$(($c + 1))
if [[ $c == $num ]]; then
$QE_EDITOR $i
fi
done
}
function get_files_list() {
fs=$(cat ${QE_FILES_LIST})
fs_arrIN=(${fs// / })
printf "${GREEN_TXT}QuickEdit files in ~${QE_FILES_LIST}${NC}\n"
# Search for longest ws name
max_l=0
for i in "${fs_arrIN[@]}"; do
l=$(expr length "$i")
# echo $(($l)), $(($max_l))
if [[ $(($l)) -gt $(($max_l)) ]]; then
# echo "New line is longer"
max_l=$(($l))
fi
done
pad=""
c=$(($max_l + 2))
while [[ $c > 0 ]]; do
pad="$pad-"
c=$((c - 1))
done
cnt=0
printf "|%-5s|%-$(echo $max_l)s|\n" "-----" "$pad"
printf "| %-3s | %-$(echo $max_l)s |\n" "NUM" "FILE NAME"
printf "|%-5s|%-$(echo $max_l)s|\n" "-----" "$pad"
for i in "${fs_arrIN[@]}"; do
cnt=$(($cnt + 1))
printf "| ${NC}%-3s${NC} | ${WHITE_TXT}%-$(echo $max_l)s${NC} |\n" "$cnt" "$i"
done
printf "|%-5s|%-$(echo $max_l)s|\n" "-----" "$pad"
}
function ask_for_num() {
max=${1:-"1"}
if [[ $(($max)) -lt 10 ]]; then
read -n 1 -p "Select a number of file to edit [1-$max] or cancel : " num
else
read -n 2 -p "Select a number of file to edit [1-$max] or cancel : " num
fi
case $num in
[123456789]*)
echo ""
# echo "Sourcing WS #$num"
;;
# [Cc]*)
# echo ""
# return 1
# ;;
*)
echo ""
echo "Cancelling."
;;
esac
}
# Function sets rosmster. Either specify last byte for Mobilicom pool, or specify full ip and second argumen 1
function RM() {
ip=${1:-100}
full_ip=${2:-0}
if [ ${full_ip} != 1 ]; then
ip="192.168.131.${ip}"
fi
arg="ROS_MASTER_URI=http://${ip}:11311"
printf "Setting environment variable: ${arg}\n"
export ${arg}
}
function upd_desktop_shortcuts() {
ln -nsf ~/Dropbox/zis_iz_backup/DesktopLinks/*.desktop ~/Desktop/
ln -nsf ~/.local/share/applications/jetbrains-clion.desktop ~/Desktop/
}
function udp() {
address=${1:-0}
port=${2:-0}
msg=${3:-0}
if [ ${address} == 0 ]; then
exit 1
fi
if [ ${port} == 0 ]; then
exit 1
fi
if [ ${msg} == 0 ]; then
msg="Empty string"
fi
echo "Sending message to ${address}:${port} with contents [${msg}]"
echo -n ${msg} >/dev/udp/${address}/${port}
}
function mcd() {
dirname=${1:-0}
if [ ${dirname} == 0 ]; then
echo "No folder specified."
else
mkdir -p ${dirname}
cd ${dirname}
fi
}
function t() {
text=${1:-0}
if [ ${text} == 0 ]; then
printf "No text pattern specified.\nUsage: ${BLUE_TXT}t <pattern>${NC} := ${BLUE_TXT}grep -r <pattern> ./${NC} \n"
else
grep -r "${text}" ./
fi
}
function size() {
dirname=${1:-"*"}
du -hs ${dirname}
}
# function d() {
# directory=${1:-""}
# du -h --max-depth=1 ${directory}
# du -sh * # Another mode
# }
function tm() {
# Use iteration to eliminate quotes need:
# { printf '"%s" ' "$@"; echo ""; }
cmd=${1:-""}
if [[ -z "${cmd}" ]]; then
printf "${RED_TXT}No command specified.${NC}\n"
printf "Usage:\ntm 'kate tempfile.txt'\n"
else
tmux new -d "${cmd}"
fi
}
function tcp_listen() {
echo $(lsof -n -i4TCP:$1 | grep LISTEN)
}
function whosaround() {
ips=$(get-ip | grep -oP "(\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})" | sort -u)
printf "Found IP addresses: ${WHITE_TXT}"
echo $ips
printf "${NC}\n"
ips=$(get-ip | grep -oP "(\d{1,3}\.\d{1,3}\.\d{1,3})" | sort -u)
arrIN=(${ips// / })
for i in "${arrIN[@]}"; do
range=${i}.0/24
printf "${LIGHT_BLUE_TXT}\nScanning range: $range.${NC}\n"
nmap -sn $range
done
}
function h() {
str=${1:-""}
if [[ -z "${str}" ]]; then
history
else
history | grep -i ${str}
fi
}
function process_info() {
PID=${1:-""}
if [[ ! -z "${PID}" ]]; then
ps -p ${PID} ww
else
printf "No process ID specified.\n"
fi
}
function pinfo(){
name=${1:-""}
if [[ ! -z "${name}" ]]; then
ps -ef | grep ${name}
else
printf "No process name specified.\n"
fi
}
function my_info() {
# Displays help hints
printf "\n${LIGHT_BLUE_TXT}Information:${NC}\n"
echo "check aliases by typing 'alias'"
echo "Implemented functions:"
printf "${WHITE_TXT}s XXX${NC}: connects by ssh to host 192.168.131.XXX\n"
printf "${WHITE_TXT}RM XXX${NC}: sets ROS_MASTER_URI environment variable to point to: http://192.168.131.XXX:11311\n"
printf "${WHITE_TXT}RM XXX 1${NC}: sets ROS_MASTER_URI environment variable to point to: http://XXX:11311\n"
printf "${WHITE_TXT}get-ip XXX${NC}: sets 'got-ip' variable to include ip addtess for network interface XXX\n\t\tIf no arguments specified, returns all but localhost.\n"
printf "${WHITE_TXT}fixJB${NC}: Updates the shortcuts for JetBrains projects to be compatible with ROS.\n"
get_ip
}
preexec() {
cmd_start="$SECONDS"
}
precmd() {
local cmd_end="$SECONDS"
elapsed=$((cmd_end - cmd_start))
print "Elapsed: ${elapsed}"
# PS1="$elapsed "
}