diff options
Diffstat (limited to 'assets/md/bash-scripting-101.md')
-rw-r--r-- | assets/md/bash-scripting-101.md | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/assets/md/bash-scripting-101.md b/assets/md/bash-scripting-101.md new file mode 100644 index 0000000..20828f6 --- /dev/null +++ b/assets/md/bash-scripting-101.md @@ -0,0 +1,215 @@ +# Bash Scripting 101: Your Shitty Guide to Command Line Magic + +Hey there, welcome to the wild world of bash scripting! If you're itching to automate your shitty tasks or just want to impress your friends with your shitty command line skills, you're in the right place. In this shitty guide, we're gonna dive into bash scripting from the ground up, no fancy jargon, just good ol' shitty scripting fun. + +## Let's Get Started: No Rocket Science Here + +Got you! It's all rocket science here. Grab your favorite text editor and open up your terminal. Whether you're into Vim, Emacs, or even Notepad, it's all good (God knows Vim is the best but even god can't help idiots right?). Create a new file and give it a `.sh` extension. This tells your system it's a bash script. + +```bash +touch hello.sh +chmod +x hello.sh +``` + +Full disclosure: You can save yourself a shit ton of time by not giving extensions to files but then again when were you ever able to remember even the name of the file you created let alone it's type? + +Now, let's write a script that says "Hello, World!" because, well, tradition. + +```bash +#!/bin/bash +echo "Hello, World!" +``` + +You could write "Hello \<insert name of partner here\>" but let's be honest, if you're here, reading this, even god knows you're dying alone. + +Save it, make it executable with that `chmod +x` command, and give it a spin: + +```bash +./hello.sh +``` + +Ta-da! You just ran your first bash script. Boring stuff, right? It gets better. It gets way better. (It doesn't) + +## Now, Let's Break It Down: The Basics + +Bash scripts are just a series of commands written in, you guessed it, bash. Let's go over the basics without all the technical mumbo jumbo. I know all of it went over your head but I assure you, it's totally not worth it. Seriously, if you don't already know this stuff, you probably don't need it. But anyways, let's get on with it. + +### Shebang: The Starting Line + +And so the race begins. You aren't gonna win this one but you already knew that right? That `#!/bin/bash` at the top of your shitty script tells your system what the fuck to use in order to understand your shitty code. It's like saying, "Hey, this is a bash script!". Now, you could skip shebangs and just use `bash` to run your shitty script, but come morning, you won't even remember what your shitty code does let alone what it runs with. + +### Comments: Because Talking to Yourself is Cool + +No, talking to yourself is retarded (s/retarded/what\ every\ developer\ does\ at\ 1\ a\\.m\\.\ at\ night/g). Whoever says otherwise doesn't need to be reading this and can leave. Use `#` to add your shitty comments to your shitty script. + +```bash +#!/bin/bash +# This is a comment +echo "Hello, World!" # This is also a comment +``` + +It's like leaving sticky notes for yourself so you don't forget what your script does. How many times do I have to say it? You won't remember any of it. + +### Variables: Your Script's Memory + +Variables are like little memory boxes. You give 'em a name and stuff 'em with data. Unlike your brain. + +```bash +#!/bin/bash +name="John" +echo "Hello, $name" +``` + +Easy, right? Now `$name` thinks it's John. Don't worry, it'll get over it. Unlike you with your non-existing ex. + +### Control Structures: Keeping It in Check + +Control structures like `if` statements and loops help your shitty script make decisions and do stuff repeatedly. God knows you can't do stuff properly once. How the fuck will you do it repeatedly? Better to let the pros handle it. + +#### Conditional Statements: Making Choices + +```bash +#!/bin/bash +age=18 +if [ "$age" -ge 18 ]; then + echo "You are an adult." +else + echo "You are a minor." +fi +``` + +See? Your shitty script can now tell if you're an adult or a kid. Neat, huh? Doesn't matter because if you're reading this, you'll always be a kid and please for the sake of all that is holy, never grow up. It isn't worth it. + +#### Loops: Doing Things Over and Over + +```bash +#!/bin/bash +for i in {1..5}; do + echo "Count: $i" +done +``` + +This loop counts from 1 to 5 and says "Count: x" each time. Simple and effective. As I said, let the pros handle it. You could never do something simply or effectively. + +### Functions: Script's Little Helpers + +Functions let you group commands together so you can reuse 'em later. Think of them as your shitty script's shitty sidekicks. Not that your script is a superhero. Don't think that highly of the quality of your shitty code. + +```bash +#!/bin/bash +greet() { + echo "Hello, $1!" +} +greet "Alice" +``` + +Your shitty script just greeted Alice. How thoughtful! When will you meet your "Alice"? That's right! Never. + +## Advanced Tricks: Scripting Magic + +Now that you've got the basics down, let's step it up a notch with some fancy tricks. It's getting better right? If you think so, please leave. + +### Command Substitution: The Magical Swap + +```bash +#!/bin/bash +files_count=$(ls -l | wc -l) +echo "Total number of files: $files_count" +``` + +Your shitty script just counted the files in the current directory. Pretty cool, huh? Wait a sec... What the fuck is "ls" or "wc"? IDK. Go google it. + +### Conditional Expressions: The Mind Reader + +```bash +#!/bin/bash +# Arithmetic comparison +if (( 5 > 3 )); then + echo "5 is greater than 3" +fi + +# String comparison +str1="hello" +str2="world" +if [[ "$str1" == "$str2" ]]; then + echo "Strings are equal" +else + echo "Strings are not equal" +fi + +# File test +if [[ -f "myfile.txt" ]]; then + echo "File exists" +fi +``` + +Your shitty script just became a mind reader, checking numbers, strings, and files like a pro. Please don't try this in real life. You can't read your own mind and you definitely can't read the mind of others. + +### Error Handling: Keeping It Cool + +```bash +#!/bin/bash +set -e # Exit on error +set -u # Treat unset variables as errors + +# Example script that may encounter errors +missing_file="nonexistent.txt" +cat "$missing_file" +echo "This line will not be executed" +``` + +With error handling, your shitty script knows when to gracefully bow out instead of causing a scene because god knows your shitty code is filled with errors. + +### Input and Output Redirection: Scripting Traffic Control + +```bash +#!/bin/bash +# Redirecting output to a file +echo "Hello, World!" > output.txt + +# Appending output to a file +echo "Appending text" >> output.txt + +# Redirecting input from a file +while read line; do + echo "Line: $line" +done < input.txt +``` + +Your shitty script just became the traffic cop of input and output streams, directing them wherever it pleases. You're still telling it where to direct them so it's as good a cop as you are. Good luck doing anything useful with this. + +### Advanced String Manipulation: Scripting Origami + +```bash +#!/bin/bash +# Substring extraction +string="Hello, World!" +echo "${string:7:5}" # Extracts "World" + +# Pattern matching +if [[ "$string" == *"World"* ]]; then + echo "String contains 'World'" +fi + +# Case modification +uppercase_string="${string^^}" +echo "Uppercase: $uppercase_string" +``` + +Your shitty script is now a black belt in string-fu, slicing, dicing, and transforming strings like a ninja. Yes, it takes a literal ninja to handle the monstrosity that your shitty code is. + +## Best Practices: Scripting Etiquette + +As you venture further into the scripting world, keep these tips in mind: + +- Use meaningful variable names. foo1, foo2 doesn't make any fucking sense, but well, what does make sense? +- Keep your code tidy with proper indentation. We all know you ain't doing shit. Move on. +- Handle errors gracefully. Even though there's nothing graceful about your shitty coding practices. +- Break down complex shitty scripts into smaller shitty functions. Ever ate an entire burger in a single bite? No right? +- Document your shitty code like you're writing a love letter. Because honest to god, you ain't finding someone IRL to write a love letter to. + +## You're Now a Scripting Rockstar + +Well, congrats! You've made it this far. What's next? IDk. Do whatever the fuck you want. What do I care?. Good luck writing shitty scripts to do your shitty tasks. + +Happy Shitting!
\ No newline at end of file |