-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.InstallGo.sh
executable file
·98 lines (86 loc) · 2.85 KB
/
1.InstallGo.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
#!/bin/sh
echo "======================================"
echo " Step 1: Installing Go 1.21.7 "
echo "======================================"
# Step 1: Download Go 1.21.7
echo "1. Downloading Go 1.21.7..."
wget https://dl.google.com/go/go1.21.7.linux-amd64.tar.gz
if [ $? -ne 0 ]; then
echo "❌ Failed to download Go. Exiting."
exit 1
else
echo "✅ Go 1.21.7 downloaded successfully."
fi
# Step 2: Extract the Go tarball
echo "2. Extracting Go..."
sudo tar -xvf go1.21.7.linux-amd64.tar.gz
if [ $? -ne 0 ]; then
echo "❌ Failed to extract Go. Exiting."
exit 1
else
echo "✅ Go extracted successfully."
fi
# Step 3: Move Go to /usr/local
echo "3. Copying Go to /usr/local..."
sudo cp -R go /usr/local
if [ $? -ne 0 ]; then
echo "❌ Failed to copy Go to /usr/local. Exiting."
exit 1
else
echo "✅ Go copied to /usr/local successfully."
fi
# Step 4: Change ownership of Go directory
echo "4. Changing ownership of Go directory..."
sudo chown $USER:$USER -R /usr/local/go
if [ $? -ne 0 ]; then
echo "❌ Failed to change ownership. Exiting."
exit 1
else
echo "✅ Ownership changed successfully."
fi
# Step 5: Add or verify environment variables in ~/.profile
echo "5. Verifying and updating environment variables in ~/.profile..."
update_profile=false
check_and_add_env_var() {
VAR_NAME=$1
VAR_VALUE=$2
if grep -q "^export $VAR_NAME=" ~/.profile; then
CURRENT_VALUE=$(grep "^export $VAR_NAME=" ~/.profile | cut -d'=' -f2- | tr -d '"')
if [ "$CURRENT_VALUE" != "$VAR_VALUE" ]; then
sed -i "s|^export $VAR_NAME=.*|export $VAR_NAME=$VAR_VALUE|" ~/.profile
echo "Updated $VAR_NAME to $VAR_VALUE in ~/.profile"
update_profile=true
else
echo "$VAR_NAME is already set to $VAR_VALUE in ~/.profile. Skipping."
fi
else
echo "export $VAR_NAME=$VAR_VALUE" >> ~/.profile
echo "Added $VAR_NAME=$VAR_VALUE to ~/.profile"
update_profile=true
fi
}
check_and_add_env_var "GOROOT" "/usr/local/go"
check_and_add_env_var "GOPATH" "$HOME/go"
check_and_add_env_var "PATH" "\$GOPATH/bin:\$GOROOT/bin:\$PATH"
if $update_profile; then
echo "✅ Environment variables updated successfully in ~/.profile."
else
echo "✅ Environment variables were already correctly set in ~/.profile."
fi
# Step 6: Apply the changes to the current script environment
echo "6. Applying profile changes..."
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
# Step 7: Verify Go version
echo "7. Verifying Go installation..."
go version
if [ $? -ne 0 ]; then
echo "❌ Go version check failed. Exiting."
exit 1
else
echo "✅ Go installed and verified successfully."
fi
echo "======================================"
echo "✅ Go 1.21.7 installed and configured!"
echo "======================================"