drpanwe

Download Golang

Jan 21st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.38 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Current version
  4. currentGoVersion=$(go version | cut -f3 -d ' ')
  5. if [ -z "$currentGoVersion" ]; then # Check if it's empty
  6.     echo "Find current Go version: [FAIL]"
  7.     exit 1
  8. else
  9.     echo "Find current Go version: [OK]"
  10. fi
  11.  
  12. # Find latest version
  13. URL=$(curl -ks https://golang.org/dl/ | grep downloadBox | grep linux-amd64.tar.gz | awk -F 'href="' '{ print $2 }' | cut -f1 -d '"')
  14. if [ -z "$URL" ]; then  # Check if the variable $URL is empty
  15.     echo "Parse latest Go tarball link: [FAIL]"
  16.     exit 1
  17. else
  18.     echo "Parse latest Go tarball link: [OK]"
  19. fi
  20.  
  21. # Check if the local version is the same as the latest one
  22. if echo "$URL" | grep "$currentGoVersion" &> /dev/null; then
  23.     echo "You have the latest version already installed"
  24.     exit 0
  25. fi
  26.  
  27. if wget --output-document=latest-go.tar.gz "$URL" &> /dev/null; then
  28.     echo "Download latest Go: [OK]"
  29. else
  30.     echo "Download latest Go: [FAIL]"
  31.     exit 1
  32. fi
  33.  
  34. # Uninstall the current Go
  35. if sudo rm -rf /usr/local/go &> /dev/null; then
  36.     echo "Uninstall current Go: [OK]"
  37. else
  38.     echo "Uninstall current Go: [FAIL]"
  39.     exit 1
  40. fi
  41.  
  42. # Install
  43. if sudo tar -C /usr/local -xzf latest-go.tar.gz &> /dev/null; then
  44.     echo "Install latest Go: [OK]"
  45. else
  46.     echo "Install latest Go: [FAIL]"
  47.     exit 1
  48. fi
  49.  
  50. # Cleanup
  51. if rm latest-go.tar.gz &> /dev/null; then
  52.     echo "Clean temporary files: [OK]"
  53. else
  54.     echo "Clean temporary files: [FAIL]"
  55.     exit 1
  56. fi
Add Comment
Please, Sign In to add comment