-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliasmng.sh
82 lines (57 loc) · 1.71 KB
/
aliasmng.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
#!/bin/bash
read -p "You want to delete or add an alias? (add/delete) " RESP
if [ "$RESP" = "add" ]; then
echo echo 'aliasname for bashrc: '
read aliasname
echo 'command for bashrc: '
read commandname
if [ -z "$aliasname" ] || [ -z "$commandname" ]; then
echo "aliasname or commandname is empty"
exit 1
fi
# check if aliasname and commandname exists in bash_aliases
if grep -q "$aliasname" ~/.bash_aliases; then
echo "aliasname exists"
exit 1
fi
if grep -q "$commandname" ~/.bash_aliases; then
echo "commandname exists"
exit 1
fi
## if check to see if the file bash_aliases exists
if [ -f ~/.bash_aliases ]; then
echo "creating alias in bash_aliases"
echo "alias $aliasname='$commandname'" >> ~/.bash_aliases
else
echo "file does not exist, creating it, then creating alias in bash_aliases"
touch ~/.bash_aliases
echo "alias $aliasname='$commandname'" >> ~/.bash_aliases
fi
# # Append the alias to the bashrc file
# echo "alias $aliasname='$commandname'" >> ~/.bash_aliases
# Reload the bashrc
source ~/.bashrc
source ~/.bash_aliases
echo 'bashrc updated'
echo 'list of aliases: '
cat ~/.bash_aliases
else
echo 'aliasname to delete: '
read aliasname
# check if aliasname exists in bash_aliases
if grep -q "$aliasname" ~/.bash_aliases; then
echo "aliasname exists"
else
echo "aliasname does not exist"
exit 1
fi
# delete the alias from bash_aliases
sed -i "/$aliasname/d" ~/.bash_aliases
# Reload the bashrc
source ~/.bashrc
source ~/.bash_aliases
echo 'bashrc updated'
#list aliases
echo 'list of aliases: '
cat ~/.bash_aliases
fi