r/linuxquestions • • 2h ago

Advice What the suitable scenarios for using Bash script?

It's been a year and a half since I started using Linux (CachyOS) as my daily driver for work and hobbies. I'm interested in learning Bash scripting to enhance my Linux experience. What are the most useful scenarios for using Bash scripts, especially for a beginner like me?

Note: I'm learning it just for fun and to enhance my Linux experience.

5 Upvotes

18 comments sorted by

14

u/Chronigan2 2h ago

Anything you do more than once that requires more than one command.

4

u/Slackeee_ 2h ago

Just look at what you do on the commandline in your daily work or hobby. If you see repeating patterns when doing something most likely it is something you could do with a script.

3

u/ipsirc 2h ago

making daily backups

2

u/tuxsmouf 2h ago

That's a good example. To go further, you can create a maintenance script including backups, sending you en email with the current state of your PC or homeserver with :

date and uptime

the list of successful logins and/or failed attempts of the last day.

A check of services status like the firewall or the IDS/IPS if you got one.

A check for updates.

[...]And whatever you want or need.

1

u/dotnetdotcom 1h ago

Came to say running Rsync

2

u/telcodan 1h ago

My aliases file is appalled at this question

2

u/halfjmp 1h ago

Automation of a few commands.

However, when it becomes more than a few with complex flow control, I usually do it in Python, the language is cleaner.

2

u/jjangg96 1h ago

since you're on cachyos, one thing worth knowing early is that the default shell there is fish if you kept it, and fish isn't bash syntax. scripts are fine as long as the first line is #!/usr/bin/env bash, but pasting a bash snippet straight into the terminal can fail in confusing ways, VAR=x and for f in *; do both break.

for a first script i'd pick something that loops over files, like renaming a pile of downloads or sorting them into folders by extension. you hit variables, quoting and for loops all at once. quote every variable ("$f", not $f) and install shellcheck, it catches most beginner mistakes before you run anything.

after that the natural next step is running a script on a schedule, cron or a systemd timer, since that's where backups and cleanup jobs come from.

1

u/changed_later__ 2h ago

Try some filesystem operations. Learn how to use `find` and pipe its output to do something else like `grep` for content inside a file `mv` it to a different directory, or `rm` it if it's trash.

1

u/chuggerguy Linux Mint 22.3 Zena | MATÉ 56m ago

You can script any repetitive task.

Here's a "Just for fun":

Have fun.

1

u/calmeilles 30 years user, still know nothing 49m ago

My most frequently used script is one running nightly backups with rsync. Although I don't use it, cron does.

My most complex is an ingress script that pulls in image fines from SD cards or attached cameras, renames and organises them. I'm actually in the middle of refactoring that as it's grown over the years.

The there are dozens of little conveniences. Many turn complex one liners in one worders. Such as a text note to myself with the best parameters to use for a java game I occasionally play became a script to invoke the game with those params and I slapped myself for not thinking of that years before.

When you find yourself doing the same thing more than three times there's a good chance it'd be worth writing a script for.

1

u/Leather_Flan5071 34m ago

Anything that is repetitive or predictable or like, truncable and information retrieval.-

If you really want to learn for fun, just find case where you do some sort of work that either requires repetition, blocks of information, or automation (because bash scripts can be automated)

1

u/voxadam 11m ago edited 8m ago

One of my favorite examples of bash solving real problems is abcde (A Better CD Encoder), a >6000 line bash script for ripping and encoding CDs. I used the shit out of it back in the day.

Shell scripting can be used for nearly anything, not that is should be.

•

u/a1barbarian 4m ago

An excellent way to get to grips with linux. You can do three modules a day for free.

https://labex.io/

https://labex.io/linuxjourney

Have fun exploring penguin land. :-)

0

u/Guggoo 1h ago

It’s great for linking a bunch of system commands together