My scripts and/or cases

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
1,083
Reaction score
921
Credits
8,303
I had almost given up posting such things on forums and/or other platforms bc many linux users are ungrateful and instead of a simple "thanks", I often received hate and curses for trying to help someone who might have come across the same problems I have or to simply make their linux life easier.
But when you read something like "I changed my "Like" reaction to "Love"", that changes everything and tells you there is a point in keep doing it. If it helps even one person, there IS a point to it and I'll keep doing it. I don't ask for money or donations, nor will I ever ask for such a thing. Everything I do or share is FOSS - forever. The only thing I do ask is this: if you find what I've posted useless FOR YOU, just skip it and move on. It may not be useful for you, but it may be useful for someone else.

You can have a look at my repositories on github - everything is FOSS with MIT License which means you're free to change things in any way you want, even without asking me. https://github.com/rado84-github?tab=repositories

So, after reading that line about changing the reaction to "love", I decided to open this topic where to share how to use case scripts (altough I already did that on my github) and share some useful cases and scripts meant mostly for lazy people like me in order to make the linux life easier. I know what you're thinking: how can a person who writes scripts be lazy but that's the thing - scripts are for automation of processes. You write it once, then activate it by alias and it does the rest with a minimal effort from you.

I went a long way since 2023 when I asked for a script to remove things from SRT subtitles. Back then I had a few hundred bash scripts that until a few months ago had reached the number of 280 .sh files and the aliases themselves used to be over 350. But then I found out about cases and learned how to use them. It turned out they're a lot easier than regular scripts, altough they do have some... peculiarities about them.

I'm well aware that everything you'll read in this post looks likе... an elite-level skill, but it's not. It just looks that way. All you need is just a little practice. Once you get used to it, you'll start coming up with your scripts and/or cases and you'll even be able to write them blindfolded. If I could learn all this in less than a month, so can you.

And nowadays the aliases and scripts I have were severely shrunk to these modest numbers, thanks to the cases:

Code:
[rado@arch]: ~>$ count cli
Total number of aliases is: 82.
Total number of bash scripts in '/B/CLI/SCRIPTS' is: 71.

From 350+ aliases and 280+ bash scripts to just 82/71 in less than a month. And yet my system has never felt so... powerful bc anything a regular person would spend 2-5 min clicking here and there, I do it in under 60 seconds.

I won't share ALL of the cases/scripts, only those that I think might be useful for someone. Once you know how to use cases, you're free to come up with your own cases.

Here's a detailed explanation about case scripts: https://github.com/rado84-github/case-scripts. I did my best to write it as clear as possible, along with some examples, but still, if you have any questions, feel free to ask - I'll do my best to answer as clearly as possible.

I can generate my own scripts and case scripts, so I think I should start with that. Basically, with a few letters in the terminal, you can generate a script or a case with predefined content (template) which then you can edit manually with your favorite text editor.

Bash script generator. It works when you open it in any specific directory. If you open the terminal without cd to a specific dir, it will create the script in your /home/$USER.

Code:
#!/usr/bin/env bash

DEST="$PWD"

cat <<EOF > "$DEST/$1.sh"
#!/usr/bin/env bash

options0="-mx0 -mmt20"
options9="-mx9 -mmt20 -md=460m"
rsf="rsync --info=progress2"
rsd="rsync -r --info=progress2"


EOF

chmod +x "$DEST/$1.sh"
gedit "$DEST/$1.sh" &
exit

These 4 lines with options0/options9, rsf - they're not mandatory. They're just part of MY template and you're free to remove or modify them, depending on your workflow. If you do decide to use them, especially options0 and options9, keep in mind they're part of the syntax of the 7zip CLI tool for linux. AND if you decide to try options9, you better remove the part with "-md=460m" bc that's a dictionary size which is tailored for my hardware. Unless you have 62.62 GiB RAM and AT LEAST a 20-thread CPU, I don't recommend you to try this option, if you don't wanna crash your system when archiving things. Dictionary size of 460 MiB wil lead to your system use exactly 50.0 GiB RAM...
This script generator will create a bash file ('.sh') in the directory you've opened with a name you type before you hit Enter. I have an alias for this generator and I generate scripts like this:

