Happy with my bash progress!
from harsh3466@lemmy.ml to linux@lemmy.ml on 16 Feb 00:53
https://lemmy.ml/post/26067118

This is just me celebrating a small win. I’ve been slowly learning bash scripting, and just now I was able to quickly write a simple bash script to automate a file moving task without referring to my notes or the web!

It’s not a super complicated script, I’m just happy I’m starting to internalize the knowledge I’ve been building.

I’ve been organizing my media files after ripping our DVD collection. I had all the files for The Smurfs cartoon (love the Smurfs) in the main Smurfs show folder. I wanted to put them all into their respective season folders (Season.XX). Here’s the script:

#! /bin/bash

for number in {01..09}; do
	find . -type f -name "The.Smurfs.S$number*" -exec mv {} Season.$number/ \;
done

I could have done it as a one liner, but I like to keep things like this for future reference.

#linux

threaded - newest

lupusblackfur@lemmy.world on 16 Feb 01:28 next collapse

Pfft…

Good job.

The “one liner” thing is entirely overrated.

Honestly, who the fuck cares…?? 🤷🤷

harsh3466@lemmy.ml on 16 Feb 01:49 collapse

Thank you! I always worry with a one liner that I’ll fuck something up in a bad way. In a script it helps me think about the process since it’s more visually structured, and for me it’s easier to test.

curbstickle@lemmy.dbzer0.com on 16 Feb 03:44 collapse

I’m with them, one line is for showing off, not utility.

Utility has comments!

Edit: BTW, throw a variable in there and you can set up your own handy rename/renumber script for anything!

mike_wooskey@lemmy.thewooskeys.com on 16 Feb 01:29 next collapse

Congrats! It’s fun and empowering to make your own tools, isn’t it?

harsh3466@lemmy.ml on 16 Feb 01:47 collapse

Absolutely. I love it.

Lettuceeatlettuce@lemmy.ml on 16 Feb 01:31 next collapse

Nice! Might modify this and use it for my own collection if that’s cool 🤗

harsh3466@lemmy.ml on 16 Feb 01:47 collapse

Of course! Feel free!! And thank you!

land@lemmy.ml on 16 Feb 01:35 next collapse

Great job!

Learning Bash scripting and Vim is on my to-do list. 🎯

harsh3466@lemmy.ml on 16 Feb 01:46 collapse

Thanks! I wrote the script in vim too! :D

I’m no vim expert but I do like it.

ookiiBoy@lemmy.blahaj.zone on 16 Feb 02:10 next collapse

Look up “shellcheck”, find a plugin for your editor. It should help a lot.

harsh3466@lemmy.ml on 16 Feb 02:14 next collapse

Oh goodness, this looks incredibly useful. Thank you!

atzanteol@sh.itjust.works on 16 Feb 03:55 collapse

This is excellent advice. Bash is fantastic but it has a lot of “foot guns” that can cause problems. Especially where spaces are concerned…

Op: good job with the script

electric_nan@lemmy.ml on 16 Feb 04:04 next collapse

