Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
kunashe19687y@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. -
bashlord4387yThat’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) -
gvnix19227y@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. -
gvnix19227y@bashlord thanks for the inputs.
Language can change but code practices don't. Code should always be readable. -
bashlord4387y@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. -
bashlord4387yThe 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.
Related Rants
-
gururaju56*Now that's what I call a Hacker* MOTHER OF ALL AUTOMATIONS This seems a long post. but you will definitely ...
-
linuxxx70This guy at my last internship. A windows fanboy to the fucking max! He was saying how he'd never use anythi...
-
creedasaurus62Another dev on my team just got a new machine. Before he came in today I made two separate USB installers and ...
When you write shell without knowing how to.
rant
linux
bash
learning shell scripting
weekends