\\\ This site is under construction \\\

../

- Bash Scripts -

A list of useful bash scripts I have created

- randomstring.sh

#!/bin/bash

symbols='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~#$&_+-=/\'
count_symbols=${#symbols}
((length = $1))
for i in $(seq 1 $length) ; do
    string+=${symbols:RANDOM % count_symbols:1}
done
echo "$string" | xsel -i -b # xsel is a dependency, or not if you don't need it
echo $length "RND Character String Was placed in Clipboard, have fun :)"
            

Usage: ./randomstring.sh *NumberOfCharacters*

What it does: outputs a random string of characters defined by a positional argument to your clipboard.

Note: the xsel portion can be removed if you want the string to be dumped in the terminal.

- directorydumper_v2.sh

#!/bin/bash

filenum="$(find -maxdepth 2 -type f | wc -l)";
finalfilenum="$((filenum-1))";
printf "%s" "$finalfilenum" " Files found.";
printf "\nPlease use the Shift+Tab keys to select all files.";
sleep 4; clear;
fzf="$(fzf -m -q / | tr '\n' ' ')" && mv $fzf "$(pwd)";
cd "$(pwd)";
            

Usage: Put the script in the directory containing the sub-directories you want to extract the files from,

then run ./directorydumper_v2.sh

What it does: for every folder, it goes 1 folder depth in to fetch files & dump them into the script directory.

Note: fuzzy finder (fzf) is a dependency. This script will not work if filenames have spaces. (I might fix it)

- worldbackup_v4.sh

#!/bin/bash

i="0"; o="0"; p="0";
                    
# The reasoning behind the sleep commands is to not give the CPU heart attacks by letting the script run loose
# Remember, a path starts with a / and typing a folder name like home/user/Documents/world will NOT work.
                    
echo "- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -"
echo "| Note: You can type full paths or a folder, but the folder needs   |"
echo "| to be in the current/working directory                            |"
echo "- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -"
                    
while [ "$p" -eq 0 ]
do
    while [ "$i" -eq 0 ]
    do
        echo "World Not Defined."
        sleep 0.5;
        while [ "$o" -eq 0 ]
        do
            printf "%s" "World Folder/Directory: ";
            read WorldDirectory
                    
            if [ "${WorldDirectory#*"/"}" != "$WorldDirectory" ]; then # POSIX substring parameter expansion
                IsWorldDirPath=1; # User entered a path
                if test -d "$WorldDirectory"; then
                    printf "Valid Path :D ";
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Path.. Trying again"
                   sleep 0.5;
                fi
            else
                IsWorldDirPath=0; # User did not enter a path
                if test -d "$WorldDirectory"; then
                    printf "Valid Folder :D "
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Folder.. Trying again"
                    sleep 0.5;
                fi
            fi
        done
        echo "Using" $WorldDirectory;
        sleep 0.5;
        ((i++)); # Ignore loop
    done
                    
    ((i--)) # Reset state to 0
    ((o--)) # Reset state to 0
                    
    while [ "$i" -eq 0 ]
    do
        echo "Backup Not Defined."
        sleep 0.5;
        while [ "$o" -eq 0 ]
        do
            printf "%s" "Backup Folder/Directory: ";
            read BackupDirectory
                    
            if [ "${BackupDirectory#*"/"}" != "$BackupDirectory" ]; then
                IsBackupDirPath=1; # User entered a path
                if test -d "$BackupDirectory"; then
                    printf "Valid Path :D ";
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Path.. Trying again"
                    sleep 0.5;
                fi
            else
                IsBackupDirPath=0; # User did not enter a path
                if test -d "$BackupDirectory"; then
                    printf "Valid Folder :D "
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Folder.. Trying again"
                    sleep 0.5;
                fi
            fi
        done
        echo "Using" "$BackupDirectory";
        sleep 0.5;
        ((i++)); # Ignore loop
        echo "----------------------------------"
    done
    echo "$WorldDirectory" "will be copied over to" "$BackupDirectory" "every day with the yyyy-mm-dd format."
    echo "Time is according to your locale, using the 12-hour clock with AM PM indicators."
    sleep 0.5;
    printf "%s" "Sounds Good? : "; read Answer
                    
    if [ "$Answer" == "yes" ] || [ "$Answer" == "Yes" ] || [ "$Answer" == "YES" ] || [ "$Answer" == "yeah" ]; then
        if [ "$IsWorldDirPath" == 0 ]; then # If not a path
            printf "Concatenating World Folder To Form a Path. . . "
            FullWorldPath="$(echo "$(pwd)""/"$WorldDirectory)"
            sleep 0.5;
            if test -d "$FullWorldPath"; then
                echo "Success !";
                ((o++)) # Ignore loop
            fi
        fi
                    
        if [ "$IsBackupDirPath" == 0 ]; then # If not a path
            printf "Concatenating Backup Folder To Form a Path. . . "
            FullBackupPath="$(echo "$(pwd)""/"$BackupDirectory)"
            sleep 0.5;
            if test -d "$FullBackupPath"; then
                echo "Success !";
                ((o++)) # Ignore loop
            fi
        fi
                    
        if [ "$IsWorldDirPath" == 1 ]; then # If a path
            echo "Valid World Path :D";
            FullWorldPath="$WorldDirectory"
            ((o++)) # Ignore loop
        fi
                    
        if [ "$IsBackupDirPath" == 1 ]; then # If a path
            echo "Valid Backup Path :D";
            FullBackupPath="$BackupDirectory"
            ((o++)) # Ignore loop
        fi
                    
        ((p++))  # Ignore loop
    else
        echo "Process Interrupted, Trying Again.";
        ((i--)) # Reset state to 0, Loop continues
        ((o--)) # Reset state to 0, Loop continues
        sleep 0.5;
    fi
done
cd "$FullBackupPath"
while :
do
    CurrentDate="$(date +"%F_%r" | tr -d " ")";
    mkdir "$CurrentDate"
    cp -r "$FullWorldPath" "$CurrentDate"
    echo "Backup done at: "$CurrentDate"";
    sleep 1d
done                    
            

Usage: ./worldbackup_v4.sh

What it does: it backups a folder every day, that's fundamentally it.

Note: This is the script that I was talking about in the Minecraft Server page.

I tested the logic & everything is fine.

DO NOT run my scripts with elevated privileges, I will beat the shit out of you am not a security expert.