Day 19 - edits

This commit is contained in:
michaelcade 2022-01-17 13:20:54 +00:00
parent 2bdffacd4d
commit 2de87da020

View File

@ -1,20 +1,18 @@
## Automate tasks with bash scripts
We could almost dedicate a whole section of 7 days to bash scripting much like the programing languages because bash gives us the capability of working along side other automation tools to get things done.
We could almost dedicate a whole section of 7 days to bash scripting much like the programming languages, bash gives us the capability of working alongside other automation tools to get things done.
I still speak to a lot of people where they have setup some complex shell scripts to make something happen and they rely on this script for some of the most important things in the business, I am not saying we need to understand shell / bash scripting for this purpose, this is not the way. But we should learn shell / bash scripting to work alongside out automation tools and for ad-hoc tasks.
I still speak to a lot of people where they have set up some complex shell scripts to make something happen and they rely on this script for some of the most important things in the business, I am not saying we need to understand shell/bash scripting for this purpose, this is not the way. But we should learn shell/bash scripting to work alongside our automation tools and for ad-hoc tasks.
An example of this that we have used in this section could be the VAGRANTFILE we used to create our VM, we could wrap this into a simple bash script that deleted and renewed this every Monday morning so that we have a fresh copy of our Linux VM every Monday morning, we could also add all the software stack that we need on said Linux machine and so on all through this one bash script.
An example of this that we have used in this section could be the VAGRANTFILE we used to create our VM, we could wrap this into a simple bash script that deleted and renewed this every Monday morning so that we have a fresh copy of our Linux VM every week, we could also add all the software stack that we need on said Linux machine and so on all through this one bash script.
I think another thing I am at least hearing is that hands-on scripting questions are becoming more and more apparent in all lines of interviews.
### Getting started
As with a lot of things we are covering in this whole 90 days, the only real way to learn is through doing. Hands on experience is going to soak all of this into your muscle memory.
As with a lot of things we are covering in this whole 90 days, the only real way to learn is through doing. Hands-on experience is going to help soak all of this into your muscle memory.
I am also going to add some specific resources at the end of this section.
First of all we are going to need a text editor. On [Day 17](Day17.md) we covered probably the two most common text editors and a little on how to use them.
First of all, we are going to need a text editor. On [Day 17](Day17.md) we covered probably the two most common text editors and a little on how to use them.
Let's get straight into it and create our first shell script.
@ -24,20 +22,20 @@ Followed by `nano 90DaysOfDevOps.sh` this will open our new blank shell script i
The first line of all bash scripts will need to look something like this `#!/usr/bin/bash` this is the path to your bash binary.
You should however check this in the terminal by running `/usr/bin/bash` if you are not using Ubuntu then you might also try `whereis bash` from the terminal.
You should however check this in the terminal by running `which bash` if you are not using Ubuntu then you might also try `whereis bash` from the terminal.
However you may see other paths possibly listed in already created shell scripts which could include:
However, you may see other paths listed in already created shell scripts which could include:
- `#!/bin/bash`
- `#!/usr/bin/env bash`
The next line in our script, I like to add a comment and add the purpose of the script or at least some information about me. we can do this by using the `#` we can use this throughout if you want to document comments throughout the whole script. I find more notes better for the user experience especially if you are sharing this.
In the next line in our script, I like to add a comment and add the purpose of the script or at least some information about me. You can do this by using the `#` This allows us to comment out particular lines in our code and provide descriptions for what the upcoming commands will be doing. I find the more notes the better for the user experience especially if you are sharing this.
I sometimes use that figlet program we installed in this Linux section to create some asci art to kick things off in our scripts.
I sometimes use figlet, a program we installed earlier in the Linux section to create some asci art to kick things off in our scripts.
![](Images/Day19_Linux1.png)
Now we want to get something done. All of the commands we have been through earlier in this Linux section ([Day15](Day15.md)) could be used here as a simple command to test our script.
All of the commands we have been through earlier in this Linux section ([Day15](Day15.md)) could be used here as a simple command to test our script.
Let's add a simple block of code to our script.
@ -51,11 +49,11 @@ You can then save this and exit your text editor, if we run our script with `./9
![](Images/Day19_Linux2.png)
We can change this using `chmod +x 90DaysOfDevOps.sh` and then you will see the x meaning we can now execute our script.
We can change this using `chmod +x 90DaysOfDevOps.sh` and then you will see the `x` meaning we can now execute our script.
![](Images/Day19_Linux3.png)
Now we can run our script again using `./90DaysOfDevOps.sh` and we can see that from that we have created our new directory, changed into that directory and then created a new file.
Now we can run our script again using `./90DaysOfDevOps.sh` after running the script has now created a new directory, changed into that directory and then created a new file.
![](Images/Day19_Linux4.png)
@ -65,21 +63,21 @@ Pretty basic stuff but you can start to see hopefully how this could be used to
- ### Variables
We can use variables in our script which when we are dealing with long scripts or complex scripts we might have repeated terms that could change over time.
Variables enable us to define once a particular repeated term that is used throughout a potentially complex script.
To add a variable you simply add it like this to a clean line in your script.
`challenge="90DaysOfDevOps"`
this way wherever we use challenge in our code if we change the variable it will be reflected throughout.
This way when and where we use `$challenge` in our code, if we change the variable it will be reflected throughout.
![](Images/Day19_Linux5.png)
If we now run our `sh` script you will see the print out that we added to our script.
If we now run our `sh` script you will see the printout that was added to our script.
![](Images/Day19_Linux5.png)
We can also ask for user input to set our variables using
We can also ask for user input that can set our variables using the following:
```
echo "Enter your name"
@ -88,10 +86,9 @@ read name
This would then define the input as the variable `$name` We could then use this later on.
- ### Conditionals
Maybe we want to find out who we have on our challenge and we want to find out how many days our users have completed in the challenge we can define this using `if` `if-else` `else-if` this is what we have defined below in our script.
Maybe we want to find out who we have on our challenge and how many days they have completed, we can define this using `if` `if-else` `else-if` conditionals, this is what we have defined below in our script.
```
#!/bin/bash
@ -136,7 +133,7 @@ You can also see from the above that we are running some comparisons or checking
- `lt` - if the first value is less than the second value will return TRUE
- `le` - if the first value is less than or equal to the second value will return TRUE
We might also use bash scripting to determine infomation about files and folders, this is known as file conditions.
We might also use bash scripting to determine information about files and folders, this is known as file conditions.
- `-d file` True if the file is a directory
- `-e file` True if the file exists
@ -157,7 +154,7 @@ fi
![](Images/Day19_Linux7.png)
Providing we have that file still in our directory we should get the comment back. But if we remove that file then we should get the other comment.
Providing we have that file still in our directory we should get the first echo command back. But if we remove that file then we should get the second echo command.
![](Images/Day19_Linux8.png)
@ -167,7 +164,7 @@ I found this amazing repository on GitHub that has what seems to be an endless a
Think about any repeatable tasks that you do every day or week or month and how could you better automate that, first option is likely going to be using a bash script before moving into more complex territory.
I personally don't feel like I have maybe shown the power of bash scripting here in particular from a DevOps point of view so I am going to be looking for examples that will allow us to better create a script that does something or automates something for us.
I don't feel like I have maybe shown the power of bash scripting here in particular from a DevOps point of view so I am going to be looking for examples that will allow us to better create a script that does something or automates something for us.
## Resources