-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_functions
More file actions
94 lines (72 loc) · 2.21 KB
/
shell_functions
File metadata and controls
94 lines (72 loc) · 2.21 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
#!/bin/sh
rmknownhost() {
local KNOWN_HOSTS_FILE="${HOME}/.ssh/known_hosts"
local USAGE="Usage: rmknownhost [-f] line#\n\n"
OPTIND=1
while getopts f opt; do
case $opt in
f) local FORCE=1;;
?)
printf "${USAGE}"
return 2
;;
esac
done
shift $((OPTIND-1))
local LINE=$@
if [ -z "${LINE}" ]; then
printf "${USAGE}"
return 2
fi
if [ ! -r "${KNOWN_HOSTS_FILE}" ]; then
printf "Known hosts file %s is not readable!\n" "${KNOWN_HOSTS_FILE}"
return 2
fi
if [ ! -w "${KNOWN_HOSTS_FILE}" ]; then
printf "Known hosts file %s is not writeable!\n" "${KNOWN_HOSTS_FILE}"
return 2
fi
test ${LINE} -eq 1 >/dev/null 2>&1
if [ $? -gt 1 ]; then
printf "Invalid line number '%s'\n" ${LINE}
return 2
fi
local COUNT=$(sed -n $= "${KNOWN_HOSTS_FILE}")
if [ ${COUNT} -lt ${LINE} ]; then
printf "Line number %d out of range\n" ${LINE}
return 2
fi
local HOST=$(awk 'NR=='${LINE}' {print $1;exit}' "${KNOWN_HOSTS_FILE}")
if [ ${FORCE:-0} -ne 1 ]; then
local INPUT=
printf "Delete '%s' [y/N]? " "${HOST}"
read INPUT
while true; do
case "${INPUT}" in
y|Y) break;;
n|N|'') return 0;;
*) printf "Delete '%s' [y/N]? " "${HOST}"; read INPUT;;
esac
done
fi
local SCRIPT="${LINE}d"
local TEMP_FILE="${KNOWN_HOSTS_FILE}.${RANDOM}${RANDOM}"
if [ -f "${TEMP_FILE}" ]; then
printf "Temp file %s exists, aborting\n" "${TEMP_FILE}"
return 2
fi
sed "${SCRIPT}" "${KNOWN_HOSTS_FILE}" > "${TEMP_FILE}" 2>/dev/null
if [ $? -ne 0 ]; then
printf "Error creating temp file %s, aborting\n" "${TEMP_FILE}"
rm -f "${TEMP_FILE}" >/dev/null 2>&1
return 2
fi
mv -f "${TEMP_FILE}" "${KNOWN_HOSTS_FILE}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
printf "Error replacing file %s\n" "${KNOWN_HOSTS_FILE}"
fi
rm -f "${TEMP_FILE}" >/dev/null 2>&1
}
settitle() {
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
}