From 35d802c753097b72ec3ebb313bcdc6451f74a970 Mon Sep 17 00:00:00 2001 From: Subarctic2796 <132659316+Subarctic2796@users.noreply.github.com> Date: Sun, 7 Jul 2024 13:05:57 +0200 Subject: [PATCH 1/4] Update setup.sh update setup.sh to be more bash like. --- setup.sh | 78 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/setup.sh b/setup.sh index 0b93242..41b221c 100755 --- a/setup.sh +++ b/setup.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash +# use bin/env bash as cannot be sure that bash actually exists at bin/bash RC='\e[0m' RED='\e[31m' @@ -25,6 +26,11 @@ if [[ ! -d "$LINUXTOOLBOXDIR/mybash" ]]; then fi fi +# add variables to top level so can easily be accessed by all functions +PACKAGER="" +SUDO_CMD="" +SUGROUP="" +GITPATH="" cd "$LINUXTOOLBOXDIR/mybash" || exit @@ -34,8 +40,10 @@ command_exists() { checkEnv() { ## Check for requirements. - REQUIREMENTS='curl groups sudo' - for req in ${REQUIREMENTS}; do + # use local as it is more correct bash + # use arrays instead of splitting + local REQUIREMENTS=(curl groups sudo) + for req in "${REQUIREMENTS[@]}"; do if ! command_exists ${req}; then echo -e "${RED}To run me, you need: ${REQUIREMENTS}${RC}" exit 1 @@ -43,9 +51,9 @@ checkEnv() { done ## Check Package Handeler - PACKAGEMANAGER='nala apt yum dnf pacman zypper emerge xbps-install nix-env' + local PACKAGEMANAGER=(nala apt dnf yum pacman zypper emerge xbps-install nix-env) - for pgm in ${PACKAGEMANAGER}; do + for pgm in "${PACKAGEMANAGER[@]}"; do if command_exists ${pgm}; then PACKAGER=${pgm} echo -e "Using ${pgm}" @@ -66,21 +74,22 @@ checkEnv() { SUDO_CMD="su -c" fi - echo "Using ${SUDO_CMD} as privilege escalation software" - + echo "Using $SUDO_CMD as privilege escalation software" + ## Check if the current directory is writable. - GITPATH="$(dirname "$(realpath "$0")")" - if [[ ! -w ${GITPATH} ]]; then - echo -e "${RED}Can't write to ${GITPATH}${RC}" + # we cd'd into this directory before this function so we can use the PWD var + if [[ ! -w "$PWD" ]]; then + echo -e "${RED}Can't write to ${PWD}${RC}" exit 1 fi ## Check SuperUser Group - SUPERUSERGROUP='wheel sudo root' - for sug in ${SUPERUSERGROUP}; do + local SUPERUSERGROUP=(wheel sudo root) + for sug in "${SUPERUSERGROUP[@]}"; do if groups | (command_exists rg && rg -q ${sug} || grep -q ${sug}); then SUGROUP=${sug} - echo -e "Super user group ${SUGROUP}" + # don't need -e as there are no escape codes to escape + echo "Super user group ${SUGROUP}" break fi done @@ -100,7 +109,7 @@ installDepend() { fi echo -e "${YELLOW}Installing dependencies...${RC}" - if [[ $PACKAGER == "pacman" ]]; then + if [[ "$PACKAGER" == "pacman" ]]; then if ! command_exists yay && ! command_exists paru; then echo "Installing yay as AUR helper..." ${SUDO_CMD} ${PACKAGER} --noconfirm -S base-devel @@ -118,20 +127,22 @@ installDepend() { exit 1 fi ${AUR_HELPER} --noconfirm -S ${DEPENDENCIES} - elif [[ $PACKAGER == "nala" ]]; then + elif [[ "$PACKAGER" == "nala" ]]; then ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} - elif [[ $PACKAGER == "emerge" ]]; then + elif [[ "$PACKAGER" == "emerge" ]]; then ${SUDO_CMD} ${PACKAGER} -v app-shells/bash app-shells/bash-completion app-arch/tar app-editors/neovim sys-apps/bat app-text/tree app-text/multitail app-misc/fastfetch - elif [[ $PACKAGER == "xbps-install" ]]; then + elif [[ "$PACKAGER" == "xbps-install" ]]; then ${SUDO_CMD} ${PACKAGER} -v ${DEPENDENCIES} - elif [[ $PACKAGER == "nix-env" ]]; then + elif [[ "$PACKAGER" == "nix-env" ]]; then ${SUDO_CMD} ${PACKAGER} -iA nixos.bash nixos.bash-completion nixos.gnutar nixos.neovim nixos.bat nixos.tree nixos.multitail nixos.fastfetch + elif [[ "$PACKAGER" == "dnf" ]]; then + ${SUDO_CMD} ${PACKAGER} install -y ${DEPENDENCIES} else ${SUDO_CMD} ${PACKAGER} install -yq ${DEPENDENCIES} fi } -installStarship() { +installStarshipAndFzf() { if command_exists starship; then echo "Starship already installed" return @@ -162,25 +173,33 @@ installZoxide() { } install_additional_dependencies() { - case $(command -v apt || command -v zypper || command -v dnf || command -v pacman) in + # we have PACKAGER so just use it + # for now just going to return early as we have already installed neovim in `installDepend` + # so I am not sure why we are trying to install it again + return + case "$PACKAGER" in *apt) curl -LO https://github.com/neovim/neovim/releases/latest/download/nvim.appimage chmod u+x nvim.appimage ./nvim.appimage --appimage-extract ${SUDO_CMD} mv squashfs-root /opt/neovim ${SUDO_CMD} ln -s /opt/neovim/AppRun /usr/bin/nvim + echo "apt" ;; *zypper) ${SUDO_CMD} zypper refresh - ${SUDO_CMD} zypper install -y neovim + ${SUDO_CMD} zypper install -y neovim + echo "zypper" ;; *dnf) ${SUDO_CMD} dnf check-update - ${SUDO_CMD} dnf install -y neovim + ${SUDO_CMD} dnf install -y neovim + echo "dnf" ;; *pacman) ${SUDO_CMD} pacman -Syu - ${SUDO_CMD} pacman -S --noconfirm neovim + ${SUDO_CMD} pacman -S --noconfirm neovim + echo "pacman" ;; *) echo "No supported package manager found. Please install neovim manually." @@ -198,9 +217,9 @@ linkConfig() { USER_HOME=$(getent passwd ${SUDO_USER:-$USER} | cut -d: -f6) ## Check if a bashrc file is already there. OLD_BASHRC="${USER_HOME}/.bashrc" - if [[ -e ${OLD_BASHRC} ]]; then + if [[ -e "${OLD_BASHRC}" ]]; then echo -e "${YELLOW}Moving old bash config file to ${USER_HOME}/.bashrc.bak${RC}" - if ! mv ${OLD_BASHRC} ${USER_HOME}/.bashrc.bak; then + if ! mv "${OLD_BASHRC}" "${USER_HOME}/.bashrc.bak"; then echo -e "${RED}Can't move the old bash config file!${RC}" exit 1 fi @@ -208,15 +227,15 @@ linkConfig() { echo -e "${YELLOW}Linking new bash config file...${RC}" ## Make symbolic link. - ln -svf ${GITPATH}/.bashrc ${USER_HOME}/.bashrc - ln -svf ${GITPATH}/starship.toml ${USER_HOME}/.config/starship.toml + ln -svf "${GITPATH}/.bashrc" "${USER_HOME}/.bashrc" + ln -svf "${GITPATH}/starship.toml" "${USER_HOME}/.config/starship.toml" echo -e "${YELLOW}Linking custom fastfetch config file...${RC}" - ln -svf ${GITPATH}/config.jsonc ${USER_HOME}/.config/fastfetch/config.jsonc + ln -svf "${GITPATH}/config.jsonc" "${USER_HOME}/.config/fastfetch/config.jsonc" } checkEnv installDepend -installStarship +installStarshipAndFzf installZoxide install_additional_dependencies create_fastfetch_config @@ -226,4 +245,3 @@ if linkConfig; then else echo -e "${RED}Something went wrong!${RC}" fi - From e67d57018cb4be979ee6b780775f66a3d67e358a Mon Sep 17 00:00:00 2001 From: Subarctic2796 <132659316+Subarctic2796@users.noreply.github.com> Date: Sun, 7 Jul 2024 13:08:12 +0200 Subject: [PATCH 2/4] Update .bashrc add xdg folders export linuxtoolboxdir as other scripts will use it add caching for starship and zoxide so that the shell starts faster --- .bashrc | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/.bashrc b/.bashrc index d09f5c9..692a5fb 100644 --- a/.bashrc +++ b/.bashrc @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash iatest=$(expr index "$-" i) ####################################################### @@ -42,6 +42,15 @@ shopt -s checkwinsize shopt -s histappend PROMPT_COMMAND='history -a' +# set up XDG folders +export XDG_DATA_HOME="$HOME/.local/share" +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_STATE_HOME="$HOME/.local/state" +export XDG_CACHE_HOME="$HOME/.cache" + +# Seeing as other scripts will use it might as well export it +export LINUXTOOLBOXDIR="$HOME/linuxtoolbox" + # Allow ctrl-S for history navigation (with ctrl-R) [[ $- == *i* ]] && stty -ixon @@ -475,10 +484,10 @@ install_bashrc_support() { sudo apt-get install multitail tree zoxide trash-cli fzf bash-completion # Fetch the latest fastfetch release URL for linux-amd64 deb file FASTFETCH_URL=$(curl -s https://api.github.com/repos/fastfetch-cli/fastfetch/releases/latest | grep "browser_download_url.*linux-amd64.deb" | cut -d '"' -f 4) - + # Download the latest fastfetch deb file curl -sL $FASTFETCH_URL -o /tmp/fastfetch_latest_amd64.deb - + # Install the downloaded deb file using apt-get sudo apt-get install /tmp/fastfetch_latest_amd64.deb ;; @@ -628,5 +637,19 @@ fi export PATH=$PATH:"$HOME/.local/bin:$HOME/.cargo/bin:/var/lib/flatpak/exports/bin:/.local/share/flatpak/exports/bin" # Install Starship - curl -sS https://starship.rs/install.sh | sh -eval "$(starship init bash)" -eval "$(zoxide init bash)" +# cache starship and zoxide for faster startup also creates the cache the first time +# refreshes the cache every 14 days so if you update during that time and the script +# for starship or zoxide initialization changes you will get the new script +local STARSHIP_CACHE="$LINUXTOOLBOXDIR/mybash/_starship.sh" +if [[ ! $(find "$STARSHIP_CACHE" -newermt "14 days ago" -print) ]]; then + starship init bash --print-full-init > "$STARSHIP_CACHE" +fi +source "$STARSHIP_CACHE" + +local ZOXIDE_CACHE="$LINUXTOOLBOXDIR/mybash/_zoxide.sh" +if [[ ! $(find "$ZOXIDE_CACHE" -newermt "14 days ago" -print) ]]; then + zoxide init bash > "$ZOXIDE_CACHE" +fi +source "$ZOXIDE_CACHE" + +unset STARSHIP_CACHE ZOXIDE_CACHE From 54f1c9144ed0d8d3c71d16a367b6b24746417228 Mon Sep 17 00:00:00 2001 From: Subarctic2796 <132659316+Subarctic2796@users.noreply.github.com> Date: Sun, 7 Jul 2024 13:14:44 +0200 Subject: [PATCH 3/4] Update README.md Add installation guide for people who don't know how to install it --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 58e04f9..f4da045 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ The `.bashrc` file is a script that runs every time a new terminal session is started in Unix-like operating systems. It is used to configure the shell session, set up aliases, define functions, and more, making the terminal easier to use and more powerful. Below is a summary of the key sections and functionalities defined in the provided `.bashrc` file. +## How to install +``` +git clone --depth=1 https://github.com/ChrisTitusTech/mybash.git +cd mybash +chmod +x setup.sh +./setup.sh +``` + ### Initial Setup and System Checks - **Environment Checks**: The script checks if it is running in an interactive mode and sets up the environment accordingly. From 098202ef28163f88614da16a7ed92d03d9de65ba Mon Sep 17 00:00:00 2001 From: Subarctic2796 <132659316+Subarctic2796@users.noreply.github.com> Date: Sun, 7 Jul 2024 14:50:59 +0200 Subject: [PATCH 4/4] remove printf debugging I by mistake left in some of the echo debugging while I was working on the script. So i removed it --- setup.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/setup.sh b/setup.sh index 41b221c..cec5608 100755 --- a/setup.sh +++ b/setup.sh @@ -184,22 +184,18 @@ install_additional_dependencies() { ./nvim.appimage --appimage-extract ${SUDO_CMD} mv squashfs-root /opt/neovim ${SUDO_CMD} ln -s /opt/neovim/AppRun /usr/bin/nvim - echo "apt" ;; *zypper) ${SUDO_CMD} zypper refresh ${SUDO_CMD} zypper install -y neovim - echo "zypper" ;; *dnf) ${SUDO_CMD} dnf check-update ${SUDO_CMD} dnf install -y neovim - echo "dnf" ;; *pacman) ${SUDO_CMD} pacman -Syu ${SUDO_CMD} pacman -S --noconfirm neovim - echo "pacman" ;; *) echo "No supported package manager found. Please install neovim manually."