Code:
makesh meab

In this case "meab" stands for "Mass Effect Andromeda backup". So, if I've opened the terminal in "/B/CLI/SCRIPTS", it will create meab.sh there and it will contain the above template. Then I can change it to create a script that would automatically backup the entire directory of "Mass Effect Andromeda".
-------------------------------
I also have a case generator which makes things a lot easier. Again, the generator is activated by an alias but the syntax is a little different. The destination is predefined in the header of the script (the variable named DEST) but you can change it to suit your purposes. Again, this is a template tailored for my system and workflow, but you can change it. The syntax to activate the case generator, including my alias, is like this:

Code:
makecase 12 SYSTEM

which will create a script file with 12 empty cases in it which then you have to manually change. The number of cases depends on you. Just remember that the case generator accepts only positive integers and if you don't enter a desired number of cases, by default it will create a file with 5 cases. The above code creates "SYSTEM-CASE.sh" with 12 cases in it.
Here's the code for the generator itself:

Code:
#!/usr/bin/env bash

DEST="/B/CLI/SCRIPTS/CASES"
COUNT="${1:-5}"   # Without an argument creates 5 cases by default. Accepts only positive integers.
OUTFILE="$DEST/$2-CASE.sh"

if [ -z "${2:-}" ]; then
    echo "You have to enter file name. Better use dashes instead of spaces.\n"
    exit 1
fi

if ! [[ "$COUNT" =~ ^[0-9]+$ ]]; then
    printf "Use integers only. Task terminated.\nUsage: './bash-case-generator.sh 18' will create example-18-case.sh in the destination.\n"
    exit 1
fi

{
cat <<HEADER
#!/usr/bin/env bash
set -euo pipefail

options0="-mx0 -mmt20"
options9="-mx9 -mmt20 -md=460m"
rsf="rsync --info=progress2"
rsd="rsync -r --info=progress2"

case "\$1" in
HEADER

for i in $(seq 1 "$COUNT"); do
    cat <<EOF
    option$i)
        echo "Put here the code for option$i"
        ;;
EOF
done

cat <<FOOTER
    *)
        echo "Cases: ."
        ;;
esac
FOOTER
} > "$OUTFILE"

chmod +x "$OUTFILE"
echo "$OUTFILE with $COUNT cases was created."

This is it for now. When I find a script/case that might be useful for someone, I'll write it in a new post.
 
Last edited:


Here are 2 both lazy and useful cases for you. One is for automated quick search on YouTube (it could be adapted for other sites that include the search terms in the URL) and one of YouTube URL shortened without visiting any suspicious sites or installing extensions that may or may not work.

1. Search. In order to find something on YT, you gotta open your browser, then YouTube, then type what you need. If you do it once or twice a day, it's fine, but if you have to do it a doze times an hour, it becomes annoying. So why not automate it? Here's how:

Code:
browser="/home/rado/Waterfox-Browser/waterfox-bin"
yturl="https://www.youtube.com/results?search_query"

    s)
        shift
        terms="$*"
        $browser $yturl="$terms official video"
        ;;

And don't forget the "shift" word! Without it the case won't work. In short, after entering the terms, that word "removes" the case alias and name and executes the script itself. You can change the browser variable to match the path of your default browser.

Also, the search terms are entered as "$*" on purpose, instead of "$1", "$2" and so on. When the terms are entered like this, you don't need to add quotes around the terms. Plus, you're searching for something, you don't know how exactly it was written on YouTube, so whatever you enter, it has to be passed to the browser without quotes. That way YouTube will show you all results containing your search terms.

Here's an example how to use this case script:

(yt is the alias I use for the activation of the case script and "s" is the name of the case itself; "s" - short for "search")
Code:
yt s lil john three-6-mafia throw it up

This opens this search results page in the browser (new tab or new window - that depends on your browser settings):

Code:
https://www.youtube.com/results?search_query=lil+john+throw+it+up+official+video

2. YouTube URL shortener.

Code:
    sh)
        read -p "Въведете YT URL: " URL
        echo "$URL" | cut -c1-43 > /B/CLI/SCRIPTS/CASES/supplements/yt-url.txt
        echo ""
        cat /B/CLI/SCRIPTS/CASES/supplements/yt-url.txt
        gedit /B/CLI/SCRIPTS/CASES/supplements/yt-url.txt &
        exit
        ;;

The activation is the same the other case above, only this time the name is "sh" - short for... "shorten".

Code:
yt sh [key Enter]

After you press "Enter", it will ask you to enter the URL. Then it creates a text file as shown above and returns the first 43 characters of the whole URL. At least for the moment, the first 43 characters of the URL are the the YT address + the video ID (those random numbers and characters). After that opens the text file in your preferred text editor (in my case that's gedit), so that you can copy the shortened URL and paste it wherever you want. It even works for mobile apps bc they'll automatically convert this URL to something that starts with https://m.whatever.....
Once the text file is open, the script exits and your terminal is awaiting for your next command.

As always, you can change the destination of the text file and you don't need to delete it. Every time you execute the shortening case, the content of the text file is being rewritten, which means you won't encounter a bunch of shortened URLs, you'll find only one.

Note: some of you will probably try to remove the "read -p" function and pass the URL directly but that won't work. Even if you surround the full URL with quotes, the script would still think it's part of a function bc there are TWO ampersands (&) in the URL address and thus the script will fail.
Whereas "read -p" treats everything you pass to it as pure text, thus this is the only way for the case script to work.

Some of you will probably wonder why I've added an empty line (echo ""). That's just in case that opening the text file fails and you end up with just the shortened address in the terminal. First, it looks better with an empty line. Secondly, add clarifications which is which and thirdly, reduces the clutter, thus reducing mistakes when copying the shortened address. Ofc, you can remove the empty line but in time you'll see it's better with it.


V1UdMny.png
 
Fast search for files, a lot faster than any GUI tool by Gnome or MATE.

Case for searching in root (/usr, /etc and /opt):

Code:
    root)
        SEARCH_DIRS=("/usr" "/etc" "/opt")
        PATTERN="$2"

        echo "Searching for files containing \"$PATTERN\" in \"/usr, /etc и /opt\"."

        find "${SEARCH_DIRS[@]}" \
        -path "*/.Trash-0" -prune -o \
        -path "*/.Trash-1000" -prune -o \
        -path "*/share/locale" -prune -o \
        -iname "*$PATTERN*" -print 2>/dev/null
        ;;

The "2>/dev/null" tells find not to show the results that return "access denied".
With the prune option you can add any paths you find to skip when searching for files.

Case for searching in the current dir. Which means you'll have to open the terminal there or cd to it.

Code:
    here)
        SEARCH_DIR="."
        PATTERN="$2"

        echo "Търся файлове, съдържащи \"$PATTERN\" в \"./$(basename "$PWD")\"."

        found=0

        while IFS= read -r path; do
            echo "$path"
            found=1
        done < <(find "$SEARCH_DIR" -iname "*$PATTERN*" -print)

        if [ "$found" -eq 0 ]; then
            echo "Не намерих нищо."
        fi
        ;;

For instance:
Code:
[rado@arch]: /B/MUSIC>$ ff here "rock\'n\'roll train"
Търся файлове, съдържащи "rock\'n\'roll train" в "./MUSIC".
./ROCK/AC-DC/01. Rock'n'Roll Train.wav

Using a \ before symbols like ' ensures that the ' symbol will be treated as text and not as a quotation mark, which would confuse bash and might return errors.

Ofcourse you can change the names of the cases, if you wish.
 
These are my case scripts. If you're interested in any of them, I'll gladly share them with you along with an explanation what they do.

Code:
ARCH-SEARCH-CASE.sh
AUDIO-CASE.sh
AVN-CASE.sh
BACKUP-CASE.sh
BD-R_COPY-CASE.sh
CONVERT-CASE.sh
COUNT-CASE.sh
FILES-CASE.sh
FIX-CASE.sh
GAMES-CASE.sh
IMAGES-CASE.sh
NETWORK-CASE.sh
NVIDIA-CASE.sh
PACK-UNPACK-CASE.sh
SYSTEM-CASE.sh
TEXT-CASE.sh
WINE-CASE.sh
YOUTUBE-CASE.sh
 


Follow Linux.org


Top