Linux Bash Shell Scripting with Real Life Examples | Imran Afzal | Skillshare

Linux Bash Shell Scripting with Real Life Examples

Imran Afzal, Systems Manager / Instructor

Linux Bash Shell Scripting with Real Life Examples

Imran Afzal, Systems Manager / Instructor

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
70 Lessons (11h 36m)
    • 1. Syllabus Overview

    • 2. Welcome to Lab Setup

    • 3. What is VirtualBox

    • 4. Download and Install VirtualBox

    • 5. Creating First Virtual Machine

    • 6. Linux CentOS Installation

    • 7. Welcome to Shell Scripting Basics

    • 8. What is Kernel

    • 9. What is Shell

    • 10. What is Shell Scripting

    • 11. Type of Shells

    • 12. Starting a Shell

    • 13. How to Run a Shell Script

    • 14. Welcome to Scripting Standards

    • 15. Scripts Naming Convention

    • 16. Script File Permissions

    • 17. Shell Script Format

    • 18. Sequence of Script Execution

    • 19. Welcome to Basic Shell Scripting

    • 20. Create your first script Hello World

    • 21. Run Basic Tasks Script

    • 22. Run Basic Administration Tasks

    • 23. Defining Variables

    • 24. InputOutput Script

    • 25. If then Scripts

    • 26. Case statement

    • 27. For loop Scripts

    • 28. Do while Scripts

    • 29. Exit Status

    • 30. Welcome to Real Life Scripting

    • 31. Accessing data from a file

    • 32. Check Remote Servers Connectivity

    • 33. Script Scheduling and Notification

    • 34. Script to Delete Old Files

    • 35. Backup Filesystem

    • 36. For loops Scripts for File System 1

    • 37. For loops Scripts for File System 2

    • 38. Copy Files to Remote Hosts

    • 39. User Directory Assignment

    • 40. List of Users Logged in by Date

    • 41. Script for Central Logging (rsyslog)

    • 42. Script for User Account Management

    • 43. Disable Inactive Users

    • 44. Check Process Status and Killing it

    • 45. Disk Space Status

    • 46. Status on Total Number of Files

    • 47. Create System Inventory

    • 48. Getting Online Help

    • 49. Congrats

    • 50. Welcome to Bonus Section

    • 51. Welcome to Bonus Section

    • 52. Important Things to remember

    • 53. Linux File Type

    • 54. Wildcards

    • 55. Soft and Hard Link

    • 56. File Ownership Commands

    • 57. TAB and Up Arrow

    • 58. Combining and Splitting Files

    • 59. Linux File Editor vi

    • 60. Sed Command

    • 61. System Utility Commands

    • 62. System Log Monitoring

    • 63. Terminal Commands

    • 64. Aliases

    • 65. NIC Bonding

    • 66. File Transfer

    • 67. RAID

    • 68. Facts About IT

    • 69. Difference Between Linux 5, 6 and 7

    • 70. Securing Linux Machine OS Hardening

12 students are watching this class
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

This course is design for those who have some experience in Linux and want to learn or refine their Linux shell scripting skills.  Most of the scripts practiced in this training are based on real life examples.

For the hobbyists this is fun to do, for the aspiring Linux system admins these are essential skills and for the experienced Linux engineers these are skills to be consolidated

In this training we will cover the following:

  • Section 1 – (Lab Setup)

    ·         What is Virtual Box?

    ·         Downloading and installing Virtual Box

    ·         Creating first virtual machine

    ·         Linux installation


    Section: 2 – (Shell Scripting Basics)

    ·         What is Kernel?

    ·         What is a Shell?

    ·         Shell Scripting?

    ·         Type of Shells

    ·         Starting a Shell

    ·         How to Run a Shell Script


    Section: 3 – (Scripting Standards)

    ·         Naming Convention

    ·         Script File Permissions

    ·         Shell Script Format

    ·         Sequence of Script Execution


    Section: 4 – (Basic Shell Scripts)

    ·         Creating 1st script

    ·         Script to run basic tasks

    ·         Defining variable

    ·         Input/output script

    ·         Conditions/If else statement scripts

    ·         Case statement scripts

    ·         For loop scripts

    ·         Do-while scripts

    ·         Exist Status

    ·         Handouts


    Section: 5 – (Real Life Scripts)

    ·         Accessing data from files

    ·         Check remote servers connectivity

    ·         Script for scheduling and notification

    ·         Script to delete old files

    ·         Backup filesystem

    ·         For loops scripts for file system – 1

    ·         For loops scripts for file system – 2

    ·         Copy files to remote hosts

    ·         User directory assignment

    ·         List of users logged in by date

    ·         Central logging (rsyslog) script

    ·         Script for user account management

    ·         Disable inactive users

    ·         Check process status and killing it

    ·         Status on total number of files

    ·         Script to create system inventory

    ·         Getting online help

    ·         Homework

  • Bonus – (Extras)

    ·         This section has so many extra lessons on Linux

Meet Your Teacher

Teacher Profile Image

Imran Afzal

Systems Manager / Instructor




Hello, I'm Imran Afzal and here is my education and experience:



About Me:

Imran Afzal



Bachelors in Computer Information Systems (Baruch College, City University of New York)

Master of Business Administration (New York Institute of Technolgy)



- Over 20 Years of IT Infrastructure experience

- 7 years of training experience in Linux, VMWare, Windows and many other IT technologies

- 5 years of IT Infrastructure management experience



- Linux Systems Management (New York University, NY)

- UNIX Operating Systems

- Linux System Administration and System Internals<... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.



1. Syllabus Overview: Hi, everyone. Let's go. Our our celeb us. What exactly are we going to cover in this class? I have actually divided my class into five different sections. Section one, which is the lab set up in this section. We will cover what is award for machine. How does it work? Downloading and installing award jewel box creating first worship machine and doing the linens installation. Now, please. Remember, this section is for those who actually have never installed or who do want to install a lab linens machine in their laptop or their work station if you already have Ah, Lennox machine. And you wanted to start practicing your land exc scripting skills from this course. Then you could simply skip through this entire section and go right into Section two in section two will start off with shell scripting basics. And what those basics are starting with Colonel. How Colonel interact with shell. What is a shelter shell scripting How the shell scripting Come along. How can we take advantage off it? What are the different type of shells? Of course, there are tons of different shells. Eash Okay, Shal see, show bashful. What is our focus in this limp in this up in this class. And of course, our focus is bash scripting. But I will cover other shells as well. Starting a shell, how to run a shell script, then moving on to section three we have in section three. We will cover the scripting standards. What exactly the standards that you should follow when you are riding a shells. And those are how to name yourself shell scripts. In this way, you would have proper naming convention for your shells, script file permissions. So when you create a script, you would need a Pacific execute herbal permissions and what those permissions are, and how can we assign them, then shell scripting format. What goes inside of a script? What are the comments? What is, um where do you define the shell? What are the variables that sequence off shell script? Then we'll wing onto the section four, which is very going to start off his actual writing scrips, the basic shell scripts. And I'm not gonna overload you on this section with, like, huge script. So I'm gonna start. I'm gonna take baby steps with you as if you are starting fresh with me and Lennox Shell scripting. Then I'll show you how to create your first group script to run basic task in those task. Our system. Administrative tasks. Defining variables, input output, script, conditional scripts. If else statements groups will cover the case statements, groups look over. Four loose skirts will write it. I'll give you examples will create do wild scripts. How do they work? What are the exits Status? How can be used the exit status and shell scripting and make use off it. And then we have a bunch of handouts, as you could read on shell scripting that will help you write war script and understand scripting details. Six Scripting basics as well moving on to the Section five, which is our biggest section, and you should be ready for this section. By the time you come to this, you would know exactly how to start, how to get your hands dirty, and you are already on a driving seat. At this point, this is where we have. He will core their real life scripting examples, and some off them are accessing data from falls. Check remote server connective ity script for scheduling and notification script to delete all files backup file systems when you wanted to back up your system and you don't have a 1/3 party backup tool. How you could use a scripting language to do that? Four Loops Scripts for Fire System That is, for one of the section and I have not included just 14 loop This lesson. This Pacific lesson would have like four or five four loop scripts that I have covered in this one section. So in this one lesson, then four Loops group for file system to now. This is another lesson. I have split that entire lesson into two lessons. Why? Because I'm covering like 5 to 8 different scripts moving on copy files to remote host user directory assignment. You will see how we could check if if a directory exists and doesn't belong to a user. This is a security risk. How we could put a script around in real life examples. A game moving on to Section five. Section five is not finish it. List of user logged in by date Central Logging script. If you do not have Splunk, which is a Logan logging third party tool in your environment, it's OK. We'll create our own Splunk. Our own logging system locks script that will run like a Splunk collection logs script for user CAL management Disabled in active users. That's a scribble core check process status and killing it. Status and total number of files. Scripts to create system inventory, getting online help, homework. And there's gonna be so much, many other scripts that I cannot just add everything here in the syllabus that I will be keep adding it later on. So this is the This is what be actually going to cover in this entire course. So are you ready with me? Are you ready to go ahead and start? Let's go. 2. Welcome to Lab Setup: All right, guys, welcome to the lab set up in this section. We are going to actually set up our lab. Ah, where we will be practicing our scripting skills. We will be building. Ah, are Lennox machine and we will be creating shell scripts. So let's see, What exactly are we going to do in this lab? Set up? So this is Ah, quick, step by step, the instruction of what we're gonna be doing. Ah, following this lesson in this section, I will be covering about the virtual environment. This is for those who actually don't know what water environment is or what a virtual machine is. So I will be going over some of the fundamentals of virtual environment. How it works. Ah. Then what is, um, Oracle Virtual box? So, of course, inversion environment. There are so many. Ah, were actual products that out there, there's VM where the orca wardrobe box. There's Microsoft Works environment. So which one to pick So in our let lab said of you will be installing Oracle warship box, and that's where we will be setting up our next lab environment. So after that, we will be installing Oracle War troll box on it. Um, then it's Ah, it's a complete process. I have, actually, including every single thing in this set up. So where do you need to get the download file? How do you have to install it? How do you have to configure it? And then we will cover the first virtual machine. So once you have the oracle box running, then what do you need to do with it? You need to create a virtual machine. What is a word from machine? If you don't know, then I have covered that in this section. You don't have to worry about a thing. I will walk you through step by step and every all the way through on setting up your Lennox machine. And then once you have the Lennox machine up and ready, then we will do the Lennox installation. And, um, the Lennox insulation I will be covering is the centre west, which is exactly like Red Hat Lennox, which is used mostly in the linen environment in a corporate environment. So this section covers all about setting up your lab. Now, let me tell you this. If you already have a lab or if you already have. Ah, Lennox machine. Then you do not have to do this entire section. You could simply skip through it. You could start up your own Lennox machine that you have. It's either a bun, too. It's either sent OS or red hat. I would put forward that you go with center Wes and, um, and red hat because most of the scripting languages and the shell scripts that I will be using I was using in bash language. If you're lit Knicks opening system understands the bash programming language. That's fine. You could you could use that. Um, if you want to install Lennox from scratch on your physical machine, you could also go. You are. You're more than welcome to do that. But if you do not have any limits machine than this section is really gonna help you. And once again, if you do have a Lennox machine not to worry about it, just go straight to the next section. 3. What is VirtualBox: what is Oracle Worship Box? And, of course, why do we need to know about worship box? Well, for this course, you do need to install sent OS Linux operating system on your computer, and there are two ways you could insult. One. You could remove the existing operating system that you have on your computer. Or you could install virtual box on top off your existing Windows operating system or Mac, and then you could install sent Oso you This way you won't lose anything. So basically, a virtual box is a free and open source hyper wiser for X 86 computers currently being developed by Oracle Corporation. It insults on your existing Intel array, MD based computers, whether they are running Windows Macklin excess LHarris. It extends the capabilities off your existing computer so that it can run multiple operating systems on one hardware at the same time. Let's see how it works. So you have when the hardware layer and then on top off it, you install the operating system. Of course, it could be anyone. You have that as Windows or Mac, and then you install Oracle Virtual Box. It's just a A program, a software that you installed on your machine like any other software that you would install. Once you have installed Oracle warship box, then you could install the Lennox on it as a separate small version off operating system. You could have windows on it, or you could have Mac on it, said this one. This virtual box allows you to have multiple operating system on the same hardware. 4. Download and Install VirtualBox: in this lesson, we will cover the Oracle Horschel Box download, and it's It's insulation. First of all, you would have to go to Google and type Oracle Virtual Box download and then the first link you will see here go to download. If your operating system is Windows, you'll click on Windows Host. Once you click on, it will ask you to save the file. I can't save the file, but since it takes some time, I have already saved the file previously to save some time. This is the file execute herbal file that's been saved. Run, click. Next, Leave everything default setting. Create a start menu entry You. Could you believe that? Create a shortcut on your desktop? That's fine. Create a shortcut in a quick lounge bore. I don't like that and Register File Association. You believe that networking installing the Oracle Worship box and network feature will reset your network adapter connection temporarily. That's fine. Proceed 10 and stole. Do you want to allow the following program to run since installed software years? Would you like to install this device software from Oracle? Yes, if you notice quickly. This created a quick desktop shortcut on your desktop. It is trying to install another device software That's fine. Click Install. Just going to say, Always trust anything that's coming from Oracle Corporation and just click on install. So this week it's not gonna bother me again. And there you go, Oracle be impartial. Box installation is complete. Click start Oracle of Urschel box after the insulation. Yes, you could leave the check box cook finish and is Cancel this man Dairy go! That's the first welcome page off the wardrobe box. And that's where we have actually completed the installation off the warship box from a hair on you could build, create a new wardrobe machine. And once we create a new washing machine, we will install Lennox Santo s operating system on that washing machine. 5. Creating First Virtual Machine: creating virtual machine. In this lesson, we will create our first social machine. And on victory will be installing are sent OS clinics operating system. But worker machine is basically nothing but a shell. Ah, container, which holds the information off a machine CPU memory disc and all the resource is this fortune machine is gonna be sharing. The resource is wherever it is installed. So let's say if it is installed on my laptop, it will be sharing. The resource is from a laptop, the Oracle work show box. I'm gonna open it up. Okay, so this is the window for the Oracle Horschel box. I'm gonna click on new. The name I will pick is my first Lennox, the M. And if you notice, um, the virtual box is smart enough to OPEC, the operatives, his assistant type and version for you. You probably noticed that you are going to see the 64 bit. You are probably seeing 32 big. Now, if you are seeing a 32 bit, then either your computer is 32 bit architecture or the virtualization technology is not enabled. First, let's find out if my computer is 64 or not. So let's go to start menu computer. I'm gonna click right click and go to properties. Once the property comes up, you will see here it will tell you if it's Win seven PC and right here you're gonna see or is this system type? It's a 64 bit operating system. So that's one way. That's one of the reason that when I'm building my worship machine, it is coming up 64 bed operating system. The second reason you might not gonna see it 64 is if you don't have virtual technology enabled in your bias sitting. If that's the case, I have actually pasted an article and in my in this lesson and there is a link to it, I would recommend that you could go that link and try the settings that are recommended and see if it changes the 64 setting. I'm sure it will work. If not, you will always email me back. So I'm gonna leave thes setting sugar click next Now here is gonna ask me the size off the memory I want to assign to this virtual machine. Now, by default is picking up one gig off memory now, you should also keep in mind How much memory do you have actually in your laptop? So in my laptop, I have Let's look at my computer again, Right? Click properties, and you will see here. My computer has installed memory is four gig. So it is. It is wise that I keep the memory below that digit. That number. So I have four gig, so I'm gonna give one gig up that memory to dis virtual machine. I'm click next, create a new Horschel desk. Yes. It will create a brand new Horschel disk. Next, leave the default dynamically allocated. Lee the default post. Um then this is the size of the disc. By default, it picks a gig. I wanted to pick 10 gig just to be safe. So I'm gonna just click inside this box, remove and type 10 and click create. And there you go. This is my first worship machine that is created again. Think off it as a piece of hardware that's been created for me. I'm on the assembly line. I'm putting the CPU and putting the memory and putting the hardest. I'm putting everything in that machine and I built a machine. So once the machine is built, what is the next step? Next step is to install the operating system, and in our course, we will be working with Santo s up a ring system. So I'm gonna power this on, and I'm gonna install the operating system. But of course, before I go ahead and install the offering system, I do need the software ISO image. So in the next lecture, we will cover how we're going to download the IRS. So, software, and then how you could attach that software to this Lennox of'em, and then we will boot off off that software. 6. Linux CentOS Installation: Hello and welcome to sent OS installation. This is the fun part. Yes, this is where we going to get our hands dirty. We gonna go ahead and download the center Will s I s O image file from the from the Internet. Once we have it downloaded, then we will go through the installation. A complete installation off sent OS Lennox operating system. So you ready? So now I want you to go ahead and open up your browser. Any browser that you prefer, whether it's ie, Firefox or chrome. So let's go in. I have my browser open already. Um, I will go into Google now. The second step is you could use Google or any search engine that you like and I want you to type sent os download and hit Enter. Once you hit enter, you will get the first length I want you go and click on it. Or you could simply go and click on download. But first, let's go to this first link. So in the first slink or the first page, you will see download Santo s Now, Sometimes you will see three options in this page. One is a DVD. I s o second one is everything DVD I eso and 3rd 1 is minimal. I s o If you are seeing three or if you're seeing too right now, it doesn't matter because what we need is DVD. I s so this is the one that we need to click on. So go ahead and click on DVD. I So this will bring you to the links where the eyes are. Images are saved. So all those links are actually the links to the servers or they're often time referred to as mirrors. These mirrors, when you click on it, will take you to the file where the file location is. So go ahead and a pick Anyone off your choice over recommend that you pick the 1st 1 So go ahead and click on the 1st 1 Once you click on the 1st 1 it will ask you or prompt you to save the file. You have to save the file. Ah, to you to the locations so going. Click. Okay, if you do not know where the file is being saved, you see this little arrow here? If you are using the, uh, fire walks, he will see this little arrow here. Click on it and it will tell you that it is being downloaded. Right now. I do not want to download because I have already downloaded that file previously because I don't want to waste time, your time and my time to go through the company download. So let's minimize this page. Let's go to a desktop because all my, um, downloads go into my desktop now. You already have this center west. Okay, Now, I want you to go ahead and click on a double click on your Oracle War Troll box. The icon you have on your desktop. If you do not have that icon, your desktop, then you go to start menu and find that up and you will go ahead and start from here. When it opens up, you will see the my first Lennox for Lennox PM. The one recreated earlier. These two You're probably not going to see it because I was playing around with some windows. Were for machine anyway, it's just you could just ignore them. Um, click on the first, my first Linux VM and click on start. Now, this is powering up yours from machine. This is exactly the same way as if you powering on your physical machine, but pressing the button on it. So now it is asking, since this washing machine has no operating system, so by default is asking you to attach award actual disk to it. So it is asking where is the location off that were actual disc, which you want to install that operating system. So go ahead and click on this little icon which will browse us to our desktop where we have our eyes. So image safe. Click on this one, and you will see right here on our desktop folder. When you click on our desktop, you come down and you will see right here say, Santo s seven, which we have downloaded Ah, few minutes ago. So go ahead and click on open and click on start. Ah, by the way, this top thing right here is telling you have option to capture key ward option. Turn on. This will cause the upper virtual machine arm of the capture. You could just go ahead and cancel it. Now it's telling you at this green is selling you. This is a cent or seven installed center or seven. Test this media and installed center a seven, or do the troubleshooting right now by default. Test this media and install center. A seven is selected. You do not want to test this media. Usually this option is picked when you have a DVD install and the reason we do the test because sometimes the DVD have scratches on it. That's why we wanted to go through the test. So since this is I saw image. We don't do that. So go ahead and hit up arrow key on your keyboard and it will select or highlight the top one. It's State installed Cento a seven and now go ahead and hit. Enter It is actually bringing the eyes. So image copying the files that are needed to actually install the operating system so it will go through certain start up processes. It will start up the gui that will allow us to go through the installation off the lyrics Center s are bring system. We will give it a minute for the going to come up. See right here says starting installer one moment and installer. The Gui itself is called Anaconda. This is a name they have given Why? I don't know, But this is the name If you want to keep in mind Um you just remember that anaconda is the Gui tool or degree Viz erred. That will help you to install the Lennox offering system. Now you are under welcome to Cento a seven. Ah, welcome page. What language would you like to use during the installation? Ah, by default English and I'm in the United States. I'll pick English United States if you are in any other country. Ah, we recommend is you pick that country or if you want to pick a different language, you could pick that. However, I am not going to tell you how you could go through the installation with different language anyway, So I support for that. You stay on English and then you definitely you could pick a different country. Now go ahead and click on. Continue at the bottom. Right. Okay, so now it says, please complete items mark with this action. This icon before continue to the next step. Now the date and time is the 1st 1 we have already pre selected America's New York Time zone. If you wanted to change the time zone, depending on which times of your and go ahead and click on it and change the time zone keyboard by default. It selects English and U S um language support a gain. English United States installation Source. Local media, Which means that we are installing the operating system through the local Aiso image, so it's already been selected. Perfect. Next stop software selection. This is where it has by default the software selection as minimal install. If you leave that as is minimal installed, then it will install the lyrics operating system, but it will not have any gooey on it. Well, remember one thing most of the time. Incorporate Marmon. There are no gooey on Lennox, but since we are going through some practice, we will go through some GUI practices as well, because I do want you to know how Gui works in Lenox as well. So we'll go ahead and click on software selection, and that's where we will pick um, a couple of different additional software that are required in the middle installed. Ah, we have other options as well. If you want to use system administration tools are any other tools you could pick them. We could leave as default. Compute nodes, anything. Any debugging tools, directory clients, hardware monitoring tools, development tools. Most of these tools are used by developers. Since we are not going through the development training, we we will leave that. As is infrastructure server. Ah. What type off server are we going to run on this machine? I will tell you you could pick, um, anyone that you think you might need it later on. Um, you could pick the d n a server for now because we are going to core DNS, which is bind in this training so you could pick DNS if you do not pick Deanna Sarah this time, there is no issue. We could always insult the in a server or package later on, but anyway, just to show you how it works, I just selected one DNS name server. Anything else that you want to select here? You could. If you don't want to just follow my lead, then let's go to the next one says file and print server. You could leave everything as is a said basic Web server. You don't need it. Work authorization hosts and no several with Cool. Yes. This is where we have to come in. And if you notice as you are going down, all the other options that were available on the base environments are actually added here as well. So don't worry about it. They're repetitive. It's not that this option will have a different d n a server and with this based environment. So right now we are in the server with Gui Environment. I want you to go down and pick um que d e which is Katie easa. Plasma workspace are highly conferred with graphical user interface. I also want you to pick Java platform because if you are running certain application that requires Yaba, we could bring that up. Um, that see what we have? You have all the virtualization tool. Um, okay, I think we are good hair, but the Katie eat now The next one is genome desktop now Genome desktop are the ah, the desktop that also per wide some type of applications. Um, for the user, let's say if you want to use Firefox or if you want to use, um calculators or something on the gooey side, then you could select this. That stop as well. So go ahead and click on genome applications. Um, any other tools we need Internet application, email, chat, video conferencing software. We don't want that because most likely incorporate environment. You will not be using any of thes Katie plasma workstation workspaces development and creative workstation, and that's pretty much it. You don't really need any other applications or add ons for the selective environment. Now go ahead and click. Done. Now you will see this option right here. It will tell you checking software dependency. It means it is going toe our ISA image and asking that isa image. Hey, this user has selected thes these many packages. Do you have these packages for me to install on this harboring system is going to say, Hey, okay, no problem. Let me go ahead and check that. So it's checking the source, and it's checking the software dependency as well. So what is needed for those add ons that we need to install installation destination while it's running will go to the next one. It's his installation destination now. How do we want to protection our disk and solution destination right now? Says automatic partitioning selected If you want to change that? Meaning Right now, we have 10 gig. This is the discus size that we selected when we were creating the worship machine. This this size has shown up here is telling us we have 10 gig of disk space. How do you want to partition it? We're telling it automatically configure the partitioning if you know, or if your organization has a requirement to partition a disk into different slices or different partitioning than you will go ahead and click on. I will configure, Petitioning. Now, go ahead and click on. Done. Next one is K dump. Que dump is enabled If something goes wrong, K means Colonel, if there is something wrong with your system Ah, Colonel is going to write some messages, um, logs that is often referred in Lenox language as a dump that is enabled. So it's always a good practice to leave that enabled. So this way, if you are troubleshooting with your vendor, let's say if you have cento us red hat involved, they could go at and take a look at your logs. Next one we have is networking and host name. Okay, you We are on a network and host name configuration. At the bottom, you'll see Host name. Go ahead and highlight what we have right now and hit, delete and type in the host name off. The Lennox operating system will type the same one as we picked for R V M my first Lennox VM and click on Apply as soon as you click on apply. You'll see the current host name was changed to the host name that you have picked. Now the second thing is to configure the even it. Now, remember, by default if your machine or the washing machine, this is your washing machine. If it is running on a PC that has one gig, often network port, it will show up here as beat one gig, and right now it is showing its wired for me because I have selected or I are my machine where I'm running this worship machine is wired, so go ahead and click on configure and by default it will show up The tab as Ethan at E E and P zero s three is the name off our network adapter. So we'll click on General and we click automatically connect to this network when it is available that arrest of this setting is blacked out by default. You could leave as default I PV before setting is the setting for automatic DCP. If you want to assign an I P, then you would have to change it to manual. But we want the I P to be assigned automatically. So if you leave it as is I'm the last one is i pp six settings. We don't use that right now, so go ahead and click on safe. So once you click on save, you will see its tells you automatically connected. It is given an I. P Already. And now you could go ahead and click on Done. Okay. And so our network and host name has been configured. Now, the last part is security profile. No profile is selected. You believe that as as unless your organization has a specific procedure to put in place for security policies, then you could define it here. But for now, you could just leave as is, and go ahead and click on begin installation. Once you click on begin installation, you will see at the bottom. It will start creating your your partitions. That were selected that you ask the system to select it for you. Now it's going to starting package installation process. While this is going through the package installation, we have to, um, uses settings to define. First is root password. So go ahead and click on root. Password is not set. And here you would have to put in the root password. Pick anything that you you will remember if you notice I picked a week Password. You should pick a strong password if you are, Um, if you're working for a company or for you or an organization, But if you are just following my lab or you're doing this everything as a tutorial, that is fine. You could pick a week password. However, if you notice at the bottom it says the password you have provided it is weak. The password fails. The directory check. It is based on a direction dictionary word which you will have to press done twice to confirm. So okay, I understand it is a weak password. In order for me to keep that password, I have to press it done twice. One to now it has taken that weak passwords, user creation Let's go ahead and create our first user. Full name. Let's put in our first name by default. Lennox takes the first name as the first letter. First initial last name is complete. Last name. So for me, the user name will be I of soul. Keep your user name shorter than 32 characters and you do not use spaces. That is fine. We are not using any spaces were wholly using six letters. Make this user administrator. You could lead that out. Requires a password to use this account. Yes, the pickup password. Again. It is weak. I understand. And I would have to do the same thing Done twice. Okay, it is done. Now if you come down and you notice right here it The operating system is installing or this Viz erred is installing packages for this offering system. Total number of packages that are required to be installed are 1397. You might have a little more or a little less, but that's okay. It depends and it changes over time. What other packages are installed? Water. The dependencies aren't still so again, If your packages do not match with my packages do not panic. You are. Okay, so now you'll see it is going through the packages installation. At this point, it is. It has installed almost 400 packages and they're still about ah 6 to 800 packages that needs to be installed. And this process will take. I would say somewhere between 10 minutes to 30 minutes, I am going to fast forward this entire process to save time. Now the packages installation has been completed. Now it is going to some post insulation set up. All right. Finally, our installation has been completed successfully. Now Santo s is now successfully installed and ready for you to use. Go head and reboot to starve using it. Click on Reboot West. Since the system comes back online, it will give you the initial set up page. The first page is about licensing. You have to accept the license. Click Accept license every hman click on done network and host name if you notice. We have already selected this network and host name configuration during our installation so that you don't need to do it again. Click done. And right here says finished configuration. Go ahead and click on finish configuration. All right. Finally, our entire Lennox installation has completed. Now we have to log in the first time, Click on your name or you could also log in as root. If if you remember, you have picked the root password, you would have to click on, not listed and type and using him crude and password. But for now, we'll go in as ourselves and we will put in a password, click on Sign in or had enter. All right, this is the desktop. This is how it looks like in Cento as it is very similar to Red Hat Cento s. You have these icons right here. You can resize them if you want to. You could delete any I conjugal create icon. Um and you could explore other things on the application and spaces. So also, one more thing when it starts up, it also gave you this welcome page. So you just leave as English. Go ahead and click on next typing. If you want to change anything you believe a default location services, I usually keep it off. If you want to leave it on, it's really up to you. Click next connect to online accounts. You don't have to do that clicked, skip, and then start using sent Os Lennox. So once again, congratulations. So now you know how to do the complete Lynn accent OAS installation? If you think that you missed something that I would recommend you to go ahead and delete this entire Lennox, we m and go ahead and create a new washing machine. And do the Lennox insulation again from scratch. The more you do, the more practice you'll do, the more you will learn. All right. Good luck. 7. Welcome to Shell Scripting Basics: welcome to the new section off shell scripting basics. That's where we actually will learn. What really a shell scripting is what are its basics? You know, starting from ABC or 123 So that's where we're going to start. So let's see, what exactly are we going to co were in this entire section? So we will start with Colonel. What, exactly? Colonel is. And how does it interacts with the shell? And is there a colonel in operating system? And how does it interact with your hardware? And how does it interact? Was your shell and your operating system? What exactly Shell there's. Of course, this is the main thing. We wanted to know how the shell scripting works. There are so many types of shells, there are corn shell there, bash. There are, um, seashell and so many that I will cover in this lesson. Then we will have starting to show how you gonna start a shell if you are in logged into into your links machine and by default, you have a bash shell and you want to say sure, definite show. How do you do that, then, Wilko? Or how you gonna run a shell script. So you have created a script. Now how do you run it? So this is a few things Very basic ones that you should really know. And this is like a fundamental things that you should understand and put it somewhere you know, deep in your head that outside of the thes are like a actual guidelines that you have to follow in order to run. Ah, perfect shell script. So you would know when if there is an error message or if there's a problem when you're running a shell script, why did it happen? This section will explain you if you really go through it. 8. What is Kernel: the next Colonel. Do you know there is a kernel in every Lennox distribution or UNIX distribution? What, exactly a colonel is? It actually interfaces between your hardware and your software. So let's look at this diagram. Here, you will see you have at the bottom is the hardware and what others? Hardware hardware is like CPU memory, hard disk and any, um, outside hardware peripheral devices. Then you have Colonel. A kernel is a program that is stored inside off your operating system. It's like a program, a command that keeps on running. And what it does is it takes the commands from shell and shell. What is a shell shell? Is anything like a gooey Davey having windows or a gui in Lennox? Or if you don't have going Lennox, we have terminal that runs bash shell or sea shell, which will cover later on. So when we are in that bash of seashell terminal environment, the execute commands. The commands are being forwarded to a colonel, and it's kernels responsibility to talk to the hardware that's inside off your computer, and then you on top of that, you have a browser, send mail, all the application that we're only calculator, calendar office or any limits related applications. And, of course, on top of that is are the users and that is you. So Shell and Colonel together, that's offer built together in one package, and that is called operating system. And that operating system is any operating system like Lennox, UNIX, Mac or Windows. Every operating system has that little program that actually talks to your hardware and anything that is a shell, which is written in C programming languages. Well, that shell or that those applications are actually the software. So this is what the real colonel means, and we really need a colonel in any operating system to talkto our hardware. 9. What is Shell: shelter. What is a show in this lesson will cover about Shell Hollow shell works and how you grown command within the shelf or how the environment it's set up in a show. So what exactly is a show show is like a container like you know, you have, like a top aware in your home or any kind of containers that holds your environmental information inside off it, it's interfaces between users and colonels or offering systems. So you have built an operating system, and then it is provided a platform you to execute commands again. Ghulam Windows is one of the platform and and now have been is offering system. When you start up a gooey, you execute commands by running. But double clicking on a program so that go into face is your shell. In Lenox. When you don't have a gooey, you are given a terminal show or cli command that is your show. To find your show, you could Aronica man like eco dollars, dollar and zero that would tell you what shell you are in. And you could also get a listing off all your shells in your Lennox distribution by running the command cat at sea shells. Your shell is also inside off, etc. Password that is defined inside off the file you and windows. Gooey is a shell in Lenox, Katie Eglise a shell and let S h which is shelled Bash sector. All these are the show that is provided to a user again. We're looking at this diagram again, and we are second from the top. You'll see the application that talks to the shell and then shell talks to Colonel and Colonel is the one that is telling hardware what to do. Let's look, look at our Lennox machine and we'll see how we could find our show. Open up a terminal real quick and when you do cat. So when you do echo dollar zero, it tells you you are in bash So you could look at the available shell that is installed in your distribution by doing cat at sea shells and tell you all the shows that are available . And when you look at Etc Password, cat, that seat password and you'll see in my user name, which is right here. It tells the shell is all the way at the end that I have a defined show, which has been bashed. I could change the show if I want to tow a different show, but I try to keep it consistent with other users so that what the shell does, and we'll cover the later in the later lessons how we could talk to Shell to create scripts and other things to automate our tasks. 10. What is Shell Scripting: So now let's talk about what is a shell script. So put all your instruction in a show or in a container and just running. So if I have to explain it through a diagram, let's put it this way. This is a shell, right? Like actually a shell. So you actually putting all those things All those instructions like do this, do that, or else do that it sector everything. You putting that together in a shell and then telling Shell, run this set of instructions and, of course, run them in a sequence by default, the shell runs old instruction in sequence. Now, if you notice you goto windows and you want to copy a file from one folder to another, you right click to it or control, see to it and then you go to another window and then you paste it. Now how does it work? How does Windows knows that I have been given instructions to copy it and paste it, because when you right click and give you the option to copy and then you go to under the window gives you the instruction to paste that is the function off a shell. The shell is copy instructions to copy a shell is instruction to paste. So just like that, any out the commands or any off those functions that you run and windows, they are all created within a shell within a commands. You don't see those commands. And in fact, if you wanted to really run and Windows, you, you could just go toe open up the DOS prompt and you could actually run those commands a copy command to copy file from another. But of course, Windows. Microsoft had made your life a lot easier to run all those commands through graphical interface, which is Click click, click now in Lenox. You don't have that luxury, and that's why I also Lennox is difference it because it's not gui based, kind of, um, operating system. It's a command line, so you could run your function a lot faster so you don't have to deal with gooey and go using up your CPU and memory. So what do you do? You run, copy command or you run, Move command or all those commands that you run. Those are what are those? Those are actual the scripts. Those are shell script and who have written those the developers, those people who have done development, those people will do C plus plus or C development. They create those scripts so you could use those scripts leverage on those scripts in Creature own Shell script. So that's how the shell scripting works. 11. Type of Shells: No, let's talk about the different type off the Lennox shells that are available to us in our Lennox environment. When we have a Lennox machine running, what are the different shells of he could take advantage off? The 1st 1 is genome. If you don't know what genome is, actually, genome is like a graphical environment that is in Lenox. Now. You must be thinking home isn't Lennox command line interface. It is most of the time, but there are people who actually installed Lennox with the gooey and the gooey while you were installing the gooey. Or while you're going through the installation, you have the option to install Gui as well. And the two options that you have for Gui is genome, and the 2nd 1 is K D E. Both of these are the desktop gui environment, and they are the shells. So you log into those environment you are in the show and then you run command. How you execute commands you to execute your keyboard and through your mouse you click click, click, drag drop here and there. That's how the interact would the operating system through that show. Now, of course, let's go back to our main topic, which is the command line chills. Now the command line shows the main one is shell is just s h shell. Of course, this is we are now talking about when we do not have a gooey We are in a command line. And now we need to use a show. When you use the logs in, um, a user is given a shell a platform to work. That platform is assigned to the user when when the user account is created. So as I mentioned before, if you go to Etsy past where you will see your shell So if you are let's suppose are given bash shell by default and you want to switch to S H. You could do that by going to a different shell. How do you How can you do that? We'll cover that later. So show this just s H is a born show Call. Sh R s H is one of the original in eggshells. It is developed by UNIX computers by Stephan Born. His name was actually born. That's why they picked the name born chill from he worked for AT and T Bell Labs in 1977 Those are one of the oldest days when actually let exports or UNIX was being developed. Its long history of use of means. Many software developers are familiar with it. It offers features such as input and output redirection shell, scripting with string integer variables and condition testing and looping. And so many of the things which, of course, don't worry about it. All those the heavy words will cover them later in the lessons. Then we have Bash Bash What is it now? The popularity off shell which was S h was so so much that you know it made it. It actually motivated programmers who actually developed the results show that was compatible with it but with the several and has men. They added so many things to it. Lennox system still offer shell scripts They came away with bash so they still have show. But now they came up bash Know what bash means? It means born again shell. So if the shelves still exists somewhere, don't think that shell S H is gone. Now the newer shell is available, which is called Born again shell. And in our entire course, we will be using Born Again shell. Why? Because it has a lot of enhanced features, and it is very user friendly. So maney, um, features of variables that we could use in that show. Then what out what their other shells there is. Seashell and T C. Shell developers have written large part of slings operating systems in C and C plus plus language is right, you know that using cease in Texas, a model Bill Joy, who was at Berkeley University, developed a seashell in, ah, believe 1977 or 78 then the Ken Ken Greer. I think that was his last name. Working at Mellon University took seashell concept as a step forward, and when it took several, what did he do? He came up to the New York show, which was T. C Shell and based on your system directory structure and follows. T Shell does not run bash scripts. Eso um, if you are in a t shell and you want to run bash, it's not gonna run it anyway. Most of the people don't use seashell and T shells unless you really are proficient in C and C plus plus programming language. So my advice to you, you could stay away from it. If you are a beginner not to worry about these, then the last one we have is corn shell or Keisha David Corn. OK, see, that's the name. So that's why the people came out with the shelves because they associate ID. The name, whether so the person who came up with a corn shell His name was David Corn. He developed a corn shell on dumb. It was about the time like T. C. C shell was introduced. So corn shell is compatible with shell and bash l as well. Now core Shal improves on boring shelled by adding floating point automatic job control and so many of the things I don't want to confuse you right now. Um, injure UNIX environment. Um, most of time. Corn shell is used in Solaris where, as in Lenox environment most of the time, a bash Ellis used, as I mentioned again, If you need to know the list of shells that you have installed in your system, all you have to do is go into you, log into your linens machine and type cat s sea shells, and you will see all the list of shells. So this is what I wanted to cover, and I want you to be aware these are the different type of shells. If you are creating a shell script and you need to get help on creating a shelves, let's say, if you google it, maybe you will find some off the information or some of the US ah, scripting syntax in bash and some off them in corn shell. So you have to be very careful. They're probably not gonna work on one another. That's why you need to know the difference off different shells. 12. Starting a Shell: Okay, Now we know a lot. About what other different type of shells. How do we start a shell? How do we get into different shells? Well, it's simple at is simply type shelling. Meaning, if you are inborn shell and you want to get into, um, just shell or seashell Just type S h or just type C show. So, um, without explaining you, Maurin detail Ah, log in straight to ah, my Lennox machine and I'll show you how you could switch from one shell to another. Okay, so I have this limits machine and you notice that I have the school he running in. Medics inside the gui I have this terminal so all the commands that I run from the terminal is actually a shell. And that shell is how do you find a show? You do echo dollar zero and it tells you my shell is bash. Now, if I don't want to use this command line and you see in the back I have this directory called home and I have this trashcan. So if I wanted to view the contents off home, what I do a double click. So this environment right here is. You see this environment is G. Norman Mark, remember, I told you genome is also a type of shell. So this is another shell right here, and this is command line Inside is into the show. So if I wanted to open this folder, I am telling genome, which is the desktop gooey to open up their folders a double click, and it will open up the contents off that directory. And these are all the directories I have these. This is a file I have off off all the directory. If I want to go into another director and in the file, or if I wanted to open up this file, I'll double click and D Genome Shell is interacting with Colonel an operating system and telling it to open it up. Now, what if I do not have the school environment? Right, So and not everyone is a windows. Um, not everyone. Um, I wanted to get into windows. They want to get in the command line. So what did they do with a? So if if you want to ah, view the content off my home directory, I'm sure you know the leg up output. I'm sure you know the output. Um, off pwd, which means home. I have salsa. Now, if I wanted to save you the content of my directory, I do ls minus l And you see, here is a list off all the directories did I have in my home directory. So I did the same thing as I was doing here by double clicking it. But right now, I used a command Ellis. So when I use the command, Alex, I actually am telling my bash shell to do this. L s command for me. Well, let's do clear. And it will clear up my terminal. Now. If you wanted to see of what other shell that you could, um, change too. You could just simply type S h and you hit S h. You see yourself shell prompt changes. You could also verified by running echo dollar zero and you'll see it is shell. Now, if you want to exit out of the shell to go back to your bash, you type exit. And if you want to go to, let's say seashells, so just type seashell and you are in seashell to confirm your in C Shall you do echo dollars, zero seashells. You want to get out? What do you get? How do you do? You do accent. So had That's how you get out. Now, what other shell that we learned? Let's say if you want to start a corn shell s que S h hit, Enter. Um, this is bash. Corn shall command not found. This is similar commanders you could use Regency shall know what I want to use corn show why I cannot get into corn show. Maybe because I do not have corn shell installed in my computer. And how do we verify it? Simply do cat shells. Okay, tell me, do you see corn shell in there? No, right. That's why I was not able to start that corn shell. Ah, shell, actually. So anyway, so that's how you could switch around chills. Start a new show again. This is if you want to get into a different shell environment. Now, how does it relate to shell scripting the shot scripting that you want to learn, right? That that's what you're probably thinking? Well, if you are creating a shell script, let's it in corn shell. And as you see right here I don't have corn shell. That script is not gonna run. So you need to know your environment beforehand before you spend tons of number of hours and then realize, Oh, boy, I don't You would have a corn shell and, you know, even how to install or down on the corner anyway. Eso That's about changing the shells. One of the basic things that we should know is how to run a shell script. So when you created there are different ways you could run it, you can run it through absolute path, or there's another one that's called relative Path. So absolute path is if, let's suppose, if you are standing right at the first step off a steer's and you have to pick up something on the very last year in the top, you have to actually either shoot from the first year all the way to the last year on top and grab that thing that you're trying to grab or there's another one. Another way is relative path. The relative pat is that you actually walk up to the steer and then grab that thing that you wanted to grab the same way. Um, absolute path and relative pat works. If you do not know what exactly, absolute path and relative pats ar, I would recommend you that you should, you know, try to look it up online and see what they are and how they work. And, um, you'll find tons of article there and make sure your type, Absolute Path and Lennox or absolute path and relative path in Lenox as well. So the way it works is I have my limits machine up. Let me log into my Lennox machine, so I have my script right here. It's called X Y Z Dash Screwed if you notice I am in my slash home slash I of Salt and Slash script directory. I created a directory called Scripts in My Home Directory on Guy. Create all my scripts in that directory. So if I am already in that directory, I could run the script or if I am not in my directory, let's say if I just do CD and which directory I'm in, I'm in slash route. So the script that I want to run it's just a test group that I created earlier. That's a few tight death script Name. X y Z Dash script and then you hit Enter, you'll see it says Bash the script. Name Command Not found. Why? Because the Lenox operating system is trying to find that script and slash root directory. And I do not have that script in slash root directory. So I would have to specify the absolute path to run the script. And the absolute path would be just the same way as if you are standing at the bottom of the stairs and then you have to fetch something from the top of the stairs. Then you actually through some kind of means or like a stick or something, and you could grab it. So the same way you will specify the entire path, which is slash home my user directory scripts and now the name of the script. Now when I run the script, I'll hit. Enter and you will see is just the script that is actually echo out or print Hello, world on the screen. How does this group created? I will cover that in the later lectures, but just to focus. This is how you need to run a script using absolute path. Now what if you are in a relative path, which is that you are already in home I of salt and Scripts Directory. See you CD into that directory. You do PWD and you'll see the path is the complete paths. Now. Now the script name is again when you do Alice, my cell tr I have the script X y Z script. Now when you run this group now script and hit, enter it still cannot find it. Why? Because when you are inside off a directory, you will always have to specify that you want to run the script but than this same directory. And that is the relative path. And to do that, you have to specify dot, slash and then the name off the script you could hit Tab. It will auto complete. So now you hit enter and you'll see it ran. So keep that in mind. There are two ways you could run the script one through absolute path and want to the relative path. If you are, let's say in slash home directory, and now you want to run the script. You could still specify the entire past lying home starting my user directory, or you could just simply start from the director you are in. So you are an I absolve from from this home onward, I have saw scripts and the script name hit, enter. And you see it works here too, because you you have to let the opening system know where your script exactly is located. So that's the basic about scripting how you run it. So please remember these two main points about running. 13. How to Run a Shell Script: one of the basic things that we should know is how to run a shell script. So when you created there are different ways you could run it, you can run it through absolute path, or there's another one that's called relative Path. So absolute path is if, let's suppose, if you are standing right at the first step off a steer's and you have to pick up something on the very last year in the top, you have to actually either shoot from the first year all the way to the last year on top and grab that thing that you're trying to grab or there's another one. Another way is relative path. The relative pat is that you actually walk up to the steer and then grab that thing that you wanted to grab the same way. Um, absolute path and relative pat works. If you do not know what exactly absolute path and relative pats ar, I would recommend you that you should, um, you know, try to look it up online and see what they are and how they work. And, um, you'll find tons of article there and make sure your type, absolute path and Lennox or absolute path and relative path in Lenox as well. So the way it works is I have my limits machine up. Let me log into my Lennox machine, so I have my script right here. It's called X Y Z Dash Script. If you notice I am in my slash home slash I of Salt and Slash script directory. I created a directory called Scripts in My Home Directory on Guy. Create all my scripts in that directory. So if I am already in that directory, I could run the script. Or if I am not in my directory, let's say if I just do CD and which directory I'm in, I'm in slash route. So the script that I want to run it's just a test group that I created earlier. That's a few tight, that script name X y Z Dash script, and when you hit enter, you'll see it says, Bash the script. Name Command not found. Why? Because the Lenox operating system is trying to find that script and slash root directory, and I do not have that script in slash root directory, so I would have to specify the absolute path to run the script and the absolute path would be just the same way as if you are standing at the bottom of the stairs and then you have to fetch something from the top of the stairs. Then you actually through some kind of means or like a stick or something, and you could grab it. So the same way you could specify the entire path, which is slash home my user directory scripts and now the name of the script. Now when I run the script, I'll hit. Enter and you will see is just the script that is actually echo out or print. Hello, World on screen. How does this created? I will cover that in the later lectures, but just to focus. This is how you need to run a script using absolute path. Now what if you are in a relative path, which is that you are already in home? I of Salt and Scripts directory See you CD into that directory, you do pwd and you'll see Pat is the complete paths Now, Now the script name is again when you do Alice, my cell tr I have the script X y Z script. Now, when you run this group now script and hit Enter it still cannot find it. Why? Because when you are inside off a directory, you will always have to specify that you want to run the script but than this same directory. And that is the relative path. And to do that, you have to specify dot, slash and then the name off the scrip you could hit tab the auto complete. So now you hit enter and you'll see it ran. So keep that in mind. There are two ways you can run the script one through absolute path and want to the relative path. If you are, let's say in slash home directory and now you want to run the script. You could still specify the entire past lying home starting my user directory. Or you could just simply start from the director you are in. So you are in Iove solve. From from this home onward, I have saw scripts and the script name hit enter and you see it works here, too, because you you have to let the opening system know where your script exactly is located. So that's the basic about scripting how you run it. So please remember these two main points about running the script 14. Welcome to Scripting Standards: welcome to Section four off scripting standards. What are the basic standards that you need to remember? Some off them that we will cover in this entire section is how you need to name your script . What are the naming convention that you should use? Okay. Hey, I know how. What? Naming convention to use? I don't have to worry about it. I would remember everything that I would know. Okay, if you do know that, be my guest. Skip ahead to this lesson. Double cover, But trust me down the road if you are. If you have created a script a year from the time you created script, you will forget it if you do not have in a proper naming convention. And if you do not have in a proper directory structure, so we'll cover about the naming. Convention will cover out how the script permission works. So when you create a file by default, it does not assigned executed permission. If you want to read a file, it has to have a re permission. If you want to execute it, it has to have execute permission. We'll cover that in that. In that, um, lesson. Then shell script format. How you should write a shell script. What are the formats? Um, standards that you should follow, followed by the sequence of the script. Execution. When you want to execute certain things inside off your script, you want to execute ABC. You need to know how they are executed. And the last one is the variables and how to create them. Variables is just like aliases will cover that in this in this section as well. So these are the basic things again. If you know all that, be my guest. You could fast forward. But I would highly recommend you to please go over and And so this way you will have the right standard in your in your memory and you will know exactly when you're starting it from the get go. You know how to start from the right path. 15. Scripts Naming Convention: Okay. Script naming convention. What are the names that we should use when he created first? First of all, I will tell you that every time you need to create scripts in your links environment, always created directory, call it scripts or my scripts. Or if it's a project based scripts type of like http directory or, um, payroll directory, where you're going to create your scripts. Directive structure is very important, so you don't you don't create scripts all over the place. So follow the standard creep creature directory, which ever folder Just you picked that you want to create your directory in. That is fine, then script name. The script name should identify the function if you are going to get into a job or if your job functionality is revolves around creating a lot of scripts, doing a lot of automation than it's best that you name it, that matches its purpose if you wanna. If your script is going to check the date, um, checked today's date and you are naming it X Y Z. Then again, a few months from the day you create, you're gonna forget what X Y Z is for, so it is good that you name it like date or something. My date script or something like that. And the script should end with a shell if you are using bash so that I would say right the name off the name of the script Followed by dot B a s h. Now, this rule this rule should only be used, which I usually follow This rule when I am creating multiple shell scripts. Meaning if you were creating shell script in corn shell, seashell, pearl or bash, then you you need to have the extension at the end images dot s h dot corn shell. So these are the few three basic standards that you should have. Let me log into my linens machine and I will show you exactly what I wanted to cover here. So if you going to your the next machine and you log into your home directory, let's say my home directory is slash home slash i absolve in my home Director. You notice I have Ah, Derek, you call script. So this is what I mean. So every time you create a script, you need to create a directory first. So this way you go into your script directory. So they say, You know, you don't have to scroll through on and find out where your scripts are. So in my home directory, you'll see I have a very few files in directory, so it's very easy for me to find out because it's a test environment. But maybe you're working in an environment where you create thousands of directories. So that's why it's important to create a doctor. Call a script once you create this cryptic tree, then you could just go in and you'll see right here in my home directory, I have a script called X y Z Dash script. No, as I mentioned X Y Z desk grip, it's not a really good name. You cannot tell what exactly it does. So if I run the script, you'll see it actually spits out Hello, World on the screen. So that's exactly what it does. If I wanted to associated that script with the named and I should do is X Y Z scrip. Move It move is a command to change a name off a file. I'm sure you know that, and I will say, um, hello world or W or O complete name. That's fine. And I will run that command and you'll see now my script name has changed to hello world. Now, if I am using multiple shells, then I should label it as move Hello world dot So hello, world Dodd B A s h This way. If I'm creating multiple script and corn shell or C shell, then I would know that this script was created in Bash. So this is basically very quick lesson I want to cover. Remember that this is the standard that you follow. Also, if you are doing that type of work in your environment and that you decided to leave your job and move on to another career and then the person who's gonna come and replace you that person I'm telling you is gonna be so happy or even your manager is gonna be so happy that you are so organized that all the script that you created while you stayed with the company are all organized and where to find them 16. Script File Permissions: script file permissions. Now, this is not just theoretical or concept or standard that I'm trying to explain it to you. This is actually mandatory. You need to have the right permission to execute a script and to and to have a proper permissions on the script file itself. So all scripts to be executed should have a proper execute. Herbal file permissions. And those are, for example, if you see here it's read, write and X is for execute herbal meaning the route. The person who created this group can execute it. Then read Dash X meaning the group who belongs to the user or the user belongs to a group, has a right to read and execute this crew and the last one are the other people who do not belong to your group or there are not you. They could also execute that script and read it. But of course they cannot write it because you don't see that w and W in the middle of group and others. So every time you create a script, you need to assigned the execute herbal permission. Okay, so now you're probably thinking, haven I create a script? Why can't it. Get a signed by default. Executed permission Question. But the problem is that whenever you create a script in Lennox or whenever you create ah, file, I should say in Lenox, by default, it does not assign execute herbal permissions because it doesn't want you to make a mistake and executed something by mistake and corrupt your file system. That's why it doesn't give the executable permissions. You have to sign them yourself. Okay, now, I created a script. Now, how do you change the permissions? Simply the command to change the permission is CH model A plus X and the name of the script . A stands for all. Everybody should have executed Bill permission off the script I use. I prefer, um, letters as a our, um, g or Oh, for user group and others. There are other people, or you probably are familiar with the digits as well. Like 65567 Something like that. Which I I I'm not too much familiar. I try to follow something that I am very familiar with, and I would like to teach you something that I know. Anyway, if you are comfortable with your own way of saying Permission, be my guest. You feel free to use that Now let's some see. If I log into my machine and I create a script, then how can we change permission? So let's see. Where is my Lennox machine? My Lennox machine is right here, and I am in my slash home slash I have solved slash script directory. I wanted to create a script and there are few ways you could create a file first. You always create a file. You could touch it. You could be I it and you could echo it and output to a file The different boys, whichever way you want to start creating a scrip. So first thing I will use a very basic is touch And the name of the script. Let's say script name is a party script had entered. Now when I do, at S minus lt are you'll notice the script that I had before. Hello. World has the execute herbal permission, right? This is the third but you see your way here. The new script that I created which is Apache script, does not have the execute herbal permission. So in order for you to run that script you need to assign execute herbal permission to assign the permission. You have to run the command change marred as I said, I'll use for all. Everybody should have executed Herbal plus X for execute Herbal The name off the scrip hit . Enter now duelist minor sell to your You will see the name off the script and it's correct . Permission. This is how you assigned permission? Very easy to do that. If you're already in Lenox, you've been using Lennox. You probably like ho. I know that. Perfect. If you know that, let's move on. 17. Shell Script Format: script format. Having a proper format inside of a script is very important. Why is that? Because I'm going to say I'm gonna bring back the same analogy. You create a script. It's a very long script, and a few months passed by, and now you need to modify the script or troubleshoot something in the script you have. Sorry, you have written down the script, but it's all over the place. The variables is up. Your comments are nowhere to be found. The description is nowhere. The commander left and right. There is no Indention, nothing. So there should be a proper format that should be followed so that someone can come in and take a look at your script, insist. Wow, that's a great job whoever did that so easy to read and so easy to understand. And what are those format? One is first and foremost is defined shell. This is something that you have to define. You could say it is a part of format are mandatory. You have to do that. You have to define a shell. Then you create a first script. You have to let your operating system no run that script as Bash script. If you're doing a corn shell, you'll have to specify pound hache slash bin slash corn shell. Anyway, when we write, our first group will cover that as well, then comments. Right after defining the shell, you have to put the comments, and those comments always starts with pound sign. And the reason I am telling you that you should include comments is because common should tell what your script does When your script was written who it was written by, Was it there any modification, anything that you wanted to include that a user o R. Someone else reading your script can understand what this script is about? Please add that to your comments, then defined variables, variables what Those variables are real. Cover that not to worry body, but just to let you know quickly. The variables is just like an alias. If you have a long command or if you have ah, a bunch of emails. What people do is they give it, um, a letter equals the whole big command that becomes their variable, so define your variable at the top after comments. Then you run the commands, whatever the commands that you want your script to execute. You need to specify those commands or if you do not have command or if you have any statement scripts. That's where you put your statement scripts. Those statements groups are if then, while for loop or so on. Now, when you have statements defined, you should start. If the line where you gonna put an if hit tab? Because this way it will show that you're f statement is starting. From this point on, there is nothing that that script will translate your tab into something else. You could use spaces. You could use tabs, anything that you want. But try to make your script formatted in a way that if someone or even you read it after year to year, you'll say, Ah, this is what I did it so easy to read. So let me log into my linens machine again, and I'll should tell you exactly how you should format your scrip. Okay, this is my Lennox machine. I'm already in my directory. Perfect. I created a script earlier, or a file earlier. Call Apache. I'm gonna take that same script, and I will formatted the way I have just disgusted so to do that. I have to now do a V I on that screen. And I am sure you guys know how to use a V. I some some of you are familiar with them, which is probably in a Hass worsen O V I. I like to use a V I It's kind of old school, but, um, the good things that it's available and pretty much all the units or her linens flavour So I could never learn something new and not utilize it on older version. Because I use V I in many system like Solara, say I X systems. And sometimes vim is not available. So v I is something that you will find anywhere, whichever the system you logging. Anyway, I'm gonna use V I to at it this file which will eventually become a script. So you hit, Enter now the first thing. As I said, you have to define a variable. It had been bash. If you look at the back, you'll see the first thing is defining your shell. I want to run this script as bash script. Perfect. Now the next thing I said to add a comment, Okay, I always add a comment. As usually add a comment is like purpose testing script format date. Put any date, Let's say August 22,020. Um, modification. If you have done any modification to the script, put down the date off the modification. So zero to 08 So last modification. Of course. Since you're creating the first time, I'll put 20 again. Okay, then anything that you wanted to add, Please Go ahead. Do so. So if I just put in block love whatever, it's just a common. It has no effect on your script. OK, Perfect. I understand Imron. But how come the first line that you have is slash bin slash bash has a common in front off . It isn't, that isn't it will be considered as a comment as well. Awesome question. Trust me, I had the same question. But let me tell you, this is just how it is. Sometimes you know, it is how it is. So the Lenox will understand this as actually a shell script, and it always starts with pound and hash. Why? I honestly don't know. If you really want to know why you could ask someone else who really know inside out when they came up with, ah, writing scripts. Or you could I'm sure, um, go online and find out why you have to define with hash. But anyway, put that in the back of the head. You have to remember there's no matter what. Okay, so moving on, then you define next thing is variable. Variable is something like I want Every time I want to echo something, I just want to say a and then echo. So let's say in my case, I wanted to have, um single coat Imron epistle. And you know what? All the same, my name is Imron. This is how you define your variable And then you could just do echoed dollar a Then it is going when you say echoed dollar A By the way, this is this a scripting language? I already started teaching you, but that's okay. Ah, you need to understand the format so variable I have defined here that every time this command will run, it is going to call upon this variable. Why didn't I put that whole thing here? I can't, of course. But what if you are working an environment that requires a huge line and does huge things. Then you it's better that you put those variables in there. Then, of course, then you put command. Let's say you want to run a date. You want to run a command G of minus age or so on, then The last one I included as statements. If you are including statements in your script, please do not start from if, let's say, um, dollar a equal. Um, that's a file or whatever. Do not start like that. Always put tab like this. So if something is equal to that file, enter and then tab again then echo this else echo dad and then fee to close the statement. You see how it is that if Kantar if statement came to be on the right hand side So you know that this is the statements that I want to run and they are and then did to the right side . What are those statements? Kits? I never seen that. Don't worry about it. I'm gonna cover that in my next lessons. The purpose of this lesson is just that you know that. Why would I follow this format on the following lessons? That's why I want undercovered that right now, so you wouldn't know. This is the best way to put your scripts together, and it will be so easier to remember when you come back to 18. Sequence of Script Execution: sequence off the script when you are defining command inside off a script. If you want to run, command a command, be and you want the command be to run first. Then you should put command be first. That's how the script works. It goes through the sequence and start from the beginning, and it goes to the end. So if you have command one, 23 and so on, it will run the command one followed by two, followed by three. For example, if you wanted to run ls command first, put Ellis Command first in your script, followed by date and Kell, which is calendar. These are a few example commands I just picked. So if you want to run date command, the first thing, then you should move that up before l s command. That's how the scripting works. Any script that you run, whether it's a corn shell, poor old, any kind off programming language that you know, it runs this way in a sequence, something you should know. And ah, if you are confused that hey, I run if else statement. But if works this way and it runs else that way because that those are the statement scripts. We will cover that statement. Scripts Don't confuse yourself right now, but focus on this one script sequence it always 1123 as they are sequential in a sequential order. 19. Welcome to Basic Shell Scripting: Hello, everyone. And once again, welcome to basic shell scripting section. Okay, so this is the section we're actually gonna get into Creating shells. Gross. The basic cell scripts. How we could create jobs, how we could run simple, um, echoes scripts. Or how we could use input and output off our state mints to war with the shell scripts. So what are we going to cover in this section? So let's take a quick look. First, we will start by creating our first script. Yes. So this is the fun part. Guys okays first Shell script. You probably have written many shell script as well. Ah, and the basic one. It might be like, Ah, you know, like ABC for you. But that's OK. We'll start with always start with ABC. So we'll learn how we could create a first group. How we could execute and permission stuff like that. Then we will create a second script where we will run basic tasks like based on our regular commands. We will run our basic tasks followed by the basic administration Task Adminstration task Are those like if you are checking the disc base, our memory and how can we put that in a script that will do all that automated for us is in that fund that you don't have to log in any anymore. And then run those tedious commands everyday to check your system and see how it is doing. We will cover that in the lesson. Okay, then. Well covered the defining variables. You guys know already about the variables, right? How can we define variables in the script and actually use it? Input and output. When you run a script, it will ask you a question. And based on your answer, how is going to respond those type of question? Now, how does it work in the rial life? Well, if you notice when you run a script in Windows or even in Lennox and you installing a package or something, it will ask you question, right. Do you want to go ahead and continue? You click quiet and says Yes. Based on your input, it does certain functions. So that is about but output. Then we'll have condition statements. This is where it gets a little complicated. But don't worry, I am with you. That's what I'm starting with. Basic. Um, it's just like as if you are, Let's say going toe a war and you were in the military. The first thing they do is they actually train you. So this is the whole thing is about training. I wanted to start from scratch, so you know everything inside out. Then we'll have some additional conditions. Statement. But for the case statement and then four loops statement. Why do we need four loops Statements? What are the advantages offered? Do? Well, scripts will also cover the Doo Wah scripts, and the last one is getting online Help. What does it mean? Getting online help. Now, let me tell you this. I'm not like a I don't know every scripting, um commands. I don't know every scripting, um, variables inside a script. So sometimes I also go online and find help. So I want you to also utilize that same technique to go online and look for Pacific scripts that can help you achieve your goals. So how can we get those on my house? All explain it to you, so sit tight. Let's start or start, but are creating first grip 20. Create your first script Hello World: Hello World. Okay, Yes. This is the basic script that most of the time that I have seen people, right. Whenever there, star learning, scripting languages. So we're learning back scripting languages and the first script that we will write as to echo or toe output on the screen that reads Hello World. I remember when I was in college the first script that was given to be by my teacher and or but my professor was too right. Ah, script that will. I'll put print on the screen that would read Hello World. So anyway, I am going to do the same thing for you guys that I want you to create the first script. If you know this, how to create a script, awesome. But start with me. Let's go with me. So we're going to start with creating the first group that will output Hello World. So I have already my lyrics machine session open and I am logged in as myself. No, as a practice, as I mentioned before, that always create your script in a Pacific directory, so it will be easier for you to remember were those scripts are so let's do CD script I'll have already created that. You should have that too, if you follow my early lessons. If not, then go ahead. Run the command make de ir followed by the scrip. And it will create a script in the script. If you do ls minus. L you probably not gonna have anything if you never had a script before here. If you do. Ah then go ahead. And the lead descript the first script. All the lead is Hello world. This is permission denied. Why is that? It is owned by me. It is owned by my group. And I could remove it by looking at this. W it has write, write permissions. I cannot delete it. That's a good troubleshooting exercise as well. Right? Uh, I know it. Why I can't delete it occurs when I created the folder or directory called scrip, I was rude. So let's go. One step back. Cd dot dot Do ls minus l and you will see. Yeah, I was right. See? Right here says this directory is created by route. So anything under this directory will be protected by user route. Nobody could touch it or delete it. So the best thing I should do is to become route, go into my home directory, and I was simply removed the directory now, so this will start fresh. Okay, so now it's gone. That's exit out off that route. Make sure you are yourself. Yes, we are. You logged in ourselves also. You could check by looking at the prompt. It tells you the, um, name off the user logged in and the server name. So now we are in our home directory, and now we're going to create a directory called Scripts. And we will put all our script from this point on to our folder called Scripts. Now, make sure it is there. Yes, it stares the end, and now it is owned by me. Now, you don't have to run all the scripts as root. The only time you would have to run scripts as route if the commands that you are adding inside of your scripts required root privileges. Yes. So you could run any any command like PWD Ellis or anything that do not require root permissions. If you are running other commands, let's say ah d m I decode or d message or D f uh, dis command. Those are the type of commands a require route rights. So it's better that you run those commands. Ah, those scripts. Ah, you create the scripts as route anyway. So let's get into our scripts territory. And now you see a We don't have any file or any directory in our scripts directory. So we will create our first script and we'll call it Hello World and you could put dot bash as well. But since we will be working primarily with Bash Ah, Shell so we don't have to specify. Ah, the extension. So just simply, um, hit Enter. Once you are in the V I mode, you specify the shell. So we want to run this script as bash. Good second thing. Who is the author off the script? I will say it's myself. Then again, comment date Script is created. Let's pick any date. So let's say if you're picking zero a slash 20 sacks. 2020. Okay, um, Daniel put in. It's a little description description and that will read this script Will output Hello, world on the screen. That's it. Simply that if there are any modification so you could put a date modified and then you could put the date. Since we're doing the first time, you could just put it the same date as you put a did created. Okay, so our shell is defined. Our comment is defined. Do we have to add anything? No, it's just a simple script. So I will go ahead and write the script that were print Go. Hello, world. And that's it. Now you could just to escape out of fed and right. Quit. And your script is created now. Verified. Yes, the script is there, but you cannot run it. Why? Because it does not have execute herbal permission right here. So in order to, uh, a sign proper execute herbal permission, you will have to run the command change mod. Oh, plus execute herbal. Hello world. And by the way, I'm using tap that auto completes file. And if the ball does not exist, it will not auto complete as an example. Let's say if our instead of h e l l o put h A l l and hit tab. See, it's not gonna work, so it has to have a correct spelling. Okay, so we have modified the permissions. Let's verify it? Yes. Now it has the execute permission. As if you see as soon as we could give it. Ah. Gave it executed information. It's color change as well. Now, please remember, don't focus on the color, as I said earlier as well that sometime maybe you probably creating a script in Solaris or in a I X or in any other UNIX or links play were that does not give you that color changing scheme or that depends on the environment. A shelf. So that's why Just focus on that it is executed. Well, by looking at execute permission. Okay, now, perfect. Now let's run it. So again, you could run it through the hole, Pat, which is Absolute Path script directory and hello, world and hit Enter. And there you go. Your foot script is created. Isn't that fun? You have just created your first script, which outputs on the screen. Hello, world. Now what I'm gonna do is I'm gonna run the script as a relative path. Sadat slash Hello, world. There you go. It does the same thing, but it executes it with a relative path if you notice. Um, there are no lines in between on top and bottom, so I wanted to make it more clear. More neat. Look. So what I will do is modify the scrip. Hello, World hit. Enter. And now I'm gonna put one echo here and one echo here. So when I put these echoes inside that it will actually generate an empty lines before. Hello, Orland. After hello World and datemodified again. I could just put it like, Let's if you're modifying it a day after the first time you created then you just 21 save the script And now let's run it. Hello, world. Nice. You see? Now it is giving the output that you want Now as exercise I would recommend you to go ahead and create a couple off, um, scripts like that that will output on the screen Like my name is Imron. Absolute. I love to teach and I am whatever that you wanted to add, just I'll put on it, chop the lines which over the way you want it. This will give you some practice on how to creates him. Well, script 21. Run Basic Tasks Script: basic task rips. This is our second scripts that we will create together and this will actually execute some basic tasks, uh, that are, like, based on some basic commands. So the task that we will define in our script as the 1st 1 is P W D. That will print my working directory ls which will list all the files or directories in my current directory. Who am I? Um, it will print my user name that I'm logged. And with date the crunch date tell the current cat calendar and the last one I want to create it. Three files in my directory that I'm logged in. Ah, dead files are A B C. And the command we was used to create them is touch. So it's just that simple. And that script will run sequentially. If you remember, it will do. The 1st 1 is pwd than ls and then so on. So my machine is right here and I am going to create my second script. Make sure I am logged in as myself and wish directory on men. I'm in scripts directory. Perfect. That's the directory I wanted to be in. Um, so I will go ahead and v I a new file and the name off the file of a pick as they basic tasks. It's just that some and her define your show. Author. I'm gonna keep making you do that again and again. So you will remember Date created, Joe. Eight money, 20 description Run. Basic Lennox Tasks Date. What was this? Fort one? Tell me. Modified Yes. Again, You're not modifying. We just putting today's day and I guess that's pretty much it. Now, I always like to start my script with Echo, so it will be a new line. And this time I wanted to write down Echo. This script will run a few basic tasks. Now, remember, this is not a description. This will actually output on the screen. Then another echo just for a new line. Now what we want to do, we want to see which director we are in. So simply, just type P. W d. Okay, it did that. No, let's see if he could do ls who am I? Date cow. And the last one is I wanted to create three files A b and C, so that's pretty much it. It's gonna run 1234 by six commands six basic tasks. Core those tasks what exactly they're gonna do and go ahead and escape. Bottle fed and right. Quit Saved the file and check permissions there. No permissions assigned the permission. Basic tasks. U Unless my cell tr just to confirm you basic task has been created. Now go ahead and run basic tasks better. Nice. Okay, let's see what it is. Okay, so this script will run a few basic commands. This is the echo. Think that we added in right? Then the next thing we wanted to know which directory we are in, we are in this directory. Then l s L s will list all the directories and files inside off my exit current working directory. And those files are basic task and hello world. So it's this own file that I just run the command and then who am I? That's my name. I am logged in as myself date. This is the date. It's telling you what date is today and calendar. This is the calendar that is output. Now the last one is Hey, what happened to touch ABC? Did it create that file? I am sure it has. But why didn't you see in hair when it was listing that files? Very good question. Now let's go back to a sequence. Remember, the script works in a sequence in an order. The first thing first. So by the time touch ABC, it hit that touch ABC. It ran. Ls command before that? So that's why you didn't see it. So now, if you run it, you will see your three styles in there. So without running the script, just two ls minus l. And you will see in there. There you go. See you. See? I said I'll do ls myself to your ltro list everything in, um, in ah date format. When was the last created? If you see, it has created a B and three C files. So this is how we do our basic tax. So if you are going through all that hassle every day and you want to automate them through one simple basic tasks, you could do that to simply this way again. I like to keep things neat and clean. So what I would do is I will put echo in every line Cool co and echo and then at the bottom of us say echo, end off screen. Nice. Welcome. You guys probably knows hate in Ron just did something I didn't purposely I just put it down wrong, um, command And let's see what the era Messi comes up. Um, it's run the basic tasks, and it did everything I asked for. Now you see, it is it has given me those ABC files as well. And now at the bottom, it says Line 21 Command not found. Okay, so it's something went wrong with the script. Now, this is how you troubleshoot a script. I wanted to cover that troubleshooting help part right here at the beginning. Because every time you write a script, there is always, always a chance. I would say I'm not gonna say that very little chance. We are humans. We make mistakes. There's always a chance that you fat fingers some of the commander. Or are you make a mistake. Then script doesn't run. Then how do you troubleshoot? Well, the Lennox offering system or the scripting shells are smart enough to tell you where the problem is. So it's telling you that the script is this and in line 21. There's a command called E C H. That command now found You want me to run the command? I understand, But there is no such thing as command. Why? Let's try it just by simply typing. Yeah, there is no such command. So we know that in line 21 in this script there is a mistake and we have to fix it. And the way I do it his i v i the script and I will Let's say if I'm right here or anywhere , I'll just do Colon 21. This will bring me exactly to that line where it was complaining they go. So it brought me right to the line and I see Ha. Okay, I'm sorry I made a mistake. I left out. Oh, now it will run it. Perfect. Perfect. I fix my mistake. That is perfect. That's how we learn. So when you hit, enter again run script and they go, it is giving you everything that you asked for and at the end is telling you End of script . So it's It's It's again. It's a good practice that you I'll put that What exactly the script will do at the beginning that will tell the user, Hey, the script will run a few basic commands. It will throw that out on the screen. And when the script finishes, it should say End of script Perfect. So that's how we do. Ah, simple basic task using scripting language. So try it out. Tried different commands in there. There are so many commands and Lennix, I want you to try them out one by one and see how you could automate a few simple tasks and fueled daily tasks. 22. Run Basic Administration Tasks: keep on moving, so we are going to create another script that will run basic administration tasks. So before we had the basic commands that we wanted to create a task in a script. Tsunami Wine tour on basic tests Those are basic administration task and those basic administrations task An example I picked as the 1st 1 is one top 2nd 1 is DF minus age. 3rd 1 is free, which will check the memory us by the A d f. Is for to check the disc space up. Time is to tell it, when was the last time system reboot and system load I o stat, is input I o, um, statistics. So I'm sure you know these commands already. If you don't know that, it's fine. You could look it up and find out what the command do. But anywhere these commands are here to check the system health. So when I actually joined my first company, this is something that we were doing manually. So every time a system administrator would come in, they will run these commands on the servers and checking the system health. Are there any issues? Are there any displace issue and so and so. So when I learned somewhat of scripting the beginning, I was like, Wow, we could do so much about this and raced in middle wasting so much time we come in, we spend like hours just going through so many systems and running certain command. So I created the first script called check scrip. So, uh, and that would check the system health. And it would actually I added so many things later on so many other commands that actually we needed as for Aziz by the system, But anyway, the point is that you could, um, and automate all those system administration checking, type of related tasks. So let's create a new script and we are logged in as ourself. And we are in our home directory and script Eritreans. As you know, we have thes two scripts have created, and then we have a few empty files. That's fine. Believe them, dear. The next group we have, you want to do admin tasks. So this is a name I picked. You could pick anything that bets, fits the description off your task. Go ahead and hit. Enter. What do you do first? Define the variable Yes. Then author who is writing the script that as you and my kids. Me, of course. Um, then we are doing Ah, date 08 0.2. 2000 in 20. Let's see. Um, then we are doing description. The script will run basic admin commands or task whichever that, um, you want to pick? Date modified since you're creating the first time. Same date. Okay, Perfect. Now I will always like to do at Go and Saiz this script will run basic administrative commands. That said, this is how it's gonna come up. Um, of course. I want to do echo hair and one echo here. Okay. Now, what are we going to do? We are going to run top command. First, I'll do echo again. The F minus. Etch Command Second echo again. Again. As you know, Echo is just to put an empty line so it will look more organized of time. Echo I O stat, Echo and echo and off. Perfect. Okay, good. Now let's go ahead. Save it and assigned permissions and the name off the file waas admin that. Let's tr verify the permissions are given. Okay, so now let's run the script Okay, so it did run. And if you notice really quickly, it spit out the line Where say's this is what the script does. And now you noticed that the SEC the first command that I wanted to run in my scribbles top . And now it's not running other commands. Why is that? Because the top command is a command that will keep your session open and give you the status off your processes, your memory in a realtor time so it does not exit out. It just stays there. So that's why my script is kind of stuck for it to finish so it could run other commands. So how do we overcome this problem? Well, one thing you could do is you could tell the output of the command to be ah, simpler or, ah, pipe it to something else. So this way it will run other commands. So how how can we do it? That's do quit and you see, as soon as did quite out of, um sorry. As soon as I did quit out of top, it did run all the remaining commands. But of course we were stuck there and we didn't know what happened. So what I'm gonna do is we will look back. You will modify the script again. And script Knee was admin tasks. And this time we're gonna do is we are going to say that when top runs to skin me the first 10 lines. That's fine, because mostly the top part off the top command has the most important one, which gives you the information about memory. CPU process is important. Most processes that are taking up most of the CPU. So I'm gonna put pipe it to head of minus turn. Um, and then we will run back. Gin scripted. Okay, so let's run it now. Admin task. All right. See it getting gives. You gonna wait for a few seconds and then I'll give you every because it was processing and now give you from here? See, this script will run basic administrative tasks, and those tasks are the 1st 1 is top and the top is giving me the 1st 10 lines. Then it it runs DF minus. H command, which is giving me the output up to here then is running. Ah, free free is free. Minus M is for the memory information to be include free. Ah, no, actually, you forgot. That's why I was looking for free and it was not there Anyway. We'll add that, um the next command that it was running was up time and up. Time is right here. And the system been up 47 minutes and the last commanded ran was hyo stat, which is right. And the last thing we added was end a script. So of course we missed a V. I Mr Free Command. That's fine. We'll go ahead and add. And and it was after DF minus h. So Echo three minus m here and now the missing command that we wanted to run, they go. So see, it did give us. Yes, it is right here. The city output off free minus m. Okay, so this is how you run the basic administration tasks. You could actually also filter out this output to through a pipe and you give you anything Pacific that you wanted to know about this output. So what happened was ah, in my, uh, beginning years of an I join my first company on Iran. This oven. I created this check system. Um What? I did it Eventually it was throwing up so much information that we really didn't need it. Toe be modified. Fills dirt out a lot of output that was coming from thes commands. So you could do that too, depending on what exactly you're looking for. Um, my recommendation is to you is go ahead and create couple of scripts that will run your basic administration task and see how they run. 23. Defining Variables: in this lesson, we are going to define variable and run those variables. So this is a good exercise where you will learn how you could define variables at the beginning of the script. So anywhere you would need to use those variables anywhere in the script, middle or the end, you could just simply run that container that very so what exactly a variable is. So let's look at this. So look, it's, um list that I have Now. You see, I have defined Ah, variable for P. I have put a pwd for l is just simply a less w is for who am I d for date and see for Cal 2018. So these are the variables. The variables are the first digits pl w d. C and the commands that that variable the commands that inside of those variables, I would say, are the ones on the right hand side. So this is how we're going to create a new script by having those variable defined at the beginning. So I'm gonna log into my machine. I believe I already have logged in. So, um, what I'm gonna do just for just for fun just for exercise or just for practice. I'm gonna go one step back and I'm gonna, um, create a new directory called M K D I R. And I'm gonna call it my scripts. So it's just to tell you how you could differentiate from one director to another. But anyway, which ever direct to you Make sure there are There's Pacific directory for every script. Don't just put it in your home director or anywhere else. It's not a good product. Um, so I have the structure created my script and, um, I am going to create a new script. I'm gonna call it, Let's say, ah, variable uh, test Mr. And the first thing, what do you have to do is been bash, And then you have to define the comments, the author description. The data was creating all that stuff. I'm not gonna take a lot of your time to go into defining every single one off it, So I'm just gonna from now on, I'm just gonna define the the shell, which is the mandatory thing. So and then I'm gonna go straight to, um, running script, that writing the actual so anyway, because I do know the time is valuable for everybody, but I am very hopeful that you guys do follow the standard that I have. So, um, first thing that is I'm gonna do is come put a comment here, miss it. Define very okay. And those variables are 1st 1 is P equal. PW 2nd 1 l and Ellis. W is for who am I? But you know what? Actually, there is a command called w and Lennox, so I'm just gonna put, um um who am I? Just w i b. That would be Who am I date for? D And see now, since the sea it is a calendar and that I want the 2018 calendar, so it has a space in the middle, so I have to put that in cold. Single coach Caliphate. Cynical doesn't work in your environment. Double coat. That's fine. Okay, so now you're variable defiance. No. What do you do is you could just simply say echo. Um run variables. Tasks. Back again. Now I'm gonna run command. Sorry. I'm gonna run the variable p to define the variable to run it or to get executed, you have to start with dollar piece of this way. The operating system or the shell would know that you want to run their very okay. Perfect. Next. Very well. I wanna run. Is l 3rd 1 is W I 4th 1 as d and one C. It's just that simple. Save it. Okay, now let's give it up. Missions. See there. That's very for one more time, if you see. Ah, the reason I came in I want to see if it has all the variables in Hey, looks good to me. It's tried to run is to test a script. Always run it variable. Okay, so we do have some kind of error messages. Perfect. That is fine. Because, as we learned, we wanted to see where those are messages are. And how can we troubleshoot? As you see, the first message that is giving us that there is in Line eight is unexpected. End of file while looking for matching coat or single course. So let's go to line eight variable to go to line eight. It's just they and it's a Ah, see what I happen. I put single code here, and I put double coat here. Okay. All right, So one problem. It's fixed. Run it again. Okay, I think it ran. Find this time. Hopefully, yes. Run variable tests and was variable as PWD is this. The file is variable inside of that directory, there's user, which is a variable date and the calendar. So that's how it was coming up. And you notice when I ran the command earlier test variable. When it had a problem, it gave me error messages on to line. Do you know why? Because first line is eight, which had that double coat on one side and single quote one side. So that'd be a no, that was era. But why? We were getting, um, error on line 17. Think of it. Let's check y v work. I know, but I wanted to show you exactly why. So let's go to the file. And now this time, type seven. Uh, you know why? I'm sure. Now you know why? Because in 98 the defined variable was this which had a problem. And in line 17 when it came down to run that variable, it says, Hey, I can't run their variable. So that's why it gave us error messages in to line. Okay, so anyway, So that's how you define variables? Um, going forward, I will be using scripts. I will be writing scripts in, Ah, different directories. Um, I could be writing in my my script. I could be writing in just the script director, but anyway, um, you could just follow just one director if you want to. Or you could just do different directory depending on how you want to structure your script . Ing's skills and how do you want to organize? 24. InputOutput Script: input and output. This lesson is about writing a script where the script will wait for the user in. So when you run the script, the script will wait for a for you to type in something so it would take your command and then output based on that written statement that you have given to the script. So the way it works that the things are the scripting commands that you need to use for that is read which will wait for your input, and the output would be eco. So whatever that you input, it will output to the screen using the echo comment. So, um, let me lock into the machine and I will show you how this could be done before had confused ume. Or so I have my machine open right here, and I am in my home. I have sold my scripts directory. Um, I am going to create a script a little, um, in put It's like that and I'm gonna go ahead. Yeah, I will define the variable. Um, next thing is, you know, you put the author I'm not going to just wait. Times a date, mom description and so on anyway. So you you you should type everything. I would definitely recommend you. So again, since you don't have time, I really want you learn fast. So don't worry about it at this. Then you, um you put in like, um Echo. Hello. Hello. My name having a bad time type. Okay, so hello. My name is M Ron Capsule. And as you know, it will just simply display that line on the screen. Now what we wanted to do next, then we wanted to do re no. Sorry. Um put a gorgeous for a empty line and then go, what is your name? So now we're asking user for user input and the way the script is gonna wait for the user input as by simply command read the read is actual command in UNIX or Lennox that will wait for the user in No, you have to define a variable to read. So any the variable anything that user types will be automatically added to the variable. And what is that variable? Well, you have to specify or defined every Let's say, um, pick any variable. Let's you and I picked name, So we'll say you know what when user put in the name put in Put that input into a container into a variable and their variable name is actually name was just you know what in simple words, um, name contain. So make it easy so you don't you are not confused with name name itself. No. Once the usurper whites that information ah Then simply put an empty line and then do echo . Hello. And now you put dollar name container, which ever variable you have defined to hit her and go and that's pretty much let's give it proper permissions. Probably missions are there. Let's run it. Okay, you see the first line on the screen as Hello, My name is Imron absolute or your name. And now it's asking you what is your name? So it's waiting. See, you didn't get the prompt back. Why? Because the script is waiting for your input. So let's say you want to type something. Let's say, um, Jerry sign And now you hit Enter and it's gonna see Hello, Jerry Seinfeld. That's how the input and output script works. Of course, it gets some ah, little bit more complicated as equal on, but basically, if you wanted to write a script and you wanted to take the input off the user. You have to use a read command. Now let's edit the same script at a few more things in the script and make make it more, um, professional. Let's see V I input script and now wanted to define variable. So I'll say, um, the variable. It's a equal host name and you know, host name is actual command. And Lennox that will give you the host name off your system. So I wanted to say hello. My So we're name is Cellular Eight. And what is that? Dollar? A. The dollar A is a the container right here. And that container is what host name that I will show you right here. And then you'll say, What is your name? And now, just for giggles? I wanted to change the read it to just a different container. I'll put B So just so clarify that don't you do not focus on the container name container name could be anything. So you are asking what is your name? And the user who's gonna provide the name will be put into the container called B. Now echo. Hello. Being and by Okay, So now what we're doing, we're saying, is that when you run the script, tell the user Hello. My server name is dollar ate. And what? His dollar? A dollar is the host name, and it will print it here. And what is your name? And you will put in your name and it will. I'll put back to your name. No, before I run it. I just want to tell you one thing. There will be error in the script. Why? Because when you define command, would the, um a variable? You could run their variable. If you only simply type dollar a, then it will run it. But if you try to run that variable using like eco command, then you have to have this ticks on the side. What are those ticks? Those techs are right below your escape key. Those other ticks. So you have to have those. But anywhere I want to take it out for now. Just so you see that? What do we get if he don't put those ticks in there? So escape? Save it. No, Let's run it. It's his. Hello. My server name is host name. Okay? It actually took the exact name that I type. It didn't think that it's a command. And it now, this is what is your name? So I'm gonna put it Imron f cyl and is going to say hello, Imron. Okay, so this part from right here to hear works this part from here to here works. But I wanted to have my host name here. And what is my host name? My host name is my first Linux VM. I wanted to have that displayed here. So going back to what I was saying is, if you are defining a variable and you want to run that variable with echo, then you have to put the little tics that is right below um, the escape character or escape key. And also below the that little curly thing. So now let's run it and see if it works. Ah, yes, it did. You see right here. Hello. My server name is this is the whole What is your name? Okay. Imron. Hello, Imron. Absolute. See, that's how the input and output script works. Cried out with different things, play around with it and ask question back and forth, back and forth. So first you should. What you should do is as your, um, practice. What you should do is you could put more than one read how you could do it is like this. Put, um, go just to empty line. And then you do again Echo. What is your profession or your occupation? What do you do? And then you have to read here again. Read. See? And then, um then you do echo again. One more eco. And now put in, um, do you like, um, you know, let's put it what is your favor show? And now this time, read A we have used be Have you see? Now, use D. Okay, that's it. Now, what do we have to do? Is first thing is had to say is cool. It will. Hello. My name. Next thing I want to do is it should echo. Um, you're you're profession, which is whatever the profession see is awesome. Next one, um, dollar D, which is favorite chew is good. You see how I did it first? I asked three questions one to and three. Once I have all those three questions asked. Then I have put all these question different containers. And I am all putting those containers one by one by saying Hello, Imron of soul echo your profession, whichever is is awesome. And then your show, whatever the show is, is a good show. Let's see. Run it successfully and put Okay, The first name for the first question comes up. What is your name? Imron. Yes. See, now it gave me a second question. It didn't give me the output right away. Now it's waiting for me to give it all the information it wants. What is your profession? That's a, um engineer. Okay, what is your favorite show? Sign fell. And now and I hit Enter because I have answered all the three questions and it will say hello, Imron. Absolute. Your profession engineer is awesome. Seinfeld is a good show. See how it works. Now think off this now when you are installing a program or you are executing some kind off script and or even if its windows or even of his Lennox. Now you know that when you going through the visit and ask you questions do you want installed at you? Say yes. No. Put in your organization name. Put it this. You've type in your name and this and that. All the information that system gathers from you, it actually plugs that information inside wherever your variable is defined. So that's just a simple example of that. So it's like your little script visit that will take all the information, and it will send it out on the screen. You could also take that information and put in your database. Send it out in eczema, whichever the way you want. So that's how you work with input and output scripts again, Um, I will say, Go ahead and keep that script long at more things to wear. It had fun things to it. Ah, there so much you could do with it. And if you wanted to share that, your new creative script, you might get sent me an email. And I hope your script runs without any error. If it If you get through any problems not to worry about it, that's how we learn 25. If then Scripts: if then scripts. Now you know how to write script. Let's get into one. Step up some advance groups. Some. If then scripts these air the scripts, which will define some variables. And if they're those variables are met. Then you could run those scripts. And if they didn't script statement. If this happens, do this otherwise do that. So whatever that you define that has to be met, then it will run it. Otherwise, I'll give you some different output, depending how you write a script to better illustrate this concept off. If then script statements. Let's get into our machine. Okay, Mr To Be are in. We are in our home directory and the directory inside. Off it is my scripts and this directory. We're gonna create a new script. I'm gonna call it the our if then script again. It does not have to be the name that I'm picking. You could pick any name like my script or X Y Z or ABC Anything that you want. So when you we wanted to write a script, you do B I and the name off the file. It will get you into the insert mode. So the first thing do you do If you want, Roy, a script is defined. Variable. Now, I already have a script written that I'm going to copy it from so it will save us time. And this is the script which checks for the variable. I'm gonna copy it and I'm gonna paste it. Now let's look at what's this group does. This script is gonna look for count equal 100. So if Count equal 100 which is the variable then is going to say echoed a Countess 100 If it is not equal 200 that is going to say the count is not 100. So that's what pretty much if else statement does. So let's look at our script. Let's run it that first save it. And once we save it well, of course. Where fly the file is created. Yes. What's inside of the file? The look into it and it has exactly the same thing that we typed in. Now we need to give execute herbal permission to the script. Verify it? Yes, it does have execute permission and I'm going to run it. There you go. It says the count is 100 Perfect because we defined in our variable that the count is equal . 200. So what happens if the change the count to, let's say one? We save it and we're on it. Once we were running, it says Count is not 100. So this is the exact purpose off. If then statements is if it looks for the variable, whatever the variable is defined. If the variable value is not met, it will tell you exactly what you wanted to do. You could also change the output by saying if then else I think. Sorry, the count. Let's make a lower case. Count is not 100 again. Countess 100. This is our variable. And it says if bracket count equal 100 then a coed Countess 100 Els otherwise echo. Sorry, the count is not 100 fee Fi is the opposite off. If and it is telling the script now, you should exit out that your work is done. And if else statement is done so you exit out of it by saving it. And now when you run it, it tells you Sorry, the count is not 100. So you just change the output to whatever you want and that's what the If statement if else our if then statement does another example off. If else statement that I'm gonna pick is right here, I'm gonna copy it and I'm gonna create a new script is if then new, I'm gonna define the variable which I already copied. And the first command I have clear it means when the script runs is gonna create the screen . I don't want to see anything that's already on the screen. Next thing is going to do is if minus e which is used, which is the option that is used to check if a file exists or not. So it's staying. If this file home my absolute narrowed a text file exist than what you should do, you should then Atco file exist else. If file does not exist then say echo while does not exist. And that's what pretty much it's doing to Let's run this command. And we, of course, have to give up. Execute herbal permissions that's verified has excuse permission. Okay, does no let's run at if now new Okay. And you run it is this file does not exist. It's telling you the actual thing. Why? Because there is no file exist. There's do cat. If then and it's the file exists where should define exist That fire should exist in my home directory. Error that text. So I'm gonna create a file. Now I'm going to just touch it, and I'm gonna create this file. The file is created. Now I'm gonna run the script, and now it says file does exist. So this is ah, sometimes a very useful command. And it is very useful tool for system administrators who wanted to run this command, um, at a certain intervals or a certain day because, um, an application create some error files or not. And they run this script on Cron job to see if the file exists or not. If it does, it means the application is running fine. If it does not, then it means the file doesn't exist, and it will tell you an error message. So this is F if then statement and we do have other effects else statements, examples in this file as well. And this file is also part off. Your, um, resource is your handouts, and I want you to try out every single script, create every new script and try out different values that you have here. So this is a very cool script that I have written. It tells you about your name and it's still asking you, Do you like working in I T. And it asks you if you if you respond, yes, I will tell you you're cool. If it does now, you say you should try. I t. It's just a good feel, so I want you to run the script on your own, created, create a new file, write the script and executed, and I'm very sure it will run just fine without any air. 26. Case statement: que statements groups. Now this lesson. We will go over the statements off case where you are given options in a script to pick from. So it's like an interactive way off involving user whether they want to select Option ABC so forth. So in case statement, if a option is selected, do this be selected? Do that CIA selected do this? So these are the different options on what, exactly a or B or C is gonna do? Well, it depends on what the variable you assigned to them. For example, if you assign a to do, um, to up date on the screen, it will output the date. What if a is selected? Now, let's let's look at the script. I have a script right here. Always starts with the show. Shell is bash. The next is Echo, which is just printing out an empty line. Then it's gonna echo. Please choose one of the options below. It will print out a as in for variable equals display Date in time B list file and directories see list uses logged in and D checks system up time. Now, this is all just a display message that's gonna show up. Once the script is run, then it will read. All the choices are put all the choice in a container. Then the case statement comes in. It's gonna say choices, which is this variable choices in And the container? We put it. All these choices and what the's choice is going to do. A. It's going to run the command Date B is gonna run the command. L s C is going to run the command list of users War logged in and D is going to run the command up time. Remember, all of these are actual commands inside off your Linux operating system. When you run it individually, it will give you the date. But this script will allow you to automate the task toe. Pick these commands for you. So let's copy them. This is the whole script we have, and I'm gonna go into our Lennox machine, which I have already logged in on logged in us myself and this territory. I'm and I'm in my home directory. I'll go into my scripts territory. Um, I believe, uh, my scripts CD, my scripts. And in that directory I have already created some of the scripts that we learned in the previous lessons. So for this, I'm gonna create a new script. Gonna v I and I'm gonna name the file case script. You could name it. Anything you want once you are in V I mode hit, insert by and you could just simply right click on your, um on your mouse and it will paced everything for you. The first option is once again it's just a shell. It's telling you to run would shell environment. The 2nd 1 is you type in anything this script is created, but by me or yourself your name or what everyone you want to do. Second thing it's doing by the way, empty lines in the script doesn't really marry. Believe the Empty or you could just put a hash sign. Next thing echo again. Echo is just gonna throw in an empty line on the screen. And as we went over the script, this exactly what's gonna do. Um, and you're gonna save this file? Do ls my Celt er this is still a file. It's not scripted because we have not assigned executed permissions to it. So to assign that secured permission a plus X to assign acceptable permissions toe all user group and others and the name off the scrip s case script Verify it has all the permissions . Yes, it does. Now we're gonna run the script by dot, slash and type case. Once you're gonna run it, you'll see it's gonna give you that. Please choose one off the options below. So you have the list of options right here? A to display the date and time. It's nothing but exactly the display off all those texts that I have typed after Echo, which you see right here it's everything after echo. It's nothing but, um, and output on the screen. So now if I select okay and run it it exit of the script and it gives me the date it runs the command date itself as a few type date. It gives you the date, today's date, the time and you let's run it again. And this time will pick be once you do be it lists all the files or directories inside off my current director, which is the command that you already know, which is Ellis. Run it again. And this time I'm gonna pick D check system up time and to the command to check system up. Time is just simply up time. And this script will run up time command and put on the screen. So this was exactly the case statement does. So the last option. If you notice we have a star right here. What? It's telling me that if any other option is packed, it's gonna say echo invalid choice. So by so let's try to run it. And this time I'm gonna pick F. Of course, F is not one of the listing. So once I do, that will say invalid choice by. So that's how the case script works. I want you try it out. Pick different commands that you want to run, pick different text message that you want to associate with those commands and try to run it. Let's say if you want it, um, pick A for what is your name, and it should type who for it, and it will give you the log in name and so on, and all the different commands that you have learned 27. For loop Scripts: four Loop scripts. Another advanced level of scripting you are keep getting better. My friends keep getting better. The next script that you're gonna run is for loop scripts for loops. What they do is keeps running until a specified number off variable. For example, if a variable is defined, 10. Then run the script 10 times or variable is green, blue, red. Then run the script three times for each color, so it actually simply fires your tasks. If you want to, for example, create file files or 10 files, then you don't have to run the command again and again again. Full four loops will do it for you. We'll go back into our Lennox machine. I will try a couple of scripts, four loops and then you could try on your own and you will become profession were running these little little time tie scripts. So which dirty we are in? We are in our home directory. Always. Home is not just home but entire home directory. And the dirty that we are in is my script. So in my scripts we're going to create a new script. We're gonna call it this time. ABC do V I A b c. And the first thing you know already by now is that you define the shell script that you're going to run this script in, or the shell environment. I would say that you're gonna run the script in and again with. I have already written few script for you to go over this training, and I'm gonna go into our first group, which is a simple for loop output. I'm gonna copy the script, and I'm gonna just simply paste it. This already has a defined shell, so I'm gonna delete that line. So what is it doing for I in 12345 Do so I hair is a variable, which is telling that this container, which is the container name, is I. It holds the numbers 12345 And then what is asking you to do? The script is gonna do echo welcome variable one times variable. Two times wearable. Three very before variable five times. And then once it's done, it is done. So that's why we have to define done at the end for the four loop to finish doing what is doing. So I'm just going to save the file once the files saved again, you have to give execute permission. The permission is given. That's verified. Yes, the ABC filed is there. Let's see what's inside of it. Inside, If it is exactly has the same thing. Let's run the script, ABC and their ego. It's doing welcome. One times, two times, three times, four times and five times. So basically, if you have a repetitive task and on trust me, you're gonna be working in a corporate environment where you would have to do so many repetitive tasks. And this loops script is gonna become so handy for you that you will be creating scripts for every repetitive task that you will be doing every day. So this is, um, uh, putting five times. Let's look at the second script. So the second script, what is doing? It's basically doing the same thing. But this time it will just output the names. And I'm gonna make this X y Z as the name of the file. And it has always starts with the shell. That shell name has been bash, and now this time you're gonna put in some comment and we're gonna say this script will output Oh, put Imron with different actions because it will say See Imron, eat cm Ron Ron, see Iran jump and see Imron play So this is what this group is gonna do. So that's why I have put it in just a comment. You could put more comment Mint. See, the script is created by Imron absolute. And if you want put a date, you can Medals kept that up and the script starts right here. It's telling you, for I in again I is a variable in Is you putting inside of your variable and variable is just like a container. It's telling you put, eat, jump, run, play inside of a container called I Once it has all these three or so four words inside of the container, I then do what we're doing. We're echoing. See Imron Dollar I. The variable is already always be displayed with the dollar and the name of the variable. So it's telling it, dollar eye And then, once I once the script is gonna display every little every word in the container, then it's gonna be done. Once done, the script is created. Script has done its job and we will verify it. The Scriptures here Next thing you know by now is to change the permission on X y z. So it will have execute herbal permission and executed permission. Once again, I am reiterating again and again. If you cannot run the script is because you do not have execute permission. So we do have executed excusable permission. Awesome! Perfect. Let's run it X y z Oh, it say's bash X y z no such file or directory. Why is that? Of course we have put c it Z actually x y z So let's run it and they go see him. Ron, Eat, See and run Run! See and Ron jump cm Ron play. So again it is doing a repetitive tasks for you. So this four loop is a very strong statement in a scripting environment that the lets you run repetitive tack again and again so you don't have to do it yourself and spend time all day and creating those little tedious task. I want you to write other scripts that I have inside of this file. This file is again in your handouts. Um, I want you to download this handout. Create these little scripts again and run it and see what they do. It will be a great exercise for you. 28. Do while Scripts: do wild scripts do? One. Scripts are very similar to four loops for scripts. The Do Well script or statement. Continued lead executes a block of statements, while a particular condition is true or met. For example, if you are running a script and you created in a loop, it will run until a certain variable is met. For example, if you have, if you want to run the script until August 2015 to 2 p.m. Then you will define that and do well script and a lot of times the scripts or the demons, the process that never dies, that keep running and running and running. They always have do wild scripts because they are running content continuously until a certain condition is met. So, for example, Rana script until 2 p.m. As soon as the two PM hits, you're done. Exit out while condition do command one command to command three. And once all these commands are done and the condition is done, then you are done. You are done with your script. So let's look at our Lennox machine and let's try out ah, do a script. It's clear the screen where you are. We are in my script Victory, which is this directory we want to be in. And this time you're gonna create in this new script and we're gonna call it script, do a while again. Um, it's just the name I'm picking. You could pick anything you want Hit Enter. You are in V I mode, you hit insert and you start typing with the first thing that your type type hash bang been bash, which defines your shell environment. And if you wanted to put a comment, you could say this script is created. But in Ron have so on 2018. Yeah, that's fine. And if you want to put a description of the script, you could do that. But let's get into the script. I have already written in the script. I'm just gonna go in and copy it. There are two scripts in my hand out. I'm gonna take the 2nd 1 I want you to do the 1st 1 yourself. Copy it, and I'm gonna paste in real quick. Let's see what this guy does the script is doing. Of course, the 1st 1 is defining the show, which we already have some we're gonna take it out, but hitting D D. And then it's telling us that the count is zero. The number as 10 wild account is less than 10. Do echo number of seconds left to stop this process, and the process is the variable, which is dollar one, and echo is just empty space. Empty line. Sleep Sleep is a command that will wait for the number of seconds that is defined after the sleep command. Then it has a few number of expressions. These are expressions. You could get it anywhere online. I have that expression for a while that I tried out many times in my work environment. It works perfectly. Any phone is using in your work environment, you're more than welcome to do so. The next one is sitting echo, and then it says echo dollar. One process is stopped and echo again. It is telling it that it's giving an empty line. Let's run the script and that see what it does give execute herbal permissions. It's his 10 seconds left to stop this process. 987654321 and done process is stopped. You see that? So these do Wah scripts are created for a certain value or certain time to be met, and once that variable is met, it will stop the process and exit out of it. I want you to create a new script in your environment, and this is a script again. This is also in the handouts. You could copy and paste the script, create a new script and run it. You could also be my guest to go online and just type Do wild script examples. There are tons and tons of do while scripts that you could try and get get a low, um, exposure to do Y scripts. I'm sure by the time you will run at least five or six scripts, you know exactly what this statement do. 29. Exit Status: All right, let's look at the exit status. What are the exits? Status Every time a command is ran in a shell are in love would say, Let's go one step back in. And Lennox, it always has a status associated with it. If it is successful, it will always give us status of zero. And there are, um, statuses from all the way from 0 to 2 55 Every status of the different meaning. These are the actual Our codes are the numbers that developers associate it with a different meanings. So every developers or every application have a different meaning. But for us, we only gonna focus on a very few statuses. And that is number 10 which is yes or no. If this is a command that we ran previously is successful or not, if it is not, then it will give a message one x status one, which is known as a minor problem. Or number two. It's a serious trouble or everything else, which is exit status from 3 to 255 everything else. So why do we need actually actually exist? Status in our scripting? Good question. Well, uneven. We have to do a lot of comparisons in our shell scripting. Following this lessons and in comparison, you need to know if your command rant if it did ran and it was successful, then do this. Otherwise, don't do that. As we learned that there are statements, Chris, if else while statements every every one of those scripts or statements do need some kind of status before proceed to the next, uh, execution. So me show you real quick by logging into my Lennox machine. Um, I am logged in as myself and I am in my home directory. So now let's see if I run a command ls minus l. And it runs successfully, right? It gave me everything that I was looking for, but a I want to know, actually, the status off my proceeding command, I would do echo dollar sign. And then the question mark, this is telling the shell or the bash that what was a command that ran previously read and tell me whether that command was successful or not. If I get a zero return, there you go. It means it did run right because I wanted to get a listing off all my Derek Tree files and directories in District Tree and I did give me and that's why I have zero. Let's take another command. That's a advantage to wish directory. I'm in. I'm in home. I have solved directory Perfect Iran. And if I do echo dollar question mark again, it will give me zero again that it was successful. What if I run a command? That's it. P W D D right This command, there is no such command. So if I run that it says bash commands found similar command is PWD. Give me a recommendation. But anyway, the command was not successful. So now if I asked the shell whether the last command was successful or not, I will run the same command echo dollars question more. And it has giving me error code 1 27 which is different from zero. So, as I said, anything from 0 to 2 55 is not, uh is this Eric owes, But anything above zero is all our coats. Anything that is zero is successful. Now when we run, when we create a script. So let's say I go into I create a script, I call it, um test, um, code. Just that. And I will. Right? I will declare my show. And now I will not define all those stuff that I know you already know. Since this is a very small, um, script, I'll just do echo. Hello. And then I will ask if the command was successful and I will save it. Test code. Now when I run that, you'll see I'm getting the hello. And but that I'm getting zero. It means it was successful. Same thing if I wanted to do something else. Let's say if I go back to test code and this time I will do instead of echo all just replaced of it. Kiki. So there is no such commanders. Icky. So anyway, let's see what the error message comes up. Now I'll run again, and now it's giving me the seamer Massive that it was not successful. Ah, same way. If I try ls minus l l l l. And it does exist, actually, let's say ls minus z. There is so many options. Right? Um ls um tax tax tax tax peak. Okay, so there you go. See unrecognized options. So now let's see what the area code is the area code is two C. It's a different. And that is, um, about error code that shows serious trouble. So this is about you have not defined the option successful. Now, let's run another script really quick. I'm gonna create that script, I'll call it. Um, we I exit code, um exit code script. And I was defined the show. And this time I wanted to make sure I run ls minus l'Homme. I have solved last check. This is the command are trying to do. So what is going to do is is going to do ls minus l. And it's gonna ah, Onley, find out if home IVs all and there is a file named check exists. Okay, so now I have to define the statement. If dollar question mark, which is command that I ran equal to zero, which means if the command ran successfully, then do what an echo file exist else. Echo file does not exist and feet and let's run it and give executed permission. Now let's run the script and it will say cannot access home. I absolve. Check. No such file and directory. Then it stays off Course file. Does not exist because the Eriko does not match. Ah, zero. So if I create a file check in my home directory So I'm in home directory. I'm just gonna do touch and check The file is there, right? So just verify it. Check. And it is, dear. Right. So now when I run that script again that I just created and it did give me that file with my LS minus l command. And it's his file exist. So that's why it is. That's that's why we the exit status importance is there. So this way you could actually, um, associate each code with some kind of meaning. So developers like C plus plus or jars other developers like object oriented based programmers or people who write operating systems. They are the ones age happily used these codes because thes codes make a lot of difference . But in our, um, class are, in our lessons, going from this point on, mostly going to you zero to make sure figures exist or not. And that's why most of the time the system administrator engineers use in the every day Lennox, um, scripting languages to try it out and see how you could create different scripts with Eric codes and what their code gives you 30. Welcome to Real Life Scripting: welcome everyone. And welcome to the real life shell scripting section. This is the actual section that you've been waiting for, where you want to learn actual real life script ings In this section, we are going to court all ex scripts that I have actually experienced in all my 16 17 years off system administration system engineering, type of related task where I had to actually automate tasks using shell scripting. And there are also many other shell scripts that I have included in this section that you would wind very, very useful in a real life for you. If you are already in a system administrator capacity, you will find those scripts really helpful. That will help you automate your daily task. I'm not going to list every script right here because I will keep adding them as the course goes and you will see so much information about 31. Accessing data from a file: accessing data from a file. Ah, file is consists of some kind of data right there. It has, ah lot of information inside, and you need to pull that information every now and then. Now, that file could be any file that could be application file, database files, a simple text file, a log file, any kind of while that you try to retrieve some data, massage the data and you want to view it the way you want to. Now, you could do that manually by running commands, adding commands here and there every day. But of course, if you are doing that, changing off data are running those commands every day. Then this task doing a manual become tedious. So the best thing to do is to automate that and the way you automated. You actually create a shell script to do that. So in this lesson, we are going to create the first grip level poll errors, failures or warnings, or any kind of information that you want to pull from a file. So most likely we will be using, like our log messaging file, which has, which is a huge fall which gets, which actually logs all the error messages or warnings of failures or many other ah system related messages in a file. But sometimes when you want to view that while you don't want to go through all that stuff , that information stuff or something that is not related or it's not, um, actually what you're looking for So this is the first script it'll go into and create a script. The second script that I wanted to cover is then you actually have some of the administrative commands that you run and then you want to change. Um, the output of the file and perhaps output toe a different ball said this way, Every time you come in, it's viewable by, um, and you whenever you run the command. So let's look at our first. I'm gonna log into my Lennox machine party client. I'm logged in as myself, and I am in my home directory. Now I want you to copy Ah, war log messages from war log to your current directory. Now, what is your current directory? You contract is home. I have salt. I want you to go ahead and create a new directory and call it, um, Sessional scripts This is the name you have chosen. Maybe you add all our new newly created scripts into this directory. And when I do ls minus l to you are You will see the bottom. It has that directory now. I wanted to copy noir log messages, file into my current directory, and there would be dot and you hit enter. Okay, You say it's coming up as permission Denied. Why? Because war log messages file is owned by room and it is not. It does not have read permission for anyone else. So what we will do is will become route. And now I will copy war log messages. So I'm doing CD war log messages to my home directory. And from there I will copy it to professional scripts. Now it is copied. Now let's go to my home. I have salt professional scripts directory and let's see, the file is there. Okay, good. So now, in order for me to read that file, it should have the right read permissions right here. So what I'll do is I will go ahead. And first of all, I'll change the ownership filed. C h mod. I have solved messages own bill. Okay, I think it's changed. Ma messages file toe. All right. Oh, no. Sorry. I'm using change mine. It has to be changed ownership because we're changing the ownership. Okay, it has changed. Now you'll see when we make this a little. Now you will see this file messages fall is owned by I have salt. Now I'm going to change the group as well as a change group. I, um, have saw two messages, and now you will see I should be able to read this file so I could have assigned the read permissions right here of a change model. Or I just have given the ownership a change. The ownership too. I have. So So now I could exit out of room. Rich, dark human. I am in slash home. I have soul. I'm gonna go into my professional scripts directory ls minus lt r. And there it has the messages. Now, to make sure I could read it, I could just simply cat the messages follow just to make sure I created Yes, I could read Clear the screen. Now I'm gonna create first scrip called Cool date. If you are using different scripting as Well, I'll put dot Basha here. Well, just literally pulled the and her. Now what do you do? First thing you give correct shells. Sorry. The shell, which has been bashed next, you know. Hello there, Date general. A slash 08 slash 2020 description. This scrip will whom Onley error messages from war log messages. You could directly pull those messages, but you have to be rude, but since, um, be in a room, Um, it's just example that I'm giving the copy. The messages fall in a local and home and modified if it is modified. Since we're greeting for the first time Totally the same date. Okay, So our basic things are done. Shell is defined, the comments are defined. Now, what are exactly be doing? We wanted to pull Onley error messages from the messages follow. You don't want to view anything. So to do that, we'll have to do cause we are groping for error messages. Minus I is to ignore or case case ever from where I would always specify absolute path. You don't want to specify the relative pat because if you're running from another directory , this same scribble failed. I have so professional scripts. So for now, just run that and just make sure works control. Um, quit right quick. And it Now, the next thing you have to do is give executed permission to pull data. This mind sell TR and you'll see our first script is created. So when I run that pull data it say's grip home I have salt is a directory, huh? Because we did not specify the file. You need to go in one step inside. So pull data. And we actually trying to grab for errors from directory. So of course, there is no such thing is Dark doesn't have the data. He only has filed, so we'll put messages. Okay. Now, when we run this script, it is giving us on Lee the error messages all the far all the lines inside of that bar log messaging file. I'm Onley getting the error messages now. Probably thinking Why do we need just a room messages? Well, if you are in corporate environment, trust me. You gonna be troubleshooting system related application related so many times that you would have to go into the log files and look for her messages that where it failed, so you could go through 19 by line by line, which is gonna take you forever. Or you could just simply fill their out for our messages. And then you will get right to the point that why and how it is failing. Now, there are some field messages right here. Of course, we're not troubleshooting these field messages, but it is just an example that I'm giving now. Every time you come in, you log into your system, your manager or your ah, someone from the development team or someone who say, Hey, I'm getting our message from this system or system is running slower. This applications failing or something. So you just all you have to do is run the script and see what are those Air messages? Four. So what I will do at this point, I will instead of do pull data, I will rename this file to move pull data to pull errors. So this way it is Pacific to, um the data are the words that you're tryingto so let's run again. Full errors. And now we have errors. Similarly, if he wanted to, um, pull all the warning messages, we could do the same thing. But instead of creative recreating a new script, what I'll do is I'll copy pull data off. Sorry, pulled errors and I'll name it Cool warning. So now I have two scripts. One is pull errors and the other one is pulled warnings. But now both heart. Same. So I have to go into the pull warn Sign quit and I'll have to instead of error put in Warren. No save now Let's run pulled Warne now and you will see you have a lot of warning signs in the system. What's going on? Um see, right here is warning application genome settings killed by signal five or any of the things that happened with the application of your system, it will be logged here now. The last one I will do is this. Copy it again. Pull! Ah, warn I will do. Pull. All right, now feel. Look, I'll go into field trip again instead of warn. I'll just do failed now I had three scripts to verify. It did. The last one is pulled fail. So let's run, pull, fail! And now it's on Lee giving me failed related lines, anything that has failed in the line. I will only get dad now. You could also leave it as is right now if you want to. But most the time of people do is they put this to another file. So this way they could easily minute police text inside of them file instead of just going into the war log messages. So how that is done, you could just let's go back to our first group, which is pull. And here I will go group minus. I are, and I will output to my home directory and directory. And now I will name the output off the script as output ever. If you notice I have to. Slash is here. So I'm gonna get rid of one. So now let's run it now. This time and you run it, it is not going to show you all those error messages on the screen. Instead, it is going to push out all the messages that came with the word error it's gonna go into ah, file that be just specified. So let's run pool era and you see, you got your prom back so it doesn't mean it didn't run. It did, Ron, But has created a fire that you wanted to create. They go see output error. So now you could have simply cat out this up put air file and read through it the same way you could do it where the fail the same way you could do with warning if you want to. Now, also, what happened is which I will also cover later on that many system administrators. What they do is they schedule this type off scripts in Cron job. They tell the Cron job, run this group at certain times of the day, and when you come in you all you have to do is view that file that's one step further. Now, if you want to take it another step further, what it will do is it will not only run the script. It would also send the output off the script, which is the help put Dash air file through your email through the Sendmail Protocol, which is one of the best ways you could do that. When you come into your office in the morning and the first thing you do, you open up your email and you say, Hey, I got these emails and alerts that saying I had this type. Our message is so this is about, um, creating a pulling data from files. You could also modify it in terms off if you want to just see the number of warnings could just do pull warning. Do you see when you run it, you get all those messages Now you could use your own script and combine it Web operating system commands to view or to change the output. The script. If you want to see how many warning messages or lines we have and me grip for warning, you could just do pipe W C minus l. And you'll see there are total off 46 warning messages. This shows that since we have the system, we have, ah, 46 warning messages that came in you could be more Pacific. So if you want to just do correct and coat and you just wanted to do August 27 then ever Onley pull August 27 warning messages and how many we have for August 27. You have 46 so basically all those messages that have came in today anyway, So this is how you pull data This is how you modify the second script I wanted to go over is script that before Matt output of the administrative commands that stood administrative commands. Um, plus, I'm gonna create a new script, call admin commands and hit Enter, and I will go through again because this is something that you really need to know. There description of the script format, the output off admin commands. And the last one has modified. Please don't get annoyed by this because I'm keep typing that the reason I do that is because I really want you to get in the habit off doing that. This shows that you are an organized, um, engineer administrator who actually organized his or her scripts and put all the scripts in the right way. So 20. Okay, now what other commands that we want to do? Um, let's do some administrator commands like date time, um, and DF minus h. So let's say if I want to do that and let's say you run it before we run it, we have to give execute herbal permissions and now gonna be run it. You'll see. I have date right here up time and the d F minus cape. Now when I mean to format the output, you could do that by either You run the script and formatted in a way like, let's say, if you only wanted to view the first Harlem off your scrip, then you will only see like Monday. Ah, the date of time on file systems are that's if you want to change the format, Um, after you run this or you could go into the script, the script, a Zvi I admin commands and you could go into date and you could do talk print dollar one up time you could do look, print dollar, let's say three. And for the DF minus H little just gripped for room. So this is how we change the output off the commands. Now, when we run it, you'll see the first column it gave us Monday. The second column is a date, and that there is defiance. H I just wanted to know on Lee the route station how much this space I have, how much I have left. I don't care for other, uh, file system. So this is how you make changes to it? Um, same way there are so many commands that we learned earlier. If you want to change the admin commands on you wanted to format it, you could try with different commands. And this is how basically we could access the data from files. 32. Check Remote Servers Connectivity: Hey, everyone here is another newer scripted. We will go where it's about checking the other server connectivity. Of course, you need to know all the time. If you are in a bigger environment, you need to know if your other servers are actually reachable and paying global. Now, I know there are a lot of monitoring solutions that that are out there that, um a lot of companies have the actually, those are monitor the servers, they monitor the health check and everything. But still, once in a while, you do need to actually paying other servers a remote servers that actually tells you if they are alive or they are down. So this lesson is about pinging of the servers. Ah, what we're gonna do is we actually gonna create a script to check the status off the remote host or hosts. Um, and we'll find out if it is payable or not. Very simple script we actually have to combine in this group. We have this combined. Um, if ah else ah state man's view might we will have to actually combine do wild statement together, and you will see how we could pink other servers. Um I have a Mullinix machine open. And I also have a quick document here. You could say a cheat sheet, which already I have a script written there, because if I go over completely blank with you, I might forget, and I might lose time. And I definitely don't want to do that. I definitely want to lose my time and your time. So anyway, the first thing I want to do is I do want to mention one thing that, um, previously be actually created. Um, a directory called professional Scripts. Now, I noticed this whole directory is a very long directory. Let's make it short. So this way it will be easier to actually change, actually from one place to another. So I'm gonna just do move professional directory scripts, too. Um, PS this simple is that now you'll see I have a doctor called PS when I goto PS, which stands for professional scripts. And now I have all my scripts. So just to start off with that now, let's start creating our first group that is going to Ping an I P address. Now, the I P address that I have selected is the I. P address off my home modem, which is one night to 1 68 $1.1. And most likely if you are in a home environment, not in the office than your I P address is the same as 1 91 61 That one. If it is not the same, then you could go find out the default gave a i p just to test if you could Pinkett or not , Or you could just pink. Or you could just pick any I p that you could, um, test your script against. So I have picked 19 to 1 $61. One. This is my i p that I'm gonna test. Um, So let's first create a script, and I'm gonna call the script Ping. Okay? And this is the first time I will copy, then later on. So I'm just gonna quickly do the information date description this script. Well, ping a remote host and notify, of course, Then, um, modified. And since this is the first time and I the okay, now, let's get into the real work now to be defined their variable. Now, Ping, I'm sure you guys know what a pink script is pink goes out on the remote host Ping An I p. Address and tell you if, um, host is available or not. So let me just right Quit out of this script. Make sure you save it. And just if you don't know Ping than just typing one I to 1 68 that one. That one. And you will see you are getting 64. Bite back from this. This machine, it means there is a machine out there with that i p address and it is reaching anyway. So now let's go back to our So what we are trying to do with the scriptures you want a ping minus c is count just Ping once the i p one I too once 1 68 1.1 now you as you go along, you're probably thinking, Hey, how come m Ron knows all those different options to specify How can he remembers How can he just pick really quick? Well, to be honest, I don't remember there many times. I just go online, I google it and and I get the options. So if I wanted to Google paying only once all type paying only ones in Shell script or Ping only once. Um, remote host. And it will give me the option that I'm looking, so don't worry about it. You're You're not the one that you're thinking. Hey, my memory sucks. I'm not gonna remember that. Trust me, my memory is probably worse than yours, so don't worry about it. If you forget anything, go online and search for it, and you will get it As long as you type exactly what you're I'm moving forward. So we are paying this. I p alright. It will ping Evil Ping one time. Perfect. Now what will happen? What are you trying to do when it pinks? Okay, we're trying to say is if single bracket dollar question more now, what is a dollar question Mark Dollar question Mark is the output off all the command that you run by default? The dollar? Um, question mark returns to well, use one is zero, which is that Yes, it, um it ran successfully or anything else. So these are the exit status. Um, if you don't know differences between exit status, you could look it up, but there are mostly to exist. Added that I you leave, um, rely on eggs. Status one and two. So dollar question mark. If the return off my command above, which is ping minus C one d I p address is equal 20 Okay. To see if the return value is equal to zero. Which means it did run. It was successful then. Since we are if else dense statements, we have to specified then tab, As I said previously, always put tabs so it will look neat and clean. Then what do you want to do? You know, the pink is successful, The