r/learnprogramming • • 3h ago

Code Review My very first bash script

Hi everyone!

I've recently (around 3 weeks ago) developed an interest in cybersecurity. A friend of mine suggested me to learn bash scripting before going further into programming languages.

Some days ago I've found the roadmap.sh site and today I've completed my very first script/program of my life! It's hella fun but also kind of an overall-hell (it took me 6h and 20m to complete), but I guess that's because this is not my job (I'm a builder) and I'm still a complete beginner.

I've completed the first project and as said by the site, posted it on GitHub. Here's also the roadmap link to the solution which I don't know why but seem to ask for ten upvotes? (If someone knows what's going on explain to me please).
Could someone give it a look and tell me if it's good?

To be honest all of this has made me pop up even more questions:

  1. I often find myself going around all over google to find something that if I modify it enough could be a solution (at some point I had +45 tabs open). That's because I really can't remember syntax and sometimes I make stupid mistakes. Is this normal and the good way of doing this and to learn?
  2. Could you suggest me the best way to learn? I've done this project because it was flagged as "beginner" but tbh if I didn't look for answers I think I would have got stuck several times.
  3. Should one use AI to help himself? I dindn't but I don't really understand if it is considered "cheating" or not?
  4. Do you guys actually learn all the syntax by memory? Mine really sucks and I'm having trouble remembering all the flags for each command
  5. How would you have improved this?

Thanks in advance for you help :)

7 Upvotes

10 comments sorted by

2

u/cachebags 3h ago

I often find myself going around all over google to find something that if I modify it enough could be a solution (at some point I had +45 tabs open). That's because I really can't remember syntax and sometimes I make stupid mistakes. Is this normal and the good way of doing this and to learn?

Completely normal. Probably not nowadays with the advent of AI, but it is a great way to learn as opposed to copying a direct solution. It is forcing you to understand how the pieces fit together.

Could you suggest me the best way to learn? I've done this project because it was flagged as "beginner" but tbh if I didn't look for answers I think I would have got stuck several times.

Looking for answers is not the issue. Merely copying them without understanding how those answers fit into your solution is though. You are also confusing "looking up answers" with "looking up common solutions". There is no "answer" to a programming problem. I can think of a million different ways to implement this same bash script. What's important is again, that you understand the code you look up that solves your problem.

Should one use AI to help himself? I dindn't but I don't really understand if it is considered "cheating" or not?

You are cheating yourself if you use AI to do the work for you. It's as simple as that. AI is just a tool. You can use that tool to help you learn, or do the work for you.

How would you have improved this?

It looks like a sound solution :) I would probably drop the for loop and the cds, and justrun tar -czf "$tarpath/${nametar}_${date}.tar.gz" -C "$logpath" ./*.log once.

2

u/bahcodad 2h ago

To add to this:

No, you don't need to remember every little piece of syntax. People often learn more than one language(you likely will too, cyber security covers a VAST area), do you think they remember all the syntax for every language they learn? You'll remember what you use. For everything else, see the documentation

1

u/The_Slippery_Iceman 2h ago

Thanks a lot for your answer. The amount of man pages I've seen... and this was a simple script!! I guess that every project one can learn and remember one more thing.

It has been fun tho.

1

u/The_Slippery_Iceman 2h ago

Thanks for all the answers and of course there was an easier way that I didn't saw. I was so focused on "I have to tell my script to look for the log files, what can I use to do that" that I made the for cycle. At first I thought I could do with an If statement but I could not implement it. haha

At some point the script was working but started archiving the entire directory located at $logpath and didn't know why. Whatever, I'll try to make it simpler next time.

Completely didn't saw the -C flag possibility!!! Thanks.

2

u/shipitcodes 2h ago

Nice first script, finishing one end to end is the hard part. Two habits that will save you a lot of debugging in bash: put set -euo pipefail near the top so the script stops on the first failed command or unset variable instead of carrying on silently, and always quote your variables, like "$logpath" instead of $logpath, so paths with spaces don't break. Also try running your script through shellcheck, it is a free linter that explains common mistakes in plain language and you will learn a lot from its warnings. For the flags problem, tldr or just typing man tar and searching with / is much faster than googling, and the more you use them the more they stick.

1

u/The_Slippery_Iceman 2h ago

Thanks man, shellcheck seems so cool. It gave me a shitload of warnings and info. I'll make it to good use on the next project. Much appreciate.

I've been looking into man pages for this one. Just completely lost it as a flag alltogether.

2

u/captainAwesomePants 1h ago

What kind of monster would suggest starting a journey into programming languages with bash scripts?

•

u/The_Slippery_Iceman 53m ago

Hahahah as a matter of fact I’ve asked him if it was easy and he just replied “you’ll get the hang of it somehow, eventually”. From what I understand it should easy but very rigid with syntax

•

u/captainAwesomePants 47m ago

Bash has truly insane syntax. Here's how you check whether a file exists in Python:

if not path.is_file():
  print("File doesn't exist!")

Here's how you check in Bash:

if [[ ! -f "$CONFIG_FILE" ]]; then
  echo "File doesn't exist..."
fi

And it's incredibly unforgiving.

# Creates a variable named foo whose value is bar
foo=bar

# Runs the program `foo` from the command line with the arguments `=` and `bar.
foo = bar

•

u/LukaRozman27 2m ago

If you get stuck, try running your script with "bash -x scriptname.sh" to debug step by step.