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
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.
A shell script is an executable file containing multiple shell commands that are executed sequentially.
- Shell (#!/bin/bash) -> first line of script
- Comments (#comments)
- Commands (echo, cp, grep etc)
- 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.
- Output to screen using echo.
- Creating task. Telling your id, current location, your files, directory, system info.
- Creating files or directories
- Output to a file “>”
- Filters / Text processors through scripts like cut awk, grep, sort, uniq, wc
Lets begin with basic script to output hello world
echo Hello World
//Save the file
//make file executable for all
chmod a+x output-screen//execute the script
We can define small tasks in script.
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
#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
echo//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
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
curl -O url