I have studiously avoided learning any bash scripting for the 17 years I’ve used Linux, so all I can say is good job! Actually just today I found a command that I needed to get a certain appimage to run without crashing, and I remembered enough that I was able to make it into a script (I struggle with whether it’s !# or #!). Having just done it today, I can confirm you don’t need to include ‘/bin/bash’, just FYI. I believe that is assumed.

LeninOnAPrayer@lemm.ee on 16 Feb 04:20 collapse

It makes it usable without typing bash. Same would apply for a python script. For example you can make a python script named with no extension and add #!/usr/bin/python to the top of the file. Bash shell sees this and knows to execute the script using that python path.

Then you just include the directory in your $PATH and chmod +x the script. Then you can type $python_script instead of $python python_script.py

electric_nan@lemmy.ml on 16 Feb 04:25 collapse

Interesting. This particular script I’m just double clicking to run, but I did name it script.sh. If I were to run it in the terminal, I would just do ./script.sh

LeninOnAPrayer@lemm.ee on 16 Feb 07:37 collapse

Yes. You’re correct. The script will execute with /bin/bash by default but only in a bash shell. #!/bin/bash is still a good habit to have. Some platforms may be running an “sh” shell by default. In this case if you ran the script it would execute with /bin/sh instead. Which would work or not work depending on if your script was written in purely sh syntax and not using any uniquely bash style syntax.

Bash can run all sh scripts but sh cannot run all bash scripts. So explicitly stating for which one your script was written for is good practice to not run into errors if you move your script to a different environment.

crmsnbleyd@sopuli.xyz on 16 Feb 04:22 next collapse

find is super arcane; most people don’t know how to use it. Congrats!

(a note for the future with all programming, be careful of numbers with leading zeros, they might indicate base-8, causing your program to fail on 09)

double_quack@lemm.ee on 16 Feb 04:56 next collapse

Good job! Flexible tools empower us. Keep it up and spread the love with other people!

JubilantJaguar@lemmy.world on 16 Feb 10:58 next collapse

Good job. But don’t worry if you have to look up answers. I’ve been at this for 20 years and I still have to look up and double-check basic syntax like the classic find -exec one. No big deal if only takes a couple of seconds.

This is definitely the sort of thing that LLM AI tools can help with, in theory.

MyNameIsRichard@lemmy.ml on 16 Feb 12:16 collapse

Yeah, you can’t keep it all in your head. Knowing what to look up is the better part of the battle.

[deleted] on 16 Feb 12:33 next collapse

.

bravemonkey@lemmy.ca on 16 Feb 13:09 next collapse

Shorthand is hard to learn from and hard to troubleshoot in complicated scripts.

MonkderVierte@lemmy.ml on 16 Feb 13:13 collapse

I keep it to shorthand for simple if-then, but write multi-line actions or if-then-else (because they can be tricky) out. It’s manageable this way.

DollyDuller@programming.dev on 16 Feb 13:33 collapse

If you want to share your script with others, I think it’s a good idea to make it as easy to read as possible.

If you’re just keeping it for yourself, that’s fine. But if you’re sharing it with me, the more readable it is, the easier I’ll be able to understand what you’re trying to do and how you’re solving the problem. This will make it easier for us to discuss ideas and improvements together. To be honest, one-liners can be a bit confusing because they can do multiple things at once. Breaking things down into individual steps makes it easier for me to follow along and for you to understand why you made certain decisions.

Plus, it’s a good habit to get into for your own future reference - you’ll be able to look back and understand your thought process more easily.

As the famous saying from SICP goes:

programs must be written for people to read, and only incidentally for machines to execute

harsh3466@lemmy.ml on 16 Feb 15:38 collapse

Agree. Make it as easy to read as possible. I learned this particularly after I had written a script that had a lot of nesting. It worked initially, but not for long and when I went back to debug I was like, “What the fuck was I thinking here?”

I ended up completely rewriting it to minimize the nesting and make it much more efficient and readable

juipeltje@lemmy.world on 16 Feb 12:37 next collapse

I really love bash scripting. I use it to symlink my dotfiles from my git repo, to configure my void system the way i like it, dmenu scripts, and i also use it on my laptop to send me notifications when my battery is almost empty, when i plug/unplug the charger, when it’s fully charged, and to hibernate when it’s lower than 5%. For some reason apps like upower don’t seem to work on my laptop so bash offered a solution.

harsh3466@lemmy.ml on 16 Feb 15:35 collapse

That’s fantastic. I’m not using it that deeply yet. I do have other scripts for managing my media files and adding them to my server as I rip music and DVDs. I also am loving learning it and using it.

akalanka@masto.es on 16 Feb 14:03 next collapse

@harsh3466 how cute! Happy to see you learn hehe

tasankovasara@sopuli.xyz on 17 Feb 06:19 next collapse

I’ve had to learn heavy duty bashing for work, and happily did take the plunge. However, they also had me learn PHP and I’ll drop this as a hook and line for OP: you can do shell-script duties with PHP also, and once you hit your head on sed enough times, I hope you remember me telling this. All that string manipulation is much nicer with PHP functions, and for running shell commands there is shell_exec(). :)

dukatos@lemm.ee on 18 Feb 13:14 collapse

Use shellcheck to test script syntax.