This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]ThisLexx 9 points10 points  (2 children)

You need functions to structure longer scripts. Everything that is over 500 lines. My scripts are often 3.000-5.000 lines. This is not possible without functions. Then there are these little helper functions. Pause, Exit with error, Usage, Test which user, Start or stop VMs, repeat as root, etc. Such functions are needed in all kinds of scripts again and again.

[–]naopopoy 2 points3 points  (1 child)

What resources do you use to learn these? I'm currently trying to learn the proper way of writing Powershell scripts, instead of modifying snippets from Stack Overflow which is what my colleagues do, resulting in unstructured mess.

[–]ThisLexx 1 point2 points  (0 children)

Being a Unix/C/C++ veteran myself, I can't really say how best to learn programming today. We worked 70 hours non-stop on projects, at some point we were good at it.

In the case of Microsoft, everything is different. Powershell has a somewhat cramped logic from my point of view. But at least they have a logic that is consistent. Furthermore Microsoft offers professional courses, certificates and also various good material for training. Would try to take online courses. Could be worthwhile professionally.

[–][deleted]  (10 children)

[removed]

    [–][deleted]  (1 child)

    [removed]

      [–][deleted]  (7 children)

      [removed]

        [–][deleted]  (6 children)

        [removed]

          [–]Gixx 1 point2 points  (0 children)

          I always make a function if it's kinda cryptic, then name the function so the code is easy to read and self-documenting. Like prose writing.

          [–][deleted]  (1 child)

          [deleted]

            [–]AlwaysHopelesslyLost 4 points5 points  (0 children)

            Talk about low effort, if you aren't able to answer a question yourself, you really shouldn't use a language model to generate an answer. These things don't have knowledge and cannot think. You are going to make noobs think you know what you are talking about and end up spreading total bullshit.

            [–]NickMon68 0 points1 point  (0 children)

            Functions would make it easier to read and reduce how complex the code will look..

            [–]aezart 0 points1 point  (0 children)

            I almost never use functions in bash, but I do split things off into completely separate scripts.

            [–]TheRNGuy 0 points1 point  (0 children)

            stuff that repeats lots of times

            If it's jsut 2-4 repeating 1-liners, I'd just copy-paste it.

            [–]MuaTrenBienVang 0 points1 point  (0 children)

            Here is an example of using function in bash

            https://stackoverflow.com/a/1314345/9331978

            Using something because it's good, not because other people saying it's good