Giving Up With Bash Scripting
Been trying to get my head around this for ages but stuff just is not sinking in
I just can't seem to take in information I have read numerous guides and just end up more and more confused. I can write basic scripts but am having difficulty taking it to the next stage.
I wish there was a guide which gets you to create a basic script then makes you build it up over time.
Could really use some pointers
I have the Bash cookbook but currently that's just like reading jargon Same for the guides like advanced bash scripting guides it's just pages full of special characters, and concepts and no actual examples of coding.
Thinking of giving up and trying Python might be easier for me to grasp programming concepts or maybe I am not cut out for this
I just can't seem to take in information I have read numerous guides and just end up more and more confused. I can write basic scripts but am having difficulty taking it to the next stage.
I wish there was a guide which gets you to create a basic script then makes you build it up over time.
Could really use some pointers
I have the Bash cookbook but currently that's just like reading jargon Same for the guides like advanced bash scripting guides it's just pages full of special characters, and concepts and no actual examples of coding.
Thinking of giving up and trying Python might be easier for me to grasp programming concepts or maybe I am not cut out for this
Microsoft's strategy to conquer the I.T industry
" Embrace, evolve, extinguish "
" Embrace, evolve, extinguish "
Comments
-
Expect Member Posts: 252 ■■■■□□□□□□Bash is one of the easiest scripting languages out there, and IMO Python will not be easier than bash, my 2c.
where do you struggle with Bash? -
ally_uk Member Posts: 1,145 ■■■■□□□□□□You must be a Linux God I came from a non Linux background and am not a programmer /scripting genius
Where do I struggle with Bash at the moment? everything
Seriously I start reading these guides books they initially start off well you know create a basic hello world script then declare variables. Then more advanced concepts get introduced such as loops / conditionals / functions.
Part of me is becoming disheartened because I am failing to understand how to apply these concepts to a script that does anything useful
i.e create a script which presents a user with a menu with a few options that allows them to perform a few system administration tasks i.e doesn't have to be advanced stuff I just need the basics i.e Options
1) Shows amount of disk space free
2) Runs a backup script
3) displays ip address
4) Shows contents of a log file
See using the above as a example I just don't know how to get it off the ground would you recommend any guides? how did you learn scripting?Microsoft's strategy to conquer the I.T industry
" Embrace, evolve, extinguish " -
YFZblu Member Posts: 1,462 ■■■■■■■■□□IMO, Python's object-oriented nature will likely add more questions on top of the general logic you're already struggling with regarding Bash. Learning to code/script can be a big struggle initially - but keep pushing, and eventually it will click - and Bash is a great place to start.
#!/usr/bin/python codingIsEasy = False if codingIsEasy: print "Everyone would do it" else: print "Keep pushing yourself, it's worth it"
-
Expect Member Posts: 252 ■■■■□□□□□□This script is too "advanced" for beginners, you should start with the basics. of course it will be hard for you to write this kind of script with little knowledge.
to get this kind of script together you need to know how to use the case switch which I am not sure you know (do you?)
I suggest to start with the basics, a good reference for bash is nixcraft (plenty of shell scripts some of them are advanced some arent) -
Expect Member Posts: 252 ■■■■□□□□□□how about taking each of the steps you mentioned (like displaying an ip address) into seperate scripts? when you are done, you can simply take all code under one script and find examples of 'case' usages (linux case/esac)
-
ally_uk Member Posts: 1,145 ■■■■□□□□□□I briefly have touch on case switch and you are right I don't yet understand them. When you first started your journey learning BASH what did you initially use? did you read a book or just wrote scripts and tinkered? thanks for the replies guysMicrosoft's strategy to conquer the I.T industry
" Embrace, evolve, extinguish " -
YFZblu Member Posts: 1,462 ■■■■■■■■□□OP, Switch/case is just a more syntactically convenient way of writing if/elif/else statements.
-
ally_uk Member Posts: 1,145 ■■■■□□□□□□Part of me is just thinking ditch BASH and learn Python to do the same things? good move?Microsoft's strategy to conquer the I.T industry
" Embrace, evolve, extinguish " -
UnixGuy Mod Posts: 4,570 ModThere are good guides (I learned from them)...
I recommend these two books (in this order)
1) Linux Shell Scripting with Bash: Ken O. Burtch: 9780672326424: Amazon.com: Books
2) O'Reilly's Bash Cookbook: bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly)): Carl Albing, JP Vossen, Cameron Newham: 9780596526788: Amazon.com: Books
Trust me mate, follow these two books. Start with the first one to get you to understand the concepts, then do the second one. Try ALL the examples in these two books, and you will become a Bash Ninja - promise!
Not only that, you will also learn more about Linux when you finish these two books.
Get Started..now -
UnixGuy Mod Posts: 4,570 ModPart of me is just thinking ditch BASH and learn Python to do the same things? good move?
No, don't do that. Trust me mate, read (and practice) all the examples in the bash books that I recommended. Once you do that, you will have a decent Bash background. Learning Python gets easier if you know bash anyway, and I don't think any Linux professional should skip bash anyway. You will end up using Bash almost all the time and wonder when would ever need Python anyway. Bash is not hard. -
Bryzey Member Posts: 260Maby try videos instead of reading material? Linuxcbt and Linux academy both have bash scripting series.
-
Kai123 Member Posts: 364 ■■■□□□□□□□I've seen a good few network engineer positions that are in a Windows Server environment but ask for either python or Bash.
What are the concepts behind that? I migh try and learn bash myself but with literally almost no Linux experience, but with the aspiration of a network engineer/NOC engineer role in a Windows environment.
-
Kinet1c Member Posts: 604 ■■■■□□□□□□Linux academy bash series is good, working my way through it. You can pick it up on Udemy.com for relatively cheap.
Edit: There's a webinar today on bash from LPI: LPI Webinar Registration Page2018 Goals - Learn all the Hashicorp products
Luck is what happens when preparation meets opportunity -
DevilWAH Member Posts: 2,997 ■■■■■■■■□□
1) Shows amount of disk space free
2) Runs a backup script
3) displays ip address
4) Shows contents of a log file
Ok so now break down each of these in to the steps you would carry out.
take for example disk space..
you need to decided what disk, while this value be in script or will you ask the user each time they run it? or will you give the m a choice of the available ones?
ask the OS for the amount of disk space free (what's the command to do this?)
hold this value some where so you can display it
Dispaly it...
So
step1. get disk name
step2. run the command to see disk space and save result to a varible
step3. out put variable to screen.
So now take each one separately, use google and simple examples to work out how to take a user input and store it in a varible and then print it back to the screen...
Find out how to get the disk space from the CLi from a script with a hard coded disk name and print to screen.
replace the disk name in the second script with the code from the first script. so you have a single script that asks for a disk name and returnes the result.
Then work out how to get a list of disk from the CLi,
use google to create a script that gives you a choose of 3 numbers to press from a menu like below"
"
press 1 to print hello
press 2 to print goodbye
press 3 to print whatever
"
Now combine the list of disk names with the menu system, so the script runs, displays a list of disks for the user to chose.
Now add in a choise for disk space free / used / total.
This is how to get in to programming, start with really basic concepts, and then build them together bit by bit. dont try to do the whole thing in a single go. Programming is very logical, you need to break apart problems in to single tasks, write a script for that single task and then link it with others.
Your first scripts might be very long winded and inefficient, but as you develop you will find ways to do more in a single command and more efficient ways to do it.- If you can't explain it simply, you don't understand it well enough. Albert Einstein
- An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Linkin Profile - Blog: http://Devilwah.com -
ccnxjr Member Posts: 304 ■■■□□□□□□□Oh Nos!
You're much closer than you imagine, for example.
Your previous sample in Python#!/usr/bin/env python # System Gathering Script ################################################## ################ import subprocess # Command 1 def disk_function(): diskspace = "df" diskspace_arg = "-h" print "Gathering diskspace information %s command:\n" % diskspace subprocess.call([diskspace, diskspace_arg]) # Command 2 def system_services(): service = "chkconfig" service_arg = "--list" print "Showing Running Services %s command:\n" % service subprocess.call([service, service_arg]) # Main Function that calls other functions def main(): disk_function() system_services() main()
is very much similiar to this Bash script which does the exact same thing!:#!/bin/bash function disk_function(){ diskspace="df" ds_arg="-h" printf "Gatering diskspace information %s %s command\n" "$diskspace" "$ds_arg" $diskspace $ds_arg } function system_services(){ services="chkconfig" services_arg="--list" printf "Showing Running Services %s %s command\n" "$services" "$services_arg" $services $services_arg } function main(){ disk_function system_services } main
I think you're trying to run to the results.
You may have read a lot but it takes a bit of practice as well.
However I won't discourage from learning Python first if you find it easier, *shrugg* personal preferences.
Not to mention Python is certainly geared toward teaching new programmers, from the documentation to the volume of ground 0 material !
Who knows, with some Python experience under your belt you may pick up Bash scripting faster!
However let me also add this:
Bash Scripting is no more challenging than chaining commands together with the added features of
-Storing variables
-Conditional branching
-and looping!
As DevilWAH mentioned, break each feature down even further and work at one bit at a time.
Sometimes it really does boil down to trial and error.
Which is how a lot of people learn .
And always don't be afraid to ask for help when your stuck, just be prepared to communicate what you've attempted so far! -
ally_uk Member Posts: 1,145 ■■■■□□□□□□Thank you for your replies I am sticking to BASH. Think the problem was trying to jump the gun without having the basics in place.
I have gathered some resources
below is a beginners bash scripting course
Shell Scripting Tutorial-1: Introduction - YouTube
I am also slowly making my way through classic Shell scripting. I have tried reading other O'Reilly books in the past but this some seems different I can actually understand and make sense of the information
I am pretty comfortable using the command line but there are some tools I haven't had experience of so thought it wouldn't hurt to watch a few basic level videos on Sed, Awk, Grep, Gawk
I will keep this thread open and post my progress and examples of code figured it may help somebody else who is new to scripting.Microsoft's strategy to conquer the I.T industry
" Embrace, evolve, extinguish " -
kj0 Member Posts: 767I've learnt BASH by working on existing scripts. We have a joiner for our macs that majority runs through BASH scripts, when I first started using BASH I took these long scripts and started reading through them and making changes here and there, then I started to thing of other cool things that would be useful, or parts of those scripts would be useful for - so I would then apply them somewhere else and then research how to get the next section to work.
I can't sit and read books while I am at work, so to make myself learn and look busy was to work through what I had available. I learnt heaps, and it is how I learnt VBS many years ago, and just last year getting started on C#.
Since getting into it, I've read through a couple of books (not completely as I have more specific studies to attend to) enough to do what I need to though.
Keep at it, and you will get it. -
lsud00d Member Posts: 1,571Grep, awk, and sed should be your best friends. Also add in cut, and you'll be able to parse/format/transform text any way you want. This is how I became a log ninja since a lot of my scripts were triggered by log events.
-
LinuxNerd Member Posts: 83 ■■□□□□□□□□I think what turns a lot of people off about Bash scripting are the horrid examples of "code" that show up searching off Google or Bing.
Some folks have 5-10 lines of code that can be done in 2 lines. I love Bash. -
elderkai Member Posts: 279Getting used to any flavor of shell scripting is probably easier if you spend most of your computing time within a shell.
After awhile you just find things that would be much faster if automated and already know a lot of the shell's tools. -
NovaHax Member Posts: 502 ■■■■□□□□□□You must be a Linux God I came from a non Linux background and am not a programmer /scripting genius
Where do I struggle with Bash at the moment? everything
A little bit late to this conversation, but if you feel more comfortable in a Windows environment, perhaps you should start with PowerShell. The ISE (Integrated Scripting Environment) is extremely powerful and makes learning it simple.
Plus, there is a lot of common ground between PowerShell and Bash. If you master powershell (...or even just become proficient), Bash will be easy to pick up.
Hang in there man...the first language is always the hardest. -
philz1982 Member Posts: 978Completely off topic, but I took some classes here and it really helped me grasp programming. I took the opposite direction and started with High-Level languages and now find myself learning scripting and assembly...
Computer Programming Course Schedule | UCSC Extension Silicon ValleyRead my blog @ www.buildingautomationmonthly.com
Connect with me on LinkedIn @ https://www.linkedin.com/in/phillipzito