-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
1211 lines (1047 loc) · 42.2 KB
/
.bashrc
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias folders='ls -d */'
alias ocho='printf "%s"' # echo without newline
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# shows hostname and file path
# export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
# shows file path but no host name
export PS1="\[\033[32m\]\w\[\033[36m\]\$(parse_git_branch)\[\033[34m\] $\[\033[00m\] ";
# exporting go env vars and update the $PATH
# by exporting these, you can use any binary you install in $GOPATH/bin, for example with:
# $ go get github.com/go-delve/delve/cmd/dlv
# you can recall directly 'dlv' command through your terminal:
# $ dlv version
# 'which go' returns /usr/local/go/bin/go
export GOROOT=$(go env GOROOT) # /usr/local/go
export GOPATH=$(go env GOPATH) # /Users/alessandro.argentieri/go
export PATH=$GOPATH/bin:$PATH
export PATH=$PATH:$GOROOT/bin
# needs: go install github.com/vc60er/deptree@latest
# shows the golang project dependency tree:
# usage:
# $ gotree 4 # max depth: 4
alias gotree='go mod graph | deptree -d'
alias uninstall-go='rm -rvf `go env GOROOT`'
# upgrades the go version downloading the new version with go itself
# usage:
# upgrade-go 1.22.0
upgrade-go() {
if [ $# -ne 1 ]; then
echo "Usage: $ upgrade-go 1.22.0"
return 1
fi
NEW_GO_VERSION=$1
go install golang.org/dl/go$NEW_GO_VERSION@latest
go$NEW_GO_VERSION download
sudo mv "$(which go$NEW_GO_VERSION)" "$(which go)"
}
alias unistall-brew='/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"'
alias install-brew='/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"'
# creates a lowercase uuid, saves into the clipboard and shows it in the terminal by pasting it
alias uuid="uuidgen | tr -d '\n' | tr '[:upper:]' '[:lower:]' | pbcopy; pbpaste"
alias docker-ip='sudo docker inspect --format="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}"'
alias docker-ids='docker ps -q' # docker ps | cut -d " " -f 1
alias docker-ps='docker ps --format "{{.ID}} --- {{.Names}}"'
alias docker-images='docker images --format="{{.ID}} {{.Repository}}"'
alias docker-stop-all='docker stop $(docker ps -q) 2>/dev/null || echo "No containers running"'
# returns the containers attached to a given network
# usage:
# docker-network-details 1a965d75c7fd
# docker-network-details mynetwork
docker-network-details() { docker network inspect --format '{{range $cid,$v := .Containers}}{{printf "%s: %s\n" $cid $v.Name}}{{end}}' $1; }
# shows useful commands to clean up docker cache and free some space, according to the article: https://depot.dev/blog/docker-clear-cache
docker-clear() {
echo "${GREEN}Possible actions: ${YELLOW}"
echo " docker container prune -f"
echo " docker image prune -f"
echo " docker image prune -a"
echo " docker volume prune -f"
echo " docker buildx prune -f"
echo " docker network prune -f"
echo " docker system prune -f"
echo " docker system prune --volumes -a -f"
echo "${GREEN}"
echo "Actual situation: ${YELLOW}"
echo " docker system df"
echo "${RESET}"
docker system df
}
# finds the networkID of a given container
# usage:
# $ docker-network <container-name>
docker-network() {
docker inspect -f '{{range .NetworkSettings.Networks}}{{.NetworkID}}{{end}}' ${1}
# docker run --network <network_id> <image_name>
}
# useful only for Mac OS Silicon M1,
# still working but useless for the other platforms
docker() {
if [[ `uname -m` == "arm64" ]] && [[ "$1" == "run" || "$1" == "build" || "$1" == "pull" ]]; then
/usr/local/bin/docker run --platform linux/amd64 "${@:2}"
else
/usr/local/bin/docker "$@"
fi
}
# allows using nerdctl (through colima started with --runtime containerd instead of docker)
# in a fully compatible way with docker.
# for example you can use -d and -i and -t tags like docker (with nerdctl is not possible):
# $ docker run -it -d -p 8080:8080 --name mynginx nginx:latest
# $ nerdctl run -it -d -p 8080:8080 --name mynginx nginx:latest
nerdctl() {
if [ "$1" == "run" ]; then
detached=false
tty_or_interactive=false
for arg in "${@:2}"; do
if [ "$arg" == "-d" ]; then
detached=true
elif [ "$arg" == "-i" ] || [ "$arg" == "-t" ] || [ "$arg" == "-it" ]; then
tty_or_interactive=true
fi
done
if [ "$detached" == true ] && [ "$tty_or_interactive" == true ]; then
args=()
for arg in "${@:2}"; do
if [ "$arg" != "-i" ] && [ "$arg" != "-t" ] && [ "$arg" != "-it" ]; then
args+=("$arg")
else
echo "Warning: -d flag cannot be used in conjunction with $arg. Removing flag $arg."
fi
done
colima nerdctl -- run "${args[@]}"
else
colima nerdctl -- "$@"
fi
else
colima nerdctl -- "$@"
fi
}
# keeps track of the branch from which a new branch is created.
# it updates the history of the branch in case of rebase onto
# when the branch is deleted it deletes its history also
# es. (branchA) $ git checkout -b branchB
# (branchB) $ git branch --onto branchC branchA
# (branchB) $ git branch history
# branchB created from branchA
# branchB rebased onto branchC
# What misses? the git switch command and the cancel operation for a rebase --onto
git() {
if [ "${1}" == "checkout" ] && [ "${2}" == "-b" ]; then
mkdir -p .git/branches_history
if [ "${4}" != "" ]; then
# git checkout -b <new-branch> <from-branch>
echo "${3} created from ${4}" > .git/branches_history/${3}
else
# git checkout -b <new-branch>
echo "${3} created from $(/usr/local/bin/git branch --show-current)" > .git/branches_history/${3}
fi
elif [ "${1}" == "rebase" ] && [ "${2}" == "--onto" ]; then
mkdir -p .git/branches_history
if [ "${5}" != "" ]; then
# git rebase --onto <new-base> <old-base> <branch-to-be-rebased>
echo "${5} rebased onto ${3}" >> .git/branches_history/${5}
else
# git rebase --onto <new-base> <old-base>
echo "$(/usr/local/bin/git branch --show-current) rebased onto ${3}" >> .git/branches_history/$(/usr/local/bin/git branch --show-current)
fi
elif [ "${1}" == "branch" ]; then
if [ "${2}" == "-d" ] || [ "${2}" == "-D" ] || [ "${2}" == "--delete" ] || [ "${2}" == "--force-delete" ]; then
# git branch -d <branch> | git branch -D <branch>
rm -rf .git/branches_history/${3} &> /dev/null
elif [ "${2}" == "history" ]; then
if [ "${3}" != "" ]; then
# git branch history <branch>
branchName=${3}
else
# git branch history
branchName=$(/usr/local/bin/git branch --show-current)
fi
cat .git/branches_history/${branchName}
# return because git branch history is not a real git command, so git doesn't have to do anything
return 0
fi
fi
# perform the real git command
/usr/local/bin/git "${@}"
}
# executes a containerized version of maven so you don't have to install it to your computer.
# you need to have docker installed and running.
# append this snippet to your .bashrc/.zshrc/.bash_profile/.zsh_profile files.
# you can specify 'jdk8' or 'jdk11' as first argument to switch jdk.
# if not specified, jdk11 is the default.
# usage example:
# /your/maven/project/directory - $ maven jdk8 clean install
# /your/maven/project/directory - $ maven clean package
maven() {
mkdir -p $HOME/.m2/repository
if [[ "$1" == "jdk8" ]]; then
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-adoptopenjdk-8 mvn "${@:2}"
elif [[ "$1" == "jdk11" ]]; then
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-eclipse-temurin-11 mvn "${@:2}"
elif [[ "$1" == "jdk6" ]]; then
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.2-jdk-6 mvn "${@:2}"
else
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-eclipse-temurin-11 mvn "$@"
fi
}
# shows you the tree of maven dependencies imported in your progect
mvn-tree() {
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:tree
}
alias maven-tree='mvn-tree'
alias mvntree='mvn-tree'
# allows you to use go 17 without installing on your computer
# usage example:
# /your/go/project/directory - $ golang run main.go
# /your/go/project/directory - $ golang test ./... -p 1 -count 1
# /your/go/project/directory - $ golang build .
golang() {
name=go-in-docker-$RANDOM
echo "executed in docker. Container name: $name"
docker run --rm -d --network host -v $PWD:/usr/src/myapp -w /usr/src/myapp --name ${name} golang:1.17 go "$@"
docker logs --follow ${name}
}
# SOME NOTES ON DELVE IN DOCKER
# build executable from docker
# $ docker exec -it delve go build -gcflags='-N -l'
# $ docker exec -it dlv bash -c "go mod download; GOOS=linux CGO_ENABLED=0 go install -gcflags='-N -l'; go build -gcflags='-N -l' -o delve-app"
# ... then you can do this
# $ docker exec -it delve dlv exec --listen=:2345 --api-version=2 ./delve-app
# ... or this
# $ docker exec -it delve dlv debug
# $ docker exec -it dlv bash -c "go mod download; GOOS=linux CGO_ENABLED=0 go build -gcflags='-N -l' -o delve-app"
# $ docker exec -it delve dlv exec --listen=:2345 --headless=true --api-version=2 ./my-project-executable
# API server listening at: [::]:2345
# 2022-07-13T08:09:47Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
# ... version
# $ docker exec -it dlv bash -c 'dlv version’
# $ docker exec -it dlv bash -c 'dlv debug main.go’
# start debugging your golang project using delve in docker
# go in the folder of your project, for example:
# $ cd myproject
# start delve in docker (a container named 'myproject-dlv' will be created):
# $ delve start
# the container is starting in background. Once it's ready you can:
# - debug the project with:
# $ delve debug
# - debug a test with:
# $ delve test <path/to/file.go> <path/to/file_test.go>
# When it enters in debug mode you can execute the following steps:
# - set a breakpoint
# (dlv) break handler/postalcode/get/get_postalcode_handler_test.go:183
# - continue to the next breakpoint
# (dlv) continue
# - print variables on breakpoint
# (dlv) print myVar.myAttr
# - show variable type
# (dlv) whatis myVar.myAttr
# - show all the current variables
# (dlv) vars
# - show a specific variable
# (dlv) vars myVar.myAttr
# - display variable changes at each step
# (dlv) display myVar.myAttr
# - force a value for a variable
# (dlv) call myVar.myAttr = "hello"
# - evaluate an expression
# (dlv) call myExpr(myInput)
# - continue to the next line
# (dlv) next
# - enter in the next function call
# (dlv) step
# - exit from the function call
# (dlv) stepout
# - show arguments for the current function
# (dlv) args
# - prints the current stack of calls
# (dlv) stack
# - list of the threads for the process
# (dlv) threads
# - list of the goroutines
# (dlv) goroutines
delve() {
container_name=${PWD##*/}-dlv
if [ "$1" == "init" ] || [ "$1" == "start" ]; then
if [ ! -z `docker ps | grep $container_name` ]; then
echo "Delve is already running"
elif [ ! -z `docker ps -a | grep $container_name` ]; then
echo "Delve is already initialized. Starting..."
docker start $container_name
else
echo "Preparing delve for the project..."
/usr/local/bin/docker run -it -d \
--platform linux/arm64/v8 \
-p 2345:2345 \
-v $PWD:/go/app/ \
-w /go/app \
--name $container_name \
golang \
bash -c '
export GOROOT=$(go env GOROOT);
export GOPATH=$(go env GOPATH);
export PATH=$GOPATH/bin:$PATH;
export PATH=$PATH:$GOROOT/bin;
go install github.com/go-delve/delve/cmd/dlv@latest;
tail -f /dev/null' > /dev/null
# sleep 5
while [ -z `docker exec -it $container_name bash -c "dlv version" | grep 'Delve Debugger'` ]
do
printf '.'; sleep 1; printf '.'; sleep 1; printf '.'; sleep 1
echo ""
tput cuu 1 && tput el
done
echo "Delve is started"
fi
elif [ "$1" == "debug" ]; then
docker exec -it $container_name dlv debug
elif [ "$1" == "test" ]; then
if [ "$2" != "help" ] || [ "$2" != "--help" ] || [ "$#" != 3 ]; then
echo "delve test <path/to/file.go> <path/to/file_test.go>"
else
docker exec -it $container_name dlv test $2 $3
fi
elif [ "$1" == "stop" ]; then
echo "stopping Delve..."
docker stop $container_name > /dev/null
sleep 1;
echo "Delve stopped."
elif [ "$1" == "remove" ]; then
echo "removing Delve from your project..."
docker rm -f $container_name > /dev/null
sleep 1;
echo "Delve removed."
else
docker exec -it $container_name dlv help
fi
}
# run bash5 by sharing the current directory as volume (MacOs doesn't have it!)
bash5() {
docker run --rm -d -v ~:/root -w /root --name bash5 bash:5.1-alpine3.14 tail -f /dev/null; docker exec -it bash5 bash
}
# nohup implementation using disown: just to play!
# nohupp ./ciao
# equivalent to
# nohup ./ciao &
# there is a problem! If you kill the PID it appears, the file is still filled by the background process
# so you must look for the process attached to it with: fuser -c nohup.out
nohupp() {
${@} &>nohup.out &
disown
}
# finds processes attached to a file
alias who-is-using='fuser -c' # who-is-using output.txt
# explains how to have a background process
background-process() {
echo "There are two ways:"
echo ""
echo "nohup ./ciao.sh &"
echo "tail -f nohup.out"
echo ""
echo "./ciao.sh &>disown.out &"
echo "disown"
echo "tail -f disown.out"
}
# explains how to use jq
how-to-jq() {
echo '{"Id": "abc123"}' | jq -r '.Id'
echo '[{"Id": "abc123"}, {"Id": "def456"}]' | jq -r '.[] | .Id'
}
# instructions to build scripts
alias last-param='echo ${@: -1}'
alias all-but-last-param='echo ${@:1:$#-1}'
# convers curl in wget
wget() {
if [[ "$#" -ne 3 ]]; then
# wget <URL>
curl -LO $1
elif [[ "$2" == "-O" ]]; then
# wget <URL> -O <filename>
curl -L -o $3 $1
fi
}
alias swagger='docker run -d -p 80:8080 --name swagger swaggerapi/swagger-editor'
# exposes a port of your host on the internet through a generated public url.
# example: you have a web server lostening on your host on http://localhost:8080/myservice/hello
# to expose it you just have to write: $ localtunnel 8080
# in a few seconds the public url will be shown on your terminal, let's suppose is https://dull-firefox-10.loca.lt
# then anybody on the internet can reach the API running on your computer with:
# curl -H 'Bypass-Tunnel-Reminder: true' https://dull-firefox-10.loca.lt/myservice/hello
localtunnel() {
docker rm -f localtunnel &> /dev/null
docker run -it -d --name localtunnel --network host efrecon/localtunnel --port $1 > /dev/null
echo "Creating the connection. Please wait..."
sleep 5
export LOCAL_TUNNEL_URL=$(docker logs localtunnel | cut -d ' ' -f 4)
echo "Your host port $1 is reachable to the public endpoint $LOCAL_TUNNEL_URL"
echo 'You can use exported env variable $LOCAL_TUNNEL_URL'
}
# shorten the terminal location line to just the current line
# for that specific terminal process
alias shortpath='export PS1="\[\033[32m\]\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "'
alias longpath='export PS1="\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "'
# show ram specs
alias memory='sudo lshw -short -C memory'
# show cpu specs
alias cpu='cat /proc/cpuinfo | less'
# show linux version
alias version='cat /proc/version'
# open project with intellij-idea-community installed with snap
# example: $ idea ./my-project-folder/pom.xml
alias idea='/snap/bin/intellij-idea-community'
# curl json with headers in the response
alias curl='curl -kLs' # allow insecure, follow redirects, hide progress
alias curlj='curl -s -D - -H "Content-Type: application/json;charset=utf-8"'
# curl with time calculation
alias curlt='curl -s -w "%{time_total}\n" '
# look for a process with ps command and grep
alias grep-process='ps auxww | grep -v grep | grep' # <processname> or <PID>
# get PIDs list from process name
alias pid='pgrep' #<processname>
# get threads info about a single process
alias threads='top -H -p' #<PID>
# show env vars for a process given its PID
alias processenvs='function prenvs(){ cat /proc/$1/environ | tr "\000" "\n"; };prenvs'
# show file information
alias finfo='function finfofx(){ file $1; echo "dimension: "; du -chs $1 | head -1; echo "lines: "; wc -l $1; };finfofx'
# returns local LAN IP address
alias local-ip='hostname -I | cut -d " " -f 1'
# Example:
# subnetscan 192.168.122.1/24
subnetscan() {
nmap -sn ${1} -oG - | awk '$4=="Status:" && $5=="Up" {print $2}'
}
# Scan subnet for available IPs
# Example:
# subnetfree 192.168.122.1/24
subnetfree() {
nmap -v -sn -n ${1} -oG - | awk '/Status: Down/{print $2}'
}
# Quick network port scan of an IP
# Example:
# portscan 192.168.122.37
portscan() {
nmap -oG -T4 -F ${1} | grep "\bopen\b"
}
alias ping='ping -c 4'
# grep string in entire directory (binary files excluded with the -I option)
alias grepdir='grep -nrI' #<string to be searched>
alias findfile='find . -type f -name' #<regex on name> es. '*.go'
alias finddir='find . -type d -name' #<regex on name> es. 'backup_*'
# show CPUs temperature (you must have sensors installed)
alias temp='sensors | head -20 | tail -9'
targz() {
dirname=$1
compressed="${dirname}.tar.gz"
tar -czvf $compressed $dirname
echo "${dirname}.tar.gz"
}
alias untargz='tar -xvzf'
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
alias mkdir='mkdir -p'
alias cp='cp -r'
alias rm='rm -rf'
# usage:
# $ t 1 # first level of the tree
# $ t 2 # first two level of the tree
# es.
#[149M] .
#├── [147M] api-go
#│ ├── [1.0K] Dockerfile
#│ ├── [ 11K] README.md
alias t='tree --du -h -L'
# removes a file by overwriting its content so it's not recoverable anymore
# usage:
# $ secure-rm passwords.txt
# $ cat passwords.txt | secure-rm
secure-rm() {
filename=$1
if [ "$#" == 0 ]; then
filename=$( < /dev/stdin )
fi
shred -zvu -n 5 $1
# -z add a final overwrite with zeros to hide shredding
# -v shows progress
# -u truncate and remove file after overwriting
# -n overwrite n times instead of 1
}
# echo 345 | copy | paste
alias copy='xclip'
alias paste='xclip -o'
# inline functions (put spaces close to {}, put ; at the end of each command)
now() { date +"%A, %b %d, %Y %I:%M %p"; }
# Markdown file viewer
# Usage:
# $ md filename.md
md() {
if [ -z "$1" ]; then
echo "Please provide the name of the markdown (*.md) file you want to view"
return
fi
if [ -z `which pandoc` ]; then
printf "${RED}${BOLD}pandoc${RESET}${RED} is not installed! Please install${RESET}\n\n"
return
fi
if [ -z `which lynx` ]; then
printf "${RED}${BOLD}lynx${RESET}${RED} is not installed! Please install${RESET}\n\n"
return
fi
pandoc ${1} | lynx -stdin
}
function backup-and-modify() {
echo "You are making a copy of $1 before you open it. Press enter to continue."
read nul
cp $1 $1.bak
nano $1
}
# substitute all occurrencies in a directory
# usage: escape special chars (like dot "."). For example
# $ substall 'env\.' 'updated-env\.'
substall() { grep -nrI "$1" | cut -f 1 -d ":" | sort | uniq | xargs -I {} sed -i "s/$1/$2/g" {}; }
# finds all files where a string occurres (no binary files included). Rembember to escape dots like above
findallfiles() { grep -nrI "$1" | cut -f 1 -d ":" | sort | uniq; }
# delete line from file where an occurrency is found
# usage:
# $ deleteline 'env\.Port' myfile.csv
deleteline() { sed "/$1/d" -i $2; }
repeatfn() { while read line; do $1; done < "$2"; }
# let you open a file with its default program. Ex. Open current folder: $ open .
# FOR MAC OS is not necessary
# alias open='xdg-open'
case "$OSTYPE" in
cygwin*)
alias open="cmd /c start"
;;
linux*)
alias open="xdg-open"
;;
darwin*)
# alias open="open" not necessary for mac
;;
esac
# launch a command into another terminal.
# Example:
# new-term-exec 'ls -a; echo "hello"'
# new-term-exec ./script.sh
new-term-exec() { gnome-terminal -- bash -c "$1; exec bash"; }
# inline if
# if [ $(git stash list | grep $STASH_NAME | cut -f 1 -d ":" | head -1) ]; then echo "true"; else echo "false"; fi
iff() { if [ $0 ]; then $1; else $2; fi; }
tablechars() {
echo "┘ ┐ ┌ └ ┼ ├ ┤ ┴ ┬ │ ─"
echo ""
echo "┌────────┬────────┐"
echo "├────┬───┴────┬───┤"
echo "│ │ │ │"
echo "├────┼────────┼───┤"
echo "└────┴────────┴───┘"
}
# usage: random-string 10
alias random-string='tr -dc A-Za-z0-9 </dev/urandom | head -c'
alias c='clear'
alias d='docker'
alias g='git'
alias k='kubectl'
alias gut='echo "`tput setaf 5``tput bold`You probabily meant git`tput sgr0`"; git'
alias gi='echo "`tput setaf 5``tput bold`You probabily meant git`tput sgr0`"; git'
// performs a backup of all the resource in all namespaces and saves the manifests in the specified file
k8s-backup() {
read -p "${YELLOW}insert the path of the backup file ${CYAN}[~/backup.yaml]${RESET}: " path
path=${path:-$HOME/backup.yaml}
kubectl get ns | cut -d ' ' -f 1 | xargs -I {} kubectl get all -n {} -o yaml >> ${path}
}
# let you refresh the current terminal when you update .bashrc file adding aliases or functions
alias bashrc='source ~/.bashrc'
alias zshrc='source ~/.zshrc'
# just an exercise to selectively delete files in a Java project
go-delete-tests() {
# on a custom branch X - let's save the staged and unstaged changes giving a random name
STASH_NAME=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 10)
git stash push -m $STASH_NAME -u
# let's fetch all changes from the remote branches and tags
git fetch -tp
# let's move to with-no-tests branch and reset it to origin/with-no-tests if exists; else it creates a new one
git checkout -B with-no-tests
# let's force with-no-tests to origin/master
git reset --hard origin/master
# delete any file terminating with '_test.go'
find . -name \*_test.go -type f -delete
# commit and push force on origin/with-no-tests branch
git commit -am "Removed test files"
git push -f origin with-no-tests
# checkout back on previous branch
git checkout -
# get stash id from the stash name
STASH_ID=$(git stash list | grep $STASH_NAME | cut -f 1 -d ":" | head -1)
# if stash exists, apply it
if [[ $STASH_ID ]]; then
echo "reapplying stash $STASH_ID - $STASH_NAME"
git stash apply $STASH_ID
else
echo "no stashed changes found"
fi
}
# just an exercise to selectively delete files in a Java project
delete-java-tests() {
# on a custom branch X - let's save the staged and unstaged changes giving a random name
STASH_NAME=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 10)
git stash push -m $STASH_NAME -u
# let's fetch all changes from the remote branches and tags
git fetch -tp
# let's move to with-no-tests branch and reset it to origin/with-no-tests if exists; else it creates a new one
git checkout -B with-no-tests
# let's force with-no-tests to origin/master
git reset --hard origin/master
# delete any test file and folder
find . -type d -name \*test* -prune -exec rm -rf {} \;
find . -name \*test* -type f -delete
# commit and push force on origin/with-no-tests branch
git commit -am "Removed tests"
git push -f origin with-no-tests
# checkout back on previous branch
git checkout -
# get stash id from the stash name
STASH_ID=$(git stash list | grep $STASH_NAME | cut -f 1 -d ":" | head -1)
# if stash exists, apply it
if [[ $STASH_ID ]]; then
echo "reapplying stash $STASH_ID - $STASH_NAME"
git stash apply $STASH_ID
else
echo "no stashed changes found"
fi
}
# split each line with a separator and get the specified column.
# Usage:
# $ split-get ':' 2 myfile.csv
# $ cat myfile | split-get ':' 2
split-get() {
if [[ "$#" == 3 ]]; then
cut -f $2 -d $1 $3
else
cut -f $2 -d $1 /dev/stdin
fi
}
# cat "hello how are you?" | spl -d ' ' -p 0
# > hello
# array=`spl --file myfile.txt --delimiter ':'` && echo "${array[2]}"
# > re
spl() {
index=0
dindex=-10
pindex=-10
findex=-10
for var in "$@"
do
if [[ "$index" == $(($dindex+1)) ]]; then
delimiter=$var
elif [[ "$index" == $(($pindex+1)) ]]; then
position=$var
elif [[ "$index" == $(($findex+1)) ]]; then
filename=$var
elif [[ "$var" == "-d" || "$var" == "--delimiter" ]]; then
dindex=$index
elif [[ "$var" == "-p" || "$var" == "--position" ]]; then
pindex=$index
elif [[ "$var" == "-f" || "$var" == "--file" ]]; then
findex=$index
elif [[ "$var" == "-h" || "$var" == "--help" ]]; then
echo "Usage:"
echo "spl -d <delimeter> -p <position-from-zero> -f <filename>"
echo "or"
echo "spl --delimeter <delimeter> --position <position-from-zero> --file <filename>"
echo "or"
echo "cat <filename> | spl --delimeter <delimeter> --position <position-from-zero>"
echo "or to have the entire array"
echo "spl -d <delimeter> -f <filename>"
return 0
fi
index=$(($index+1))
done
if [[ -z "$delimiter" ]]; then
echo "No delimiter specified with -d or --delimeter flag"
return 1
fi
if [[ -z "$filename" ]]; then
filecontent=$( < /dev/stdin )
else
filecontent=$(cat $filename)
fi
splitted=(${filecontent//$delimiter/ })
if [[ -z "$position" ]]; then
$splitted
return 0
fi
echo "${splitted[$position]}"
}
# prints from line x to line y of a given file.
# Usage:
# $ middle 11 15 file.txt
# $ cat file.txt | middle 11 15
middle() {
if [[ "$#" == 3 ]]; then
head -$2 $3 | tail -$(($2-$1+1))
else
head -$2 /dev/stdin | tail -$[$2 - $1 + 1]
fi
}
export -f middle
show-me() {
NAME=$1
FILE=$2
LINE=$(grep $NAME $FILE -c)
START=$(($LINE-10))
if [[ "$START" -lt 0 ]]; then
START=1
fi
echo $START
END=$(($LINE+10))
echo $END
middle 90 100 $FILE
}
export -f show-me
# executes locally a remote bash script given its URL
# es. : $ remote-bash https://github.com/myaccount/myrepo/myscript.sh
remote-bash() {
if [[ "$#" == 1 ]]; then
script_url=$1
curl -s $script_url | bash
else
echo "No valid script url provided"
return 1
fi
}
is-arm() {
dpkg --print-architecture
}
is-amd() {
dpkg --print-architecture
}
k8s-apigroups() {
kubectl api-resources -o wide
}
# finds the namespace of a resource via grep and copies into the clipboard
# usage:
# find-ns deployment nginx
find-ns(){ kubectl get $1 -A | grep $2 | cut -d ' ' -f 1 | copy; }
# moves a bash script to /usr/local/bin to let you recall directly without using ./<scriptname>.
# it allows you to specify an alias:
# example:
# $ executable myscript.sh
# $ executable myscript.sh myscript
executable() {
if [[ "$#" == 1 ]]; then
chmod +x $1
sudo mv $1 /usr/local/bin
elif [[ "$#" == 2 ]]; then
mv $1 $2
chmod +x $2
sudo mv $2 /usr/local/bin
else
echo "Invalid command format: specify file and optional alias"
exit 1
fi
}
news() {
curl https://api.nytimes.com/svc/mostpopular/v2/emailed/1.json?api-key=LGKGUSF6JjCctvSIOLq7LPCaxSoALvEY \
| jq '.results[] | "\nTITLE: " + .title + "\nABSTRACT: " + .abstract' | xargs -I {} printf "{}\n\n"
}
# show latest file
alias latest='ls -t | head -1'