Shell scripting - an Intro

What is shell scripting in Linux?

Shell scripting in Linux is a process of automating your day-to-day activities or regular activities on your Linux computer.

Linux provides you an option which is very good called man

man is nothing but manual so just suffix any command with man and simply type the command so it gives you the details of the command like it can be any command previously we use the touch command so I can suffix touch with man and once I press enter it provides the details of this command

What is shebang?

#!/usr/bin/env (sh/bash/python3)
you have to inform your Linux or your kernel that this is the executable(the one who will execute this file) that I want to use for executing my shell script or simply script.

one of the most widely used is Bash
the way you write a for Loop in bash and the way you write it for Loop in ksh totally different

so bash provides you more easy way of writing a for Loop whereas with ksh it is slightly different and because it is obsolete you don’t have to learn it what is obsolete mean like nobody is using it or very less people are using ksh.

Difference b/w [#!/usr/bin/sh] and [#!/usr/bin/bash] is that initially, in its(#!/usr/bin/sh) previous days it redirects to #!/usr/bin/bash only. That is previously they were the same. But nowadays, some OS decided to redirect it(#!/usr/bin/sh) to #!/usr/bin/dash . So it(#!/usr/bin/sh) works differently where #!/usr/bin/bash is needed. So, if you need bash then use this(#!/usr/bin/bash) not #!/usr/bin/sh.

some of the operating systems have decided to use Dash as default so your Shell script might not execute if you are writing in bash scripting on a machine where Dash is the default.

While you write a shell script then to execute it write #!/usr/bin/sh.
While you write a bash script then to execute it write #!/usr/bin/bash.

because we are learning bash scripting we’ll always use the same first-line i.e. #!/usr/bin/bash

chmod - granting permissions
(change permissions of a file)

File permissions (read, write, execute) for users -

  1. root user

  2. group

  3. All users

Granting access to everybody(777 ) [- -r- -w- -x : 4,2,1 respectively ] for each user i.e. for root we have 4,2,1; for groups we have 4,2,1 and for all users also we have 4,2,1.

To learn more about Chmod : man chmod

complete permissions to the group which is my group I did not create any User Group here so if at all I am creating a user group and I’m adding my colleague as a user in my group let’s say I’m in the development team or DevOps team so you can add people to DevOps group and what happens is Linux will grant everybody in the DevOps group read write execute access

Role of shell scripting inside DevOps
Shell Scripting is necessary for infrastructure automation, configuration management, and also your code management using git repositories. Although many tools have arrived today such as Ansible, yet we need Shell scripting. you have to write some shell scripts for regular automation of cron jobs or you know for switching into the folders.

Lets say,

John goes to these machines and uses the Linux commands to fix all of these things what he will instead do is he will make use of shell scripting okay so he will write one simple shell script and he’ll save the shell script in a git repository and whenever some devops engineer says that okay there is issue with 9999th VM what he’ll do is using the shell script he will just execute the shell script on his local machine and what the shell script does is it logs into this one of these he mentioned that 9999 machine is not working

so this shell script automatically logs into this machine and it looks into all of these parameters like CPU memory and why are the processes going slow which files are running how many files are open on this machine so he writes a shell script and this shell script returns an output saying that okay so I executed this script and you know what I noticed is the memory is completely used on one of these nodes okay so it returns whenever he runs the shell script it says it evaluated all these parameters and it says on 9999th machine he said that the memory is going out of place so the memory is completely used

On a frequent basis like once every day or once every two days he decides to basically execute this script automatically okay so without even his consent this script should get executed

and this script should look at all of this 10 000 VMS okay like I mentioned there are 10 000 VMS what this script should do is this script should log into each of these 10 000 machines look at the status of each of these 10 000 machines and it has to send out an email notification saying that hey John I evaluated all your 10 000 machines and out of which 10 machine seems to be suspicious okay 10 Mission seems to be suspicious and in five machines there is issue with memory and in five machines there is issue with CPU

Monitor node health by checking the CPU and RAM usage:

You can also use ‘top’ command to get a whole view of CPU, RAM , what processes are running etc.

How do we write cron jobs , how to trap signal? in part2

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

  1. The “man” command provides details about shell commands.

  2. “touch” and “Vim” commands create files but have different purposes.

  3. The “shebang” line specifies the shell interpreter.

  4. “/bin/bash” is commonly used in shell scripting.

  5. Writing comments in scripts is a good practice.

  6. “chmod” command grants permissions to files.

  7. “chmod 777” grants read, write, and execute permissions to everyone.

  8. “pwd” displays the current working directory.

  9. “cd” changes the directory.

  10. “mkdir” creates a directory.

  11. Comments help others understand your script.

  12. Shell scripting is crucial in DevOps and Linux.

  13. Git interaction often requires Linux.

  14. DevOps engineers use shell scripts for infrastructure maintenance.

  15. Shell scripting can automate tasks for fixing issues.

  16. Python is useful for more complex automations.

  17. Shell scripting is essential in the DevOps roadmap.

  18. Learning “man” commands helps understand shell commands.

  19. “chmod” is used to modify file permissions.

  20. Writing to files is necessary in shell scripting.

  21. Shell scripts can monitor system resources like CPU and RAM.

  22. Scripts can monitor the health of nodes.

  23. Monitoring involves sending Linux signals.

  24. Handling Ctrl+C in a script to avoid stopping it.

  25. Advanced shell scripting concepts can be explored.

CREDIT : Abhishek Veeramalla

Connect with me👇👇👇

E-mail: sparshvk18dominate@gmail.com
LinkedIn: https://www.linkedin.com/in/sparsh-kumar-2a2b17210