Bash scripting 101

ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
Hey guys does anybody have any tips on the best way to pick up and learn Bash scripting I have tried plowing my way through numerous guides. But need a similar approach to Powershell in Lunchbreaks does such a thing exist?

How did you guys get to grips with Bash scripting I need a really noob freindly guide that walks you through different scripts and mechanics slowly

Many Thanks

i.e Bash scripting for numpties :)
Microsoft's strategy to conquer the I.T industry

" Embrace, evolve, extinguish "

Comments

  • jvrlopezjvrlopez Member Posts: 913 ■■■■□□□□□□
    To what level are you looking at being capable at?

    To be honest, the way I picked up on it was to just play around with it. It was easier for me to script a few simple tasks (read, locate, grep, then uniq, and sort) then to go through and read some book. If you have some sample or working scripts that you like, I'd recommend looking at their lines and seeing how they function.

    I wrote a simple bash automation script to start up a cuckoo sandbox and bring up Firefox for a new employee who wanted it done. He had never done any bash scripting before but took my template, ran with it, and did a good job touching it up.
    And so you touch this limit, something happens and you suddenly can go a little bit further. With your mind power, your determination, your instinct, and the experience as well, you can fly very high. ~Ayrton Senna
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    I was going to recommend the above book as well. You could also get it for free on their website LinuxCommand.org: Learn the Linux command line. Write shell scripts. but I liked the formatting on the production version better.

    After finishing up The Linux Command Line I'm now following up w/ Unix Shell Programming (3rd Edition): Stephen G. Kochan, Patrick Wood: 9780672324901: Amazon.com: Books
    Currently 2/3rd of the way thru and altho it's pretty dry, the knowledge inside is great.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • the_Grinchthe_Grinch Member Posts: 4,165 ■■■■■■■■■■
    I agree that you should just start writing scripts. Find a task you want to automate, plan out how it should work, and start typing. I didn't have a lot of knowledge, but I wrote a script without too much trouble. My script does the following:

    Go to three websites and download a file
    Perform hash on the files that are downloaded
    Perform hash on the original files
    Compare the hashes
    If hashes match, send email saying that the file hasn't changed and place it in an archive folder
    If hashes don't match, perform a diff, then email the diff file to me, and then move to failure folder
    WIP:
    PHP
    Kotlin
    Intro to Discrete Math
    Programming Languages
    Work stuff
  • philz1982philz1982 Member Posts: 978

    I used this book reading a chapter every morning when i woke up.
  • JockVSJockJockVSJock Member Posts: 1,118
    Totally free

    Linux Shell Scripting Tutorial - A Beginner's handbook

    Also check out thegeekstuff.com too
    ***Freedom of Speech, Just Watch What You Say*** Example, Beware of CompTIA Certs (Deleted From Google Cached)

    "Its easier to deceive the masses then to convince the masses that they have been deceived."
    -unknown
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    I want to be capable of writing scripts for basic system administration tasks. Such as backing up files, Opening a specfic firewall port or automating adding a system user that kind of thing.

    I also would like to create a menu for scripts where you can run options and call other scripts is that possible? I.e create a admin menu with options such as backup, install package, open port

    Currently I am comfortable with commands and concepts and use Vim as my editor of choice, Scripting wise I can write basic stuff i.e chain commands together to run in a sequence, echo out stuff and get input from whoever runs the script.

    I have tried to work my way through some material like advanced bash scripting and beginners guides but found them both to be dry and sleep inducing.
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • hiddenknight821hiddenknight821 Member Posts: 1,209 ■■■■■■□□□□
    I used Pro Bash Programming book written by Chris Johnson. It has some good fundamental and intermediate-to-advance examples. I'd use it as my reference.

    It seems like you have a solid idea what you wanted to do with a script. If you have a solid programming fundamental, then you probably can do without those books. All you probably need is a **** sheet explaining how to do the conditional test, loop, set variable, and so forth with the proper syntax. You probably also need to know some built-in shell variables. Here are some good examples starting with the dollar/peso sign which are useful in a script:

    $# $? $$ $*,$@ "$*" "$@"

    Again, you'd need to refer to some sources to learn and understand the power of bash scripting and use the examples as a template. Don't try to think too big. Start in a small scale. Test your program to see if it can accomplish the first task before adding more lines to the script to avoid ambiguity.
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Thanks for the reply I guess I am a visual learner and learn best from getting hands on / watching videos I have a subscription to Udemy Bash Scripting course and have the CBT nuggets series on Bash scripting
    ( Old skool videos) I may sit down take things slow and build a foundation I guess I am trying to over complicate things and run before I can walk :)

    Somebody Should write a Bash scripting project book or something that would be awesome aimed at noobs it would walk you through creating a project and gradually introducing more advanced concepts with each lesson does such a thing exist? if not why not? somebody should get writing :)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • the_Grinchthe_Grinch Member Posts: 4,165 ■■■■■■■■■■
    I'm not a programmer by trade, but I believe you'd want to write functions for each of things you want to do. Thus you have the menu options appear, ask the user to enter a number, take that number and run it into a loop. That in turn will select the function that will take the settings you specify and run the task you want. I'm sure someone here might know of a better way that would make the script more efficient, but with a couple of hours of time you should be able to write that pretty quickly.
    WIP:
    PHP
    Kotlin
    Intro to Discrete Math
    Programming Languages
    Work stuff
  • UnixGuyUnixGuy Mod Posts: 4,570 Mod
    I strongly recommend this book:
    http://www.amazon.com/Linux-Shell-Scripting-Bash-Burtch/dp/0672326426?tag=viglink20307-20

    and then this one for practice:
    http://www.amazon.com/bash-Cookbook-Solutions-Examples-Cookbooks/dp/0596526784

    I believe those two books are all you'll ever need...
    Certs: GSTRT, GPEN, GCFA, CISM, CRISC, RHCE

    Learn GRC! GRC Mastery : https://grcmastery.com 

  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Thank you for all the help guys :)

    Unix Guy that book you recommended has been really good I am slowly working my way through it typing in various commands and examples trying to build a grounding.

    Other stuff I am using

    Advanced Bash Scripting Guide
    Linux Shell Scripting With Bash - Ken O Burch
    CBT Nuggets Video Bash scripting - Dean Davis ( really good if you are a visual learner like me )

    http://bash.cyberciti.biz/guide/Menu_driven_scripts - really great site! very informative!

    Built myself a basic script which has a menu and has a few functions and a Loop to run the logic

    So I am gradually getting somewhere :)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    I've heard that it's a bad idea to learn bash scripting from the Advanced Bash Scripting Guide.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Howcome?
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • nice343nice343 Member Posts: 391
    check linuxcbt. The guy who teaches it is a little boring but he has an example of almost every script a network engineer/administrator will need on a daily basis.
    My daily blog about IT and tech stuff
    http://techintuition.com/
  • ExpectExpect Member Posts: 252 ■■■■□□□□□□
    I have also used the previously recommended Pro Bash Programming book written by Chris Johnson book. it was pretty decent.
  • varelgvarelg Banned Posts: 790
    ally_uk wrote: »
    I want to be capable of writing scripts for basic system administration tasks. Such as backing up files, Opening a specfic firewall port or automating adding a system user that kind of thing.

    I also would like to create a menu for scripts where you can run options and call other scripts is that possible? I.e create a admin menu with options such as backup, install package, open port

    Currently I am comfortable with commands and concepts and use Vim as my editor of choice, Scripting wise I can write basic stuff i.e chain commands together to run in a sequence, echo out stuff and get input from whoever runs the script.

    I have tried to work my way through some material like advanced bash scripting and beginners guides but found them both to be dry and sleep inducing.
    Seems like a sys admin http based portal is what would suit you. Ah but you'd love to know what's behind the buttons...
    Start with variable declaration and loops, then learn the built in variables, then pick up some sed and/or awk. Bash by itself is pretty powerful, but it has its limitations.
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Didnt like the pro bash book thought it was a bit to above my level and a bit all over the place.

    bash has limitations what would they be?
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • UnixGuyUnixGuy Mod Posts: 4,570 Mod
    Author of the Pro bash book is really opinionated..I'd stay away to be honest.

    Bash has limitations but others will disagree with this statement. I wouldn't concern myself with the limitations. For system administration I think it's enough. (Keeping in mind that the world is changing now and Configuration management tools like Puppet are the new way of doing things).
    Certs: GSTRT, GPEN, GCFA, CISM, CRISC, RHCE

    Learn GRC! GRC Mastery : https://grcmastery.com 

  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    DoubleNNs wrote: »
    I was going to recommend the above book as well. You could also get it for free on their website LinuxCommand.org: Learn the Linux command line. Write shell scripts. but I liked the formatting on the production version better.

    After finishing up The Linux Command Line I'm now following up w/ Unix Shell Programming (3rd Edition): Stephen G. Kochan, Patrick Wood: 9780672324901: Amazon.com: Books
    Currently 2/3rd of the way thru and altho it's pretty dry, the knowledge inside is great.

    This combo I posted above really, really helped me w/ my bash scripting. Try to read them, even if you only skim thru them, and in that order. By the time I got thru w/ them, most of the tasks I could do manually on the Linux/Unix CLI I could find a way to automate using bash shell scripting.

    Once you know what the tools are, what is possible using them, and a few of the options and use cases, you can use man pages and google (stackexchange, the geekstuff, etc) to get thru the syntax.

    And from those books I'd probably learn sed/awk a little bit more in depth, and then maybe seek out either a more in depth shell scripting book, or one that has more example scripts.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
Sign In or Register to comment.