15/180: Going Deep Linux Kernel and Shell Scripts

Today is day 15 of 180 days of challenge, so managed somehow to learn regularly and post regularly. This challenge I have taken to improve my skills and make learning as a habit. It should not be like that if I am getting some work at office only then I have to learn. If I have to grow in my field I have to make sure I need to learn regularly, The more I learn more I become strong in connecting things and learning becomes a fast process. Today I will be discussing about the Linux Kernel and Shell scripts

Let’s begin!!!

Linux Kernel

It’s an interface between hardware and software. If we see the structure from bottom to top, we have hardware layer, Kernel program, Shell, Application or services, User. We have user at the top most layer. Combining Kernel and Shell becomes the Operating System and combining Application and Shell we can say it as software.

Introduction to Shell

It is like a container. Interface between Users and Kernel/OS. CLI is a shell. Finding your shell using echo $0 command. Shells available in “cat /etc/shells”. Linux have KDE and GNOME as GUI shells. Linux sh, bash is a shell. Bash -> born again shell. User friendly, have enhanced features.

Shell Scripting

A shell script is an executable file containing multiple shell commands that are executed sequentially.

  1. Shell (#!/bin/bash) -> first line of script
  2. Comments (#comments)
  3. Commands (echo, cp, grep etc)
  4. Statements (if, while, for etc)

Shell script have executable permissions. Example: -rwx r-x r-x. Shell script has to be called from absolute path cd /home/path/to/file. If from the directory directly then ./scriptname

Shell Script — Basic

We can do many things using shell script and one of the most important usage is automate system tasks.

  1. Output to screen using echo.
  2. Creating task. Telling your id, current location, your files, directory, system info.
  3. Creating files or directories
  4. Output to a file “>”
  5. Filters / Text processors through scripts like cut awk, grep, sort, uniq, wc

Lets begin with basic script to output hello world

mkdir myscripts
cd myscripts
vi output-screen.sh
echo Hello World
//Save the file
//make file executable for all
chmod a+x output-screen
//execute the script
//output result
Hello World

We can define small tasks in script.

#Defining variables
echo "My First Name is $a";
echo "My Last Name is $b"
//make file executable for all
chmod a+x filename
//execute the file
My First Name is Navneet
My Last Name is Ojha

Create script to take input from user

#Author: Navneet
#Date : 02-04-2021
#Script for taking user input
echo Hello My name is Navneet Ojha
echo #this will print an empty line
echo What is your name
read nameVar //for taking user input
echo Hello $nameVar
//On running the script this will prompt to the user input

Use of commands in variables

echo My server name is $hostname

Creating scripts using if-then

count = 100
if[$count -eq 100]
echo Count is 100
echo Count is not 100
fi //opposite of if to exit

Creating scripts using for loops

for i in 1 2 3 4 5
echo "Welcome $i times"

Above was brief description on Shell scripts. Now let’s discuss few more important Linux commands

Aliases: is a very popular command that is used to cut down on lengthy and repetitive commands

alias l = “ls -al” // on using l as command it will display the same result as that of ls -al

alias pl = “pwd”

alias tell = “whoami; hostname; pwd”

alias dir = “ls -l | grep ^d”

If have to remove the alias we can use unalias command

These alias exists till the sessions exists on logout everything is gone. We can also create global aliases by adding to the .bashrc file. Global depends on User and root. If have to apply alias for a particular user go to /home/user/.bashrc and append the alias command at the bottom. For making it global you can append the alias in /etc/bashrc file

Shell History

All commands are recorded in .bash_history file and can be seen using history command. If we want to run the previous command again we can run the command using the number given on the left hand side of command in the history. !numberofcommand

Downloading files or repos on Linux

We can use wget command to download something or the curl command

wget url
curl -O url

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.