-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.sh
90 lines (83 loc) · 2.76 KB
/
template.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
#!/bin/bash
# IMPORTANT!
# `apt` does not have a stable CLI interface. Use with caution in scripts.
## Refer: https://askubuntu.com/a/990838
export DEBIAN_FRONTEND=noninteractive
## Refer: https://unix.stackexchange.com/a/356759
export PAGER='cat'
export SYSTEMD_PAGER=cat
# Consider adding the following aliases to `~/.bash_aliases` for daily use.
: << 'ALIASES'
```shell
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mkdir='mkdir -p'
alias crontab='crontab -i'
```
ALIASES
# Shell functions are only known to the shell. External commands like `find`, `xargs`, `su` and `sudo` do not recognize shell functions.
# Instead, the function contents can be executed in a shell, either through sh -c or by creating a separate shell script as an executable file.
## Refer: https://github.com/koalaman/shellcheck/wiki/SC2033
cd() {
cd "$@" || exit 1
}
rm() {
$(type -P rm) "$@"
}
cp() {
$(type -P cp) "$@"
}
mv() {
$(type -P mv) "$@"
}
mkdir() {
$(type -P mkdir) -p "$@"
}
# Personally I would recommend using `install` rather than `cp`
## Refer: https://unix.stackexchange.com/a/441251 , https://superuser.com/a/229980
mkdir_and_install_644() {
# $(type -P install) --preserve-context -pvDm 644 "$@"
$(type -P install) -pvDm 644 "$@"
}
curl() {
$(type -P curl) -LRq --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
# The following version will not work on non-interactive environment
: << 'CURL_2_DEST'
```shell
curl_to_dest() {
tmp_dir=$(mktemp -d)
pushd "$tmp_dir" || exit 1
read -rp "(Download URL is): " -a download_url
if $(type -P curl) -LROJq --retry 5 --retry-delay 10 --retry-max-time 60 "${download_url[@]}"; then
read -rp "(The dest for file copying): " dest_path
# find . -maxdepth 1 -type f -print0 | xargs -0 -i -r -s 2000 sudo "$(type -P install)" -pvDm 644 "{}" "$dest_path"
find . -maxdepth 1 -type f -print0 | xargs -0 -i -r -s 2000 "$(type -P install)" -pvDm 644 "{}" "$dest_path"
fi
popd || exit 1
/bin/rm -rf "$tmp_dir"
dirs -c
}
```
CURL_2_DEST
curl_to_dest() {
if [[ $# -eq 2 ]]; then
tmp_dir=$(mktemp -d)
pushd "$tmp_dir" || exit 1
if $(type -P curl) -LROJq --retry 5 --retry-delay 10 --retry-max-time 60 "$1"; then
# find . -maxdepth 1 -type f -print0 | xargs -0 -i -r -s 2000 sudo "$(type -P install)" -pvDm 644 "{}" "$2"
find . -maxdepth 1 -type f -print0 | xargs -0 -i -r -s 2000 "$(type -P install)" -pvDm 644 "{}" "$2"
fi
popd || exit 1
/bin/rm -rf "$tmp_dir"
dirs -c
fi
}
git_clone() {
if [[ -z "$GIT_PROXY" ]]; then
$(type -P git) clone -j "$(nproc)" --no-tags --shallow-submodules --recurse-submodules --depth 1 --single-branch "$@"
else
$(type -P git) -c "$GIT_PROXY" clone -j "$(nproc)" --no-tags --shallow-submodules --recurse-submodules --depth 1 --single-branch "$@"
fi
}