Shell Scripting in Linux (Day 4 task)

Shell Scripting in Linux (Day 4 task)

1. What is Kernel?

A kernel is a computer program and is the central, core part of an operating system. It manages the operations of the computer and the hardware, most notably memory and CPU time. It is an integral part of any operating system. It loads into memory when Linux is booted. It is a collection of routine communication with hardware directly.

2. What is Shell?

Shell is an environment in which we can run our commands, programs, and shell scripts. Shell is the medium between the user and the kernel. A shell is a user interface for access to an operating system's services Shell is an environment in which we can run our commands, programs, software, and shell scripts. Computers do not have the capability of translating commands into actions, Shell does it.

When we enter commands through the keyboard, it gathers input from us and executes programs based on that input. When a program finishes executing, it displays that program's output.

3. What is Linux Shell Scripting?

Shells are interactive in that mean, they accept commands as input from users and execute them. However sometimes we want to execute a bunch of commands routinely, so we have to type in all commands each time in the terminal.
As a shell can also take commands as input from the file we can write these commands in a file and can execute them in the shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with a .sh file extension eg. script. sh

A shell script has syntax just like any other programming language.

4. Types of Shell

BASH (Bourne Again SHell): It is the most widely used shell in Linux systems. It is used as the default login shell in Linux systems and macOS. It can also be installed on Windows OS.

CSH (C SHell): C shell’s syntax and usage are very similar to the C programming language.

KSH (Korn SHell): The Korn Shell also was the base for the POSIX Shell standard specifications etc.

5. Why do we need Shell Script?

  • To avoid repetitive work and automation

  • System admins use shell scripting for routine backups

  • System monitoring

  • Adding new functionality to the shell etc.

    Suppose you want to create a backup for a particular file at a certain time every day, you can write a shell script to automate this and also by using of Cron schedule job as it's not possible to write the same task manually every day. So it will save a lot of time.

6. What is #!/bin/bash and can we write #!/bin/sh as well ?

This is known as shebang in Unix. Shebang is a collection of characters or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script. So we can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

yes, we can write #!/bin/sh, as it is used in scripts to instruct the internal system shell to start interpreting scripts.

7. Write a Shell Script which will print

I will complete the #90DaysOfDevOps challenge

Open a Vim editor by giving a file name as myshellscript.sh

#!/bin/bash [# is used to comment out the line, it will not print in the output]

# myshellscript. sh

echo " I will complete this DevOps roadmap challenge "

Before executing the script you have to give the +x (execute) permission to the file as chmod +x <file name>

To print a bash script uses ./myshellscript.sh ( bash myshellscript.sh if you're using Windows)

(To execute a bash script on Windows, you have a few options:

  1. Install a Bash emulator or subsystem such as Git Bash, Cygwin, or Windows Subsystem for Linux (WSL). These tools provide a Linux-like environment on Windows and include the bash interpreter.

  2. Use a bash interpreter from a third-party software installation. For example, if you have Git installed on your system, it includes the Git Bash terminal, which provides a bash shell.

  3. Convert the bash script to a Windows batch script (.bat) or PowerShell script (.ps1) and execute it using the appropriate command interpreter. This may require modifying the script to make it compatible with Windows syntax and commands.

Choose the method that suits your needs and the specific requirements of your bash script and system configuration. )

8. Important commands for Vim

h - Move cursor left

j - Move cursor down

k - Move cursor up

l - Move cursor right

i - Switch to insert mode

ESC - Switch to command mode

:q - Quit vim

:q! - Quit vim (ignoring unsaved edits)

:w - Save document

:wq - Save document and quit vim

9. Write a Shell Script to take user input, input from arguments, and print the variables.

#!/bin/bash

# user input and variables

name= abc

echo " Hello ${name}, enter your age ?" #[$name it is defined as a variable ]

read age

echo "Age is ${age}"

Output

Hello abc, enter your age?

Age is 20

#!/bin/bash

# take inputs from arguments

#arguments.sh

echo " Hello $1, how are you ?" [here $1 means the first argument ]

./arguments.sh Maher[execute the script as ]

Output

Hello Maher, how are you?

9 . Write an Example of If else in Shell Scripting by comparing 2 numbers.

We can use the if-else statement to compare two numbers

#!/bin/bash

# Assigning values to variables number1=10 number2=20

# Comparing the numbers

if [ $number1 -eq $number2 ]

then

echo "The numbers are equal."

elif [ $number1 -gt $number2 ]

then

echo "Number 1 is greater than Number 2."

else

echo "Number 2 is greater than Number 1."

fi

10. Some Vim tutorials

https://www.youtube.com/watch?v=5givLEMcINQ&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=9

https://www.youtube.com/watch?v=TwV_VYApysQ&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=10

https://www.youtube.com/watch?v=Ue9ErbfVCLQ&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=11

https://www.youtube.com/watch?v=lspQJKIO2Eg&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=12

https://www.youtube.com/watch?v=Hbm8MzvpAmA&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=13

https://www.youtube.com/watch?v=NgMpP18xXAo&list=RDCMUCRclYvtDY8M6gb2n8bcDlKA&index=14