14
gvnix
6y

When you write shell without knowing how to.

Comments
  • 3
    @IllSlapU - good lookin out. Sometimes a little nod to say we are are not writing complete and utter shit is helpful.

    Especially in bash - goodness knows I feel my scripts are an embarrassment at times.
  • 3
    That’s a pretty readble script for someone who doesn’t know what he’s doing! My 2 cents:

    - You are using sub shells where you don’t really need them. ‘Echo $(wc -c something) | bla ‘can become ‘wc-c something | bla’, wc already prints its output to stdout.
    - I personally prefer using ‘declare’ in stead of local. When used in a function, declare behaves the same as local, but when using it outside of a function you still get the benefit of more explicit variable declaration. You can read more about the declare builtin by running ‘help declare’. (The -p option is awesome for debugging btw)
  • 2
    @IllSlapU thanks man, appreciate all your inputs. :)

    I will take a while before I get comfortable in bash. Unlike Java there is no one holding your hand while crossing the road.
  • 2
    @bashlord thanks for the inputs.

    Language can change but code practices don't. Code should always be readable.
  • 0
    Keep your indentation consistent.
  • 1
    @IllSlapU Unless called with the -g option, declare limits the scope of the declared variables to the block it is used in, just like local does. Local in bash is actually kind of an alias of declare as it takes the same options for type declarations like -a and -A. Which brings me to this: I think that using declare for ALL variables, even outside of functions is a very good habit. If this is done consistently:

    - declaration of global variables becomes more explicit as you use explicitly state that you want to make the variable global by using the -g option
    - You get explicit “type” declarations by using -a for an array, -A for an associative array, -i for an integer and -r for readonly constants.
    - you get a consistent way of declaring variables, which is not dependent on the context of the declaration.
    - if a script is using declare, you can source it inside of a code block without it contaminating your current shell because the variables will be scoped to the block.
  • 1
    The only difference between declare and local is actually that declare has some more options, and local throws errors when used outside of a function or block.
Add Comment