Last active
October 17, 2021 21:11
-
-
Save seakintruth/95d49b6ea316c2b8a6e283e1ee1a3f3a to your computer and use it in GitHub Desktop.
Rstudio Version Launcher (And R Version installer) for Ubuntu / Debian.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# failed to perform conditional arithmatic when using #!/bin/sh | |
# Made available under (CC BY-SA 3.0) https://creativecommons.org/licenses/by-sa/3.0/ | |
# The menu system as modified from https://askubuntu.com/a/257766/957393 | |
# The option_picked function is by Jeremy D. Gerdes seakintruth@gmail.com April 2019 | |
# The install_if_missing is modified from https://stackoverflow.com/a/22592801/1146659 | |
# ------------------------------------------------------------------------------------- | |
# Before running this script Install R and Rstudio with/from: | |
# sudo apt install r-base | |
# https://rstudio.com/products/rstudio/download/#download | |
# ------------------------------------------------------------------------------------- | |
# The most recent version of this script can be called by either one of: | |
# bash <(curl -s https://gist.githubusercontent.com/seakintruth/95d49b6ea316c2b8a6e283e1ee1a3f3a/raw) | |
# bash <(wget -q -O - https://gist.githubusercontent.com/seakintruth/95d49b6ea316c2b8a6e283e1ee1a3f3a/raw) | |
normal=`echo "\033[00;00m"`; # normal white | |
msgcolor=`echo "\033[01;31m"`; # bold red | |
fgred=`echo "\033[31m"`; # red | |
prmtmsg=`echo "Perform one of the following:\n - enter a R Vesion Number (i.e. 3.6.1)\n - pick an option from the menu\n - or ${fgred}x ${normal}to exit.\n:"` ; | |
menu=`echo "\033[36m"`; #Blue | |
number=`echo "\033[33m"`; #yellow | |
bgred=`echo "\033[41m"`; | |
install_if_missing() { | |
# https://stackoverflow.com/a/22592801/1146659 | |
local package_name=$1 | |
if [ $(dpkg-query -W -f='${Status}' $package_name 2>/dev/null | grep -c "ok installed") -eq 0 ]; then | |
echo -n "Attempt to install package $package_name? [yes or no]: " | |
read yno | |
case $yno in | |
[yY] | [yY][Ee][Ss] ) | |
printf "Missing $package_name. Setting up $package_name. \n" | |
sudo apt-get --yes install $package_name | |
;; | |
[nN] | [nN][oO] ) | |
printf "Package $package_name is required \n" | |
exit 1; | |
;; | |
*) | |
printf "Invalid Input \n" | |
exit 1; | |
;; | |
esac | |
fi | |
} | |
option_picked(){ | |
local r_ver=$1 | |
local set_rstudio_version="/opt/R/$r_ver/bin/R"; | |
# if the expected file exist then we just launch for this version | |
if test -f "$set_rstudio_version"; then | |
echo "$set_rstudio_version exist" | |
else | |
r_major=`expr substr ${r_ver} 1 1` | |
if [ "$r_major" = "1" ] || [ "$r_major" = "0" ]; then | |
extention="tgz" | |
else | |
extention="tar.gz" | |
fi | |
R_Opt="/opt/R" | |
if [ ! -d "$R_Opt" ]; then | |
sudo mkdir $R_Opt | |
fi | |
local r_url=`echo "https://cran.r-project.org/src/base/R-$r_major/R-$r_ver.$extention"` | |
# test if the desired web page exists | |
# https://stackoverflow.com/a/2924444/1146659 | |
install_if_missing curl; | |
curl -s --head $r_url | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null | |
# $? will be 0 if curl url resturns 200 or 3* status | |
if [ $? -eq 0 ];then | |
printf "Getting file for installation:\n\t" | |
echo -n "Attempt to install R version $r_ver? [yes or no]: " | |
read yno | |
case $yno in | |
[yY] | [yY][Ee][Ss] ) | |
echo "Agreed, installing $r_ver" | |
# install Dependancies | |
install_if_missing libx11-dev; | |
install_if_missing xorg-dev; | |
install_if_missing libcurl4-openssl-dev; | |
curl $r_url \ | |
--output ~/Downloads/R-$r_ver.$extention | |
printf "~/Downloads/R-$r_ver.$extention ~/Downloads/" | |
cd ~/Downloads && tar -xzvf ~/Downloads/R-$r_ver.$extention | |
tar -xzvf ~/Downloads/R-$r_ver.$extention ~/Downloads/; | |
rm ~/Downloads/R-$r_ver.$extention; | |
cd ~/Downloads/R-$r_ver && \ | |
sudo ./configure --prefix=/opt/R/$r_ver --enable-R-shlib && \ | |
sudo make && \ | |
sudo make install | |
if test -f "$set_rstudio_version"; then | |
printf "Installation complete for $set_rstudio_version" | |
else | |
printf "Failed to install $set_rstudio_version " | |
exit 1; | |
fi | |
# Cleanup install folder in ~/Downloads folder | |
sudo rm -r -f ~/Downloads/R-$r_ver/ | |
local local_desktop=~/.local/share/applications | |
if [ ! -d "$local_desktop" ]; then | |
sudo mkdir $local_desktop | |
fi | |
if test -f "$local_desktop/name.desktop"; then | |
# create a shortcut to this file if desktop file doesn't exist | |
printf "You may need to edit the file $local_desktop/name.desktop to add this launcher to your applications" | |
else | |
local launcher_path=~/.local/share/RSTUDIO_Version_Launcher.sh | |
curl https://gist.githubusercontent.com/seakintruth/95d49b6ea316c2b8a6e283e1ee1a3f3a/raw \ | |
--output $launcher_path | |
sudo chmod +x $launcher_path | |
touch $local_desktop/r.desktop | |
echo "[Desktop Entry]" >> "$local_desktop/r.desktop" | |
echo "Type=Application" >> "$local_desktop/r.desktop" | |
echo "Terminal=true" >> "$local_desktop/r.desktop" | |
echo "Name=RSTUDIO_Version_Launcher" >> "$local_desktop/r.desktop" | |
echo "Icon=/usr/lib/rstudio/rstudio.png" >> "$local_desktop/r.desktop" | |
echo "Exec=$launcher_path" >> "$local_desktop/r.desktop" | |
printf "/n Created launcher application shortcut $local_desktop/r.desktop /n" | |
cat "$local_desktop/r.desktop" | |
fi | |
;; | |
[nN] | [n|N][O|o] ) | |
printf "Not agreed, you can't launch untill you agree to attempt an install of $r_ver \n"; | |
exit 1; | |
;; | |
*) | |
printf "Invalid input \n" | |
exit 1; | |
;; | |
esac | |
fi | |
fi | |
local message="Attempting to Start RStudio $r_ver from path: \n\t ${normal}$set_rstudio_version"; | |
printf "${msgcolor}${message}${normal}\n"; | |
if [ -f "$set_rstudio_version" ]; then | |
printf "$set_rstudio_version \n\t"; | |
export RSTUDIO_WHICH_R="$set_rstudio_version"; | |
install_if_missing screen; | |
[[ ! -d /run/screen ]] && sudo /etc/init.d/screen-cleanup start; | |
screen -d -m rstudio; | |
else | |
printf "Unable to find $set_rstudio_version \n" | |
fi | |
show_menu; | |
} | |
repeat() { | |
local ts=$(printf "%${2}s"); | |
printf %s "${ts// /$1}"; | |
} | |
display_menu_item(){ | |
local item_number=$1 | |
local version_number=$2 | |
# get character count: https://stackoverflow.com/a/31009961/1146659 | |
local ver_char_len=${#version_number} | |
# repeat a space n number of times: https://stackoverflow.com/a/23426605/1146659 | |
let white_char_len=" 16 - $ver_char_len " | |
local whitespace=`repeat " " $white_char_len` | |
local end_first_column=29 | |
local end_second_column=59 | |
if (( $item_number > $end_second_column )); then | |
printf "${menu}** ${number}$item_number) ${menu}$version_number${normal}\n"; | |
elif (( $item_number > $end_first_column )); then | |
printf "${menu}** ${number}$item_number) ${menu}$version_number${normal}$whitespace"; | |
elif (( $item_number > 9 )); then | |
printf "${menu}** ${number} $item_number) ${menu}$version_number${normal}$whitespace"; | |
else | |
printf "${menu}** ${number} $item_number) ${menu}$version_number${normal}$whitespace"; | |
fi | |
} | |
show_menu(){ | |
printf "\n${menu}*******************************************************${normal}\n"; | |
printf "Built for Ubuntu, Dependancy prior to running this script is 'RStudio'. \n"; | |
printf "If installation is required will prompt for sudo access to build from source."; | |
printf "\n${menu}*******************************************************${normal}\n"; | |
printf "\n${menu}****** Select the Version of R to Launch RSTUDIO ******${normal}\n"; | |
printf "\n${menu}*******************************************************${normal}\n"; | |
display_menu_item 1 3.0.0; display_menu_item 30 2.0.0; display_menu_item 60 2.13.0 | |
display_menu_item 2 3.0.1; display_menu_item 31 2.0.1; display_menu_item 61 2.13.1 | |
display_menu_item 3 3.0.2; display_menu_item 32 2.1.0; display_menu_item 62 2.13.2 | |
display_menu_item 4 3.0.3; display_menu_item 33 2.1.1; display_menu_item 63 2.14.0 | |
display_menu_item 5 3.1.0; display_menu_item 34 2.2.0; display_menu_item 64 2.14.1 | |
display_menu_item 6 3.1.1; display_menu_item 35 2.2.1; display_menu_item 65 2.14.2 | |
display_menu_item 7 3.1.2; display_menu_item 36 2.3.0; display_menu_item 66 2.15.0 | |
display_menu_item 8 3.1.3; display_menu_item 37 2.3.1; display_menu_item 67 2.15.1-w | |
display_menu_item 9 3.2.0; display_menu_item 38 2.4.0; display_menu_item 68 2.15.1 | |
display_menu_item 10 3.2.1; display_menu_item 39 2.4.1; display_menu_item 69 2.15.2 | |
display_menu_item 11 3.2.2; display_menu_item 40 2.5.0; display_menu_item 70 2.15.3 | |
display_menu_item 12 3.2.3; display_menu_item 41 2.5.1; display_menu_item 71 1.0.0 | |
display_menu_item 13 3.2.4; display_menu_item 42 2.6.0; display_menu_item 72 1.0.1 | |
display_menu_item 14 3.2.4-revised; display_menu_item 43 2.6.1; display_menu_item 73 1.1.0 | |
display_menu_item 15 3.2.5; display_menu_item 44 2.6.2; display_menu_item 74 1.2.0 | |
display_menu_item 16 3.3.0; display_menu_item 45 2.7.0; display_menu_item 75 1.3.0 | |
display_menu_item 17 3.3.1; display_menu_item 46 2.7.1; display_menu_item 76 1.3.1 | |
display_menu_item 18 3.3.2; display_menu_item 47 2.7.2; display_menu_item 77 1.4.0 | |
display_menu_item 19 3.3.3; display_menu_item 48 2.8.0; display_menu_item 78 1.4.1 | |
display_menu_item 20 3.4.0; display_menu_item 49 2.8.1; display_menu_item 79 1.5.0 | |
display_menu_item 21 3.4.1; display_menu_item 50 2.9.0; display_menu_item 80 1.5.1 | |
display_menu_item 22 3.4.2; display_menu_item 51 2.9.1; display_menu_item 81 1.6.0 | |
display_menu_item 23 3.4.3; display_menu_item 52 2.9.2; display_menu_item 82 1.6.1 | |
display_menu_item 24 3.4.4; display_menu_item 53 2.10.0; display_menu_item 83 1.6.2 | |
display_menu_item 25 3.5.0; display_menu_item 54 2.10.1; display_menu_item 84 1.7.0 | |
display_menu_item 26 3.5.1; display_menu_item 55 2.11.0; display_menu_item 85 1.7.1 | |
display_menu_item 27 3.5.2; display_menu_item 56 2.11.1; display_menu_item 86 1.8.0 | |
display_menu_item 28 3.5.3; display_menu_item 57 2.12.0; display_menu_item 87 1.8.1 | |
display_menu_item 29 3.6.0; display_menu_item 58 2.12.1; display_menu_item 88 1.9.0 | |
display_menu_item 90 3.6.1; display_menu_item 59 2.12.2; display_menu_item 89 1.9.1 | |
display_menu_item 91 3.6.2; display_menu_item 92 3.6.3; display_menu_item 93 4.0.0 | |
display_menu_item 94 4.0.1; display_menu_item 95 4.0.2; display_menu_item 96 4.0.3 | |
display_menu_item 97 4.0.4; display_menu_item 98 4.0.5; display_menu_item 99 4.1.0 | |
display_menu_item 100 4.1.1; display_menu_item x "exit launcher" | |
printf "${menu}*********************************************${normal}\n"; | |
printf "$prmtmsg"; | |
read opt; | |
while [ "$opt" != "" ]; do | |
if [ "$opt" = "x" ]; then | |
exit; | |
else | |
case $opt in | |
1) option_picked "3.0.0";; | |
2) option_picked "3.0.1";; | |
3) option_picked "3.0.2";; | |
4) option_picked "3.0.3";; | |
5) option_picked "3.1.0";; | |
6) option_picked "3.1.1";; | |
7) option_picked "3.1.2";; | |
8) option_picked "3.1.3";; | |
9) option_picked "3.2.0";; | |
10) option_picked "3.2.1";; | |
11) option_picked "3.2.2";; | |
12) option_picked "3.2.3";; | |
13) option_picked "3.2.4";; | |
14) option_picked "3.2.4-revised";; | |
15) option_picked "3.2.5";; | |
16) option_picked "3.3.0";; | |
17) option_picked "3.3.1";; | |
18) option_picked "3.3.2";; | |
19) option_picked "3.3.3";; | |
20) option_picked "3.4.0";; | |
21) option_picked "3.4.1";; | |
22) option_picked "3.4.2";; | |
23) option_picked "3.4.3";; | |
24) option_picked "3.4.4";; | |
25) option_picked "3.5.0";; | |
26) option_picked "3.5.1";; | |
27) option_picked "3.5.2";; | |
28) option_picked "3.5.3";; | |
29) option_picked "3.6.0";; | |
30) option_picked "2.0.0";; | |
31) option_picked "2.0.0";; | |
32) option_picked "2.0.1";; | |
33) option_picked "2.1.0";; | |
34) option_picked "2.1.1";; | |
35) option_picked "2.2.1";; | |
36) option_picked "2.3.0";; | |
37) option_picked "2.3.1";; | |
38) option_picked "2.4.0";; | |
39) option_picked "2.4.1";; | |
40) option_picked "2.5.0";; | |
41) option_picked "2.5.1";; | |
42) option_picked "2.6.0";; | |
43) option_picked "2.6.1";; | |
44) option_picked "2.6.2";; | |
45) option_picked "2.7.0";; | |
46) option_picked "2.7.1";; | |
47) option_picked "2.7.2";; | |
48) option_picked "2.8.0";; | |
49) option_picked "2.8.1";; | |
50) option_picked "2.9.0";; | |
51) option_picked "2.9.1";; | |
52) option_picked "2.9.2";; | |
53) option_picked "2.10.0";; | |
54) option_picked "2.10.1";; | |
55) option_picked "2.11.0";; | |
56) option_picked "2.11.1";; | |
57) option_picked "2.12.0";; | |
58) option_picked "2.12.1";; | |
59) option_picked "2.12.2";; | |
60) option_picked "2.13.0";; | |
61) option_picked "2.13.1";; | |
62) option_picked "2.13.2";; | |
63) option_picked "2.14.0";; | |
64) option_picked "2.14.1";; | |
65) option_picked "2.14.2";; | |
66) option_picked "2.15.0";; | |
67) option_picked "2.15.1-w";; | |
68) option_picked "2.15.1";; | |
69) option_picked "2.15.2";; | |
70) option_picked "2.15.3";; | |
71) option_picked "1.0.0";; | |
72) option_picked "1.0.1";; | |
73) option_picked "1.1.0";; | |
74) option_picked "1.2.0";; | |
75) option_picked "1.3.0";; | |
76) option_picked "1.3.1";; | |
77) option_picked "1.4.0";; | |
78) option_picked "1.4.1";; | |
79) option_picked "1.5.0";; | |
80) option_picked "1.5.1";; | |
81) option_picked "1.6.0";; | |
82) option_picked "1.6.1";; | |
83) option_picked "1.6.2";; | |
84) option_picked "1.7.0";; | |
85) option_picked "1.7.1";; | |
86) option_picked "1.8.0";; | |
87) option_picked "1.8.1";; | |
88) option_picked "1.9.0";; | |
89) option_picked "1.9.1";; | |
90) option_picked "3.6.1";; | |
91) option_picked "3.6.2";; | |
92) option_picked "3.6.3";; | |
93) option_picked "4.0.0";; | |
94) option_picked "4.0.1";; | |
95) option_picked "4.0.2";; | |
96) option_picked "4.0.3";; | |
97) option_picked "4.0.4";; | |
98) option_picked "4.0.5";; | |
99) option_picked "4.1.0";; | |
100) option_picked "4.1.1";; | |
[xXqQ][eE] | [eE][xX][iI][tT] | [qQ][uU][iI][tT] ) exit;; | |
\n)exit;; | |
*) | |
# if the length of the option picked is >=3 assume a version # is passed | |
if [ ${#opt} -ge 3 ]; then | |
option_picked $opt; | |
else | |
echo "done"; | |
fi | |
show_menu; | |
;; | |
esac | |
fi | |
done | |
} | |
show_menu; |
Create a custom launcher pointing to this script with https://askubuntu.com/a/141247/957393
Some feedback is that the script should also accept a version number as input, and provide a general failure on error.
Last update adds several functions to clean up the code for the menu system.
#! /usr/bin/env bash in the first line, /bin/bash is not always the location of bash
#! /usr/bin/env bash in the first line, /bin/bash is not always the location of bash
Thanks for the input, after reviewing https://unix.stackexchange.com/a/29620 I've incorporated this recommendation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To execute this script from the terminal use this line:
bash <(curl -s https://gist.githubusercontent.com/seakintruth/95d49b6ea316c2b8a6e283e1ee1a3f3a/raw)