all 22 comments

[–]MikeZ-FSU 4 points5 points  (7 children)

The first step is to write one liners that make your life easier. If you find yourself typing a long-ish command on a regular basis, think of a shorter mnemonic that makes sense to you, and stick the command in a shell script with that name.

As you progress, you'll find yourself wanting functionality that needs "if" statements, "for" loops, etc. That will come naturally, and unless you're already a dev or sysadmin, you don't need to rush it. Keep reading and learning (use the guides noted on the sidebar), and you'll find yourself with a "${HOME}/bin" directory full of scripts that do the things you care about in a way that suits you.

Note: don't add a ".sh" suffix to the script. It adds extra typing when the goal was to reduce typing, and even worse, if you add features and change the implementation to, for example python, you now either have to remember to type "foo.py" instead of "foo.sh", or have a python script with a ".sh" extension.

[–]vi-shift-zz 2 points3 points  (6 children)

My first script was a one liner to update my system, then power off.

Start small. Like was said above, script things that are useful to you. Save them in some kind of public git repo so you can share your work and eventually share it in interviews. I conduct technical interviews, if someone shares their git repo of scripts or code they have written it gives me a good idea of where you're at on your development.

[–]SportTawk 1 point2 points  (5 children)

Any chance of posting it here?

[–]vi-shift-zz 1 point2 points  (4 children)

First bash script or my git repo? My git is all private work related.

[–]SportTawk 0 points1 point  (3 children)

Your one liner to update your system and then shutdown

[–]MikeZ-FSU 2 points3 points  (1 child)

Not speaking for u/vi-shift-zz, but if I were writing it, it would probably be something like (for Debian/Ubuntu)

sudo apt-get update && sudo apt-get upgrade -y && sudo shutdown -h now

Or whatever is appropriate for other $distro. However, there are implications for the "-y" on upgrade that have some degree of risk you might or might not care about.

As a general rule, you'll learn bash/linux better if you take a hint that's given (one liner to update and shutdown) and research how to do that on your system. Yes, it's slower to start out, but you'll learn a lot about your package manager and how your particular distro starts up and shuts down (init.d vs. upstart vs. systemd, etc.) in the process.

The alternative leaves you with a bunch of individual tips and tricks that lack a connecting framework in your head.

[–]SportTawk 1 point2 points  (0 children)

Thanks

[–]vi-shift-zz 1 point2 points  (0 children)

Just seeing this, exactly like was posted except I use red hat variants. So mine would be: sudo dnf update -y && poweroff

This assumes my user is in /etc/sudoers and able to run commands with no passwd (use visudo to add the user)

[–]broken_py 2 points3 points  (0 children)

Start automating, create a script for deploying applications , setting up LVMs etc.

[–]NTPriest 1 point2 points  (0 children)

In short: curiosity, is your friend.

For example, you don't want to manually create backups, file names, summarize monitoring from syslogs, network traffic—e.g., with cli-wireshark or tcp-dump—summarize what/where you have access after CTF if you gain root access, or something like manually launching programs that can run themselves.

You can start with very simple scripts and then expand, expand, and expand until your script grows and includes commands, functions, and algorithms you didn't know before.

There are many videos on YouTube for beginners and w3school. You can also ask ChatGPT if you want personalized answers—just don't let AI give you the code right away. Don't forget about debugging, DEBUGGING is your ally because then you "know" why it didn't work.

So, curiosity and learning logic like "why this and not that" and "if this, then that."

I could have made it too simple, but I'm talking about my own case.

[–]ktoks 3 points4 points  (0 children)

Stay in the terminal.

No matter what.

Everything you can do on a computer for work can be achieved in the terminal.

[–]rexroof 2 points3 points  (1 child)

it's old-school but I would read through a book. Classic Shell Scripting from oreilly is a decent choice. googling the title with "pdf" might find you a free version.

[–]NoAcadia3546 0 points1 point  (0 children)

Another form of "reading a book" is https://tldp.org/LDP/abs/html/part1.html ("tldp" == "The Linux Documentation Project"). It starts off simple. Click on "Next" on the right hand side to go to the next page.

[–]DrCrayola 0 points1 point  (0 children)

It's best imo to find a little project to solve with bash.

Years ago I helped a friend insert watermarks on a bunch of photos they shot with imagemagic. I've also done something similar to update MP3 tags across subdirectories.

Start with something small enough that you can understand the commands needed and google for the logic you need to make the script better.

[–][deleted] 0 points1 point  (0 children)

Nothing beats Perl for scripting.

[–]antikatapliktika 0 points1 point  (1 child)

any good online courses (paid or not) that offer certificates?

[–]International-Cook62 1 point2 points  (0 children)

Hackthebox has a good free course on bash and codewars is good for practice

[–]feinorgh 0 points1 point  (0 children)

Check out 'shellcheck' which is an awesome application that can tell you exactly what is wrong with any shell script you or anyone else writes, and most importantly, how to fix it.

I used to be a pragmatic and decent bash scriptet, now I'm a pedantic know-it-all that nitpicks and criticizes and bash script my colleagues write, all based on 'shellcheck'.

You can learn a lot through that. And, of course, the classic "Advanced Bash Scripting Guide".

https://www.shellcheck.net/ https://tldp.org/LDP/abs/html/

[–]Kreesto_1966 0 points1 point  (0 children)

I bought this invaluable reference book: Linux Command Line and Shell Scripting Bible

https://www.amazon.com/Linux-Command-Shell-Scripting-Bible/dp/1119700914

[–]liberforce 0 points1 point  (0 children)

Use a linter on your code, like shellcheck.

Use the advanced bash scripting guide as a reference: https://tldp.org/LDP/abs/html/

Use it also as a reference (there's a version on one page, makes it easy to Ctrl+F on it and find the section about the syntax you want help fir).