-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_bash_alias.sh
111 lines (85 loc) · 2.83 KB
/
set_bash_alias.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
#!/bin/bash
## Decorative tty colors
function Tput_Colors {
## Foreground Color using ANSI escape
FG_BLACK=$(tput setaf 0)
FG_RED=$(tput setaf 1)
FG_GREEN=$(tput setaf 2)
FG_YELLOW=$(tput setaf 3)
FG_BLUE=$(tput setaf 4)
FG_MAGENTA=$(tput setaf 5)
FG_CYAN=$(tput setaf 6)
FG_WHITE=$(tput setaf 7)
FG_NoColor=$(tput sgr0)
## Background Color using ANSI escape
BG_BLACK=$(tput setab 0)
BG_RED=$(tput setab 1)
BG_GREEN=$(tput setab 2)
BG_YELLOW=$(tput setab 3)
BG_BLUE=$(tput setab 4)
BG_MAGENTA=$(tput setab 5)
BG_CYAN=$(tput setab 6)
BG_WHITE=$(tput setab 7)
BG_NoColor=$(tput sgr0)
## set mode using ANSI escape
MODE_BOLD=$(tput bold)
MODE_DIM=$(tput dim)
MODE_BEGIN_UNDERLINE=$(tput smul)
MODE_EXIT_UNDERLINE=$(tput rmul)
MODE_REVERSE=$(tput rev)
MODE_ENTER_STANDOUT=$(tput smso)
MODE_EXIT_STANDOUT=$(tput rmso)
# clear styles using ANSI escape
STYLES_OFF=$(tput sgr0)
FGBG_NoColor=$(tput sgr0)
}
## make an alias variable in ~/.bashrc to launch keyboard-layout.sh
function make_bashrc_alias {
aliasVar=$1
bashFile=$2
bashFilePath=$(pwd)/$bashFile
echo -e "$FG_CYAN \
\n## ################################################################## \
\n## Making an alias variable in ~/.bashrc to launch keyboard-layout.sh \
\n## the alias is \"kbd\" \
\n## ################################################################## \
\n $STYLES_OFF"
## make bash executable
chmod +x $bashFilePath
origianlBashrc=~/.bashrc
backupBashrc=$origianlBashrc.backup_$(date +%d%b%Y%H%S)
tempBashrc=$origianlBashrc.temp
## make a safety backup of ~/.bashrc
cp $origianlBashrc $backupBashrc
cp $origianlBashrc $tempBashrc
## Make a temporary .bashrc file to edit
## Delete previous line reference to bash file
sed -i "/$bashFile/d" $tempBashrc
sleep 1s
aliasautoString="\
\n## Alias for $bashFilePath \
\nalias $aliasVar=\"bash $bashFilePath\" \
\n"
## Append ~/.bashrc with alias and it's auto complete argv
echo -e "$aliasautoString" >> $tempBashrc
## Make the temp file the ~/.bashrc file
mv $tempBashrc $origianlBashrc
sleep 2s
## apply the new ~/.bashrc
source $origianlBashrc
echo -e "${FG_YELLOW}Done.\n ${STYLES_OFF} Restrting Bash in 3 seconds ..."
sleep 3s
reset
echo -e "${FG_GREEN}\nThe bash alias \"kbd\" was added to ~/.bashrc\n\tType \"kbd\" to launch the keyboard-layout.sh script.\n${STYLES_OFF}\n"
if [ ${BASH_VERSION::1} -lt 4 ]; then
echo -e "This script application is optimized for Bash versions 4 or greater.\n\tCurrent Bash Version: ${BASH_VERSION}"
fi
}
function main {
Tput_Colors
aliasVar=kbd
bashFile=keyboard-layout.sh
make_bashrc_alias $aliasVar $bashFile
}
## RUN
main