CULT9

shell scripts

Started by danmaku, Apr 07, 2025, 08:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

danmaku

Here's another little fun script you can do - a magic 8-ball!

first, paste the following into a new text file, and name it something like magic8ball.sh

Code: bash
#!/usr/bin/env bash
read -p "Ask me a yes or no question: "
sort -R 8ballanswers.txt | head -n1

To break it down, it's first collecting an input from the user with the "read" command, then uses "sort" to randomly sort the answers from the file provided, and pipes that output to "head" which is set to print only the first line of the output.

Then create a separate text file called 8ballanswers.txt with an answer per line, for example:

Concentrate and ask again.
Reply hazy, try again.
Yes, definitely.
As I see it, yes.
My reply is no.
Signs point to yes.

Of course you can add as many as you like.
Make sure these two files are in the same directory.
Then you'll want to make it so you can execute the script, just open a terminal in the same directory as the script and run
Code: bash
chmod +x magic8ball.sh

Then you can actually execute the script:
Code: bash
./magic8ball.sh

This has no real-world use but it's something silly I made one day and thought I'd share it.
Software Freedom is Human Freedom!

sickseeds

 :noted:

Ooohhh, thank you! I will play around with a virtual machine then until I feel confident enough to do it on my own system!

danmaku

Thankfully messing around with bash scripts won't mess your system up at all, unless of course, if the script contains something like "sudo rm -rf /" hehe

For real though, if you're comfortable navigating the terminal with commands like cd, ls, mv, rm, cp, etc. then starting to do basic shell scripting isn't too much to grasp. All a script is in essence is a list of commands that are run line by line. At first it might seem daunting but once you understand the fundamentals there is so much you can do.

Oh, and if you're really worried about borking your system, or just wanna keep it free of clutter/test files and such, I highly recommend spinning up a virtual machine to play around in. That way if you do break anything within it, it's of no detriment to your actual OS. Since you're on Arch already, I'd recommend virt-manager.

https://www.freecodecamp.org/news/bash-scripting-tutorial-linux-shell-script-and-command-line-for-beginners/

This shell scripting guide is a bit verbose, but it's good to get your bases covered and it's all quite straightforward. If you really wanna get into scripting, I'm not an expert on it by any means but I do enjoy it lots and would be happy to help you learn.
Software Freedom is Human Freedom!

sickseeds

I have yet to try shell scripting, as part of me is worried about completely messing things up. I mean, i know if I somehow nuke my Arch i can just easily build it again. I have a bad habit of letting my anxieties stopping me from trying things like that, but I really want to I just don't know where to even start with learning bash scripts

danmaku

i don't have any experience writing powershell scripts (fuck wangblows) however, after using gnu/linux for a while i have dabbled in scripting for the bash shell. even simple one liners can be used for keyboard shortcuts, for example. menial, tedious tasks can easily be automated through the use of scripting. there's all sorts of great uses for it, the only limits are your imagination and ability to produce the necessary code.

as a simple example, here's a script i wrote to pull up twitch streams on the fly, with the only dependencies being mpv and yt-dlp.

Code: bash
#!/usr/bin/env bash
# twi-cli: Twitch command-line interface
# Prompts user to enter Twitch streamer username (must be exact)
printf "%s\n" "Enter Twitch streamer username:"
read twistr
playstream() {
# Checks for mpv and yt-dlp dependencies.
if [[ -e /usr/bin/mpv ]] && [[ -e /usr/bin/yt-dlp ]]; then
mpv --fs --force-media-title="$twistr (live) - Twitch" https://twitch.tv/$twistr
else printf "%s\n" "Could not find mpv or yt-dlp binary. Make sure you have mpv and yt-dlp installed!"
fi
}
playstream



Feel free to use/share/modify this script as you see fit. If you haven't tried shell scripting at all, you can discuss it here freely. You can also share your scripts here if you want!
Software Freedom is Human Freedom!