Skip to content

Instantly share code, notes, and snippets.

@ksverchkov
Last active November 11, 2023 14:35
Show Gist options
  • Select an option

  • Save ksverchkov/f7f565cc84c9ab64a83972005103c7ee to your computer and use it in GitHub Desktop.

Select an option

Save ksverchkov/f7f565cc84c9ab64a83972005103c7ee to your computer and use it in GitHub Desktop.
Simple bash installing addon script
#!/bin/bash
# Function to echo colored text
color_echo() {
local color=$1
local message=$2
case "$color" in
"red")
echo -e "\033[0;31m$message\033[0m"
;;
"green")
echo -e "\033[0;32m$message\033[0m"
;;
"yellow")
echo -e "\033[0;33m$message\033[0m"
;;
"blue")
echo -e "\033[0;34m$message\033[0m"
;;
"purple")
echo -e "\033[0;35m$message\033[0m"
;;
"cyan")
echo -e "\033[0;36m$message\033[0m"
;;
"white")
echo -e "\033[0;37m$message\033[0m"
;;
*)
echo "Unsupported color: $color"
;;
esac
}
# Function to download a file
download_file() {
local url=$1
local destination=$2
color_echo "cyan" "Downloading $url to $destination"
if command -v curl &>/dev/null; then
curl -s -o "$destination" "$url" && color_echo "green" "Download successful" || { color_echo "red" "Download failed"; exit 1; }
elif command -v wget &>/dev/null; then
wget -q -O "$destination" "$url" && color_echo "green" "Download successful" || { color_echo "red" "Download failed"; exit 1; }
else
color_echo "red" "Error: Neither curl nor wget found for downloading."
exit 1
fi
}
# Function to unzip a file
unzip_file() {
local file=$1
local destination=$2
color_echo "cyan" "Unzipping $file to $destination"
if command -v unzip &>/dev/null; then
unzip -q "$file" -d "$destination" && color_echo "green" "Unzip successful" || { color_echo "red" "Unzip failed"; exit 1; }
else
color_echo "red" "Error: unzip command not found."
exit 1
fi
}
# Function to untar a file
untar_file() {
local file=$1
local destination=$2
color_echo "cyan" "Untarring $file to $destination"
tar -xzf "$file" -C "$destination" && color_echo "green" "Untar successful" || { color_echo "red" "Untar failed"; exit 1; }
}
# Function to copy a file
copy_file() {
local source=$1
local destination=$2
color_echo "cyan" "Copying $source to $destination"
cp "$source" "$destination" && color_echo "green" "Copy successful" || { color_echo "red" "Copy failed"; exit 1; }
}
# Function to remove a file
remove_file() {
local file=$1
color_echo "cyan" "Removing $file"
rm "$file" && color_echo "green" "Remove successful" || { color_echo "red" "Remove failed"; exit 1; }
}
# Function to remove a directory
remove_directory() {
local directory=$1
color_echo "cyan" "Removing directory $directory"
rm -rf "$directory" && color_echo "green" "Remove directory successful" || { color_echo "red" "Remove directory failed"; exit 1; }
}
# Function for force removal (ignores non-existent files)
force_remove() {
local target=$1
color_echo "cyan" "Force removing $target"
rm -rf "$target" && color_echo "green" "Force remove successful" || { color_echo "red" "Force remove failed"; exit 1; }
}
# Function to move/rename a file or directory
move() {
local source=$1
local destination=$2
color_echo "cyan" "Moving $source to $destination"
mv "$source" "$destination" && color_echo "green" "Move successful" || { color_echo "red" "Move failed"; exit 1; }
}
# Function to create a directory
make_directory() {
local directory=$1
color_echo "cyan" "Creating directory $directory"
mkdir -p "$directory" && color_echo "green" "Create directory successful" || { color_echo "red" "Create directory failed"; exit 1; }
}
# Function to check if a file exists
file_exists() {
local file=$1
color_echo "cyan" "Checking if $file exists"
[ -e "$file" ]
}
# Function to check if a directory exists
directory_exists() {
local directory=$1
color_echo "cyan" "Checking if directory $directory exists"
[ -d "$directory" ]
}
function ask() {
local message=$1
local default=${FALSE}
read -p "${message} [Yes/no]: " yn
result=''
if [ "${yn}" == "" ]; then
result="true"
fi
if [ "${yn}" == "Yes" ]; then
result="true"
fi
if [ "${yn}" == "No" ]; then
result="false"
fi;
[ "${result}" == "true" ]
# Ctrl-D pressed.
return ${default}
}
function read_variable() {
local message=$1
local default=${FALSE}
read -p "${message}: " yn
echo "${yn}"
}
function read_secure_variable() {
local message=$1
local default=${FALSE}
read -p "${message}: " -s yn
echo "${yn}"
}
# Example usage:
if ask "Are you sure?"; then
password=$(read_variable "Enter variable")
echo "$password"
download_file "https://src.fedoraproject.org/lookaside/extras/nss_db/db-4.7.25.NC.tar.gz/073ab7f20d24b3872a51ca762f5090e7/db-4.7.25.NC.tar.gz" "./somefile.tar.gz"
#unzip_file "./somefile.zip" "."
untar_file "./somefile.tar.gz" "."
remove_file "./somefile.tar.gz"
remove_directory db-4.7.25.N*
copy_file "./source.txt" "./destination.txt"
remove_file "./unwanted_file.txt"
remove_directory "./unwanted_directory"
force_remove "./nonexistent_file_or_directory"
move "./old_name.txt" "./new_name.txt"
make_directory "./new_directory"
if file_exists "./existing_file.txt"; then
color_echo "green" "File exists!"
else
color_echo "red" "File does not exist."
fi
if directory_exists "./existing_directory"; then
color_echo "green" "Directory exists!"
else
color_echo "red" "Directory does not exist."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment