What do you know about Linux?
Linux is a popular operating system, which is designed on the base of Linux Kernel. Linux is an open-source system which can run on multiple hardware platforms. The importance of using the Linux operating system is to provide free and less expensive operating services for users. Linux operating system is also considered as a user-friendly work nature which can be modified easily and also enables the user to create variations in the source code. Users can use this operating system like Windows, MAC, Android, and IOS. This type of operating system always conveys the input data which is processed by the processor and brings them to the hardware for display purposes.
History of Linux:
LINUS Torvalds first invented the Linux operating system. Linus Torvalds was a student at the Helsinki University, Finland 1991. During the time of his academic project, he started writing codes on his own to get the easier version of the UNIX for free. Later on, we started calling it a Linux Kernel. In the current tech market, Linux is the fastest-growing operating system. This operating system is used from developing mobile applications to supercomputers by almost all the top hardware companies.
Linux Architecture
Linux operating system establishes the interface between the computer hardware system and the users. In this operating system, many software application devices run on the operating system, mainly used to run the hardware devices on a computer. Linux architecture mainly consists of 4 layers, with the help of the below diagram; I will explain them one by one;
1. Hardware component layer: The hardware component layer consists of the physical devices which are attached to the computer system. For example;
Hardware disk drives, RAM, CPU, Motherboards, ROM, and more.
2. Kernel layer: This is one of the important core components for any LINUX operating system, and it directly interacts with all hardware devices.
3. Shell layer: Shell acts as an interface between the user and computer devices. With the help of this shell component, takes input data from users and sends the instruction to the Kernel. And then takes the output data from the kernel and send the results to Shell.
4. Application Layer: Application layers are the utility programs that mainly run on the shell. The application can be of any type like the web browser, text editors, and media players.
Advantages of Linux:
Below are the important key advantages of the Linux operating system:
- Linux operating system supports most of the hardware components without the need for any external system drivers.
- This Linux operating system provides a very good Graphical user interface.
- With the help of a Linux operating system, many applications are developed for Windows computers only.
- Windows XP provides universal Plugins and play features.
- Windows XP is user friendly when compared to other operating systems in the market.
- Linux systems are stable and free to use.
- No threats of viruses and Linux operating systems come with most of the pre-installed software devices.
- With the Linux operating system, users will get the highest degree of possible customization.
Linux Certification Training
- Master Your Craft
- Lifetime LMS & Faculty Access
- 24/7 online expert support
- Real-world & Project Based Learning
Linux features:
Below are the key features of the Linux operating system:
- Multi-user capability: by using Linux operating multiple users can access the various system resources such as hard disks, CPU, memory, and motherboards, etc.
- Offers Multitasking: with the help of the Linux operating system, more than one function can be performed simultaneously and also divides the CPU time wisely.
- Portability: You can carry your data using pen drives, or memory cards. This operating system supports different hardware types.
- Security: This type of operating system offers three ways of security, namely Authentication: here user needs to provide USERNAME and PASSWORD, Authorization: which uses the permission tokens such as read, write, and executes Encryption: this helps to convert the files into an unreadable format.
- CD/USB usage: all Linux distros offer live CD/USB so that users can run/try any operating system without installing it.
- Graphical user Interface in Windows XP system: Linux is available in the form of Command-line interface this can be converted into GUI by using packages.
- Supports customized usage of keyboards: this operating system is used worldwide, so this supports different language keyboards.
- provides different application support: Linux has its repository so that users can download and install many applications.
- File system management: Linux provides a hierarchical file management system so that you can arrange any type of files and directories.
- it’s an open-source: We know that Linux codes are available free to all of us and also community-based development projects.
Why should you start using Linux?
Here we are going to give a few points which will explain this;
- Linux uses fewer resources and computing power than any other operating system such as Microsoft Windows and MAC.
- It will increase the ability to learn the use of other software and specifically, information security tools.
- The open-source platform enables multiple systems to have an operating system without limitations of product keys and license.
- Although Linux is still vulnerable to malware, the majority of present malware is certified to target windows because it is still the most widely used operating system.
- Today it has an advantage for the programmers, which is they can design their own custom operating systems.
- it’s a completely trouble-free operating system and don’t have an issue with viruses, malware, and showing down your computer.
Linux BASH- a complete detail:
BASH is a type of UNIX shell and the command lines are written by Brian FOX to work on the GNU project. It is free software and sometimes we can use it as a replacement for Bourne Shell. This is also interpreted and cannot able to compile it. BASH shell can also run on any terminal windows. So users can write commands and perform related actions. BASH shell is also used to read the commands which are written using shell scripts. BASH is also known as a command language interpreter for Linux operating system. The main purpose of using this BASH is to allow users to interact with the LINUX operating system using commands. This BASH supports various methods like variables, functions, and flow control like any other programming language.
The BASH offers many improvements over SH:
- This provides a command-line editing facility.
- It consists of unlimited size history commands
- This BASH provides the job control
- This also facilitates the Shell functions and Aliases
- It provides the indexed array sets of unlimited size.
- BASH consists of integer arithmetic values which ranges from 2 to 64.
BASH can be downloaded from the official GNU via using both links http://ftp.gnu.org/gnu/bash/) and FTP (ftp://ftp.gnu.org/gnu/bash/) servers.
Shell :
If you are a new Linux user, first you need to open the terminal screen, this terminal contains the shell. This shell allows the user to execute the commands for the interaction purpose. Here users can perform various operations such as the store, retrieve the data, and process information.
If you want to open the terminal-> you need to press the CTRL+ALT+T keys. Here the basic operations included are DATE, CAL, Is, and PWD.
Below is an example for Shell;
Scripting in LINUX:
In Linux, every day we execute the basic four commands they are, date, cal, ls, and PWD.
Linux has come up with a new feature called Scripting which allows users to execute more than one task at a time. Here it’s always good to specify the scripts rather than performing tasks.
For a better understanding of this scripting we are going to explain this with a simple example;
- First specify the script in any task, -> define the script with some tasks.
- To define a script you have to create a file with a .sh extension.
- To perform this, we are using the VI editor. Consider the following command;
vi tasks.sh
The above command opens the vi editor in normal mode. If you want to switch this to insert mode, just press ESC-> then enter the “i” keys-> now enter the desired tasks. The point to be noted here, each task should be specified in a new line. Consider the below task;
Date
Cal
Pwd
Is
Linux Distributions (Distros)
Other operating systems like Windows or MAC combine each bit of code internally and release them as a package. This can be done on the base of the version you are going to use. I can say that Linux is different from other operating systems. As we discussed earlier, the Linux operating system is developed by different organizations. Different parts can be Kernel, shell utilities, system environments, X servers, and Graphical programs, etc. If anyone wants to access the different codes which are assembled correctly to make them work properly.
1.Linux Distribution List part:
Here we can say that there are six hundred Linux distributions used to offer different features. Let me explain a few of the Linux distributions namely;
a. Ubuntu:
This Linux Distribution was developed in the year 2004 by Canonical and has become more popular. Canonical always wanted to use this Ubuntu for graphical design purposes. Here one advantage of using this list is to make the Linux desktop work without the use of any command-line interface. This is one of the popular distribution lists. Ubuntu is the next version of the Debian list and offers easy to use methodologies. This Ubuntu consists of pre-installed applications and easy to use libraries.
Subscribe to our YouTube channel to get new updates..!
b. Linux Mint:
Mint is developed on the base of Ubuntu list and it consists of repository software and packages are similar in both Ubuntu and Linux mint.
Earlier it was used as an alternative to the Ubuntu list and some of the features like media codes, proprietary software components were absent in the Ubuntu distributions. But now it uses the Cinnamon and unity desktop environment to work properly.
c.Debian:
Debian was released in the year 1993 and releases versions that are slower than Ubuntu and Mint. These features make it the most stable Linux distributor list. As I said earlier Ubuntu list is developed on the base of Debian and the main purpose of developing this list was to improve the core bits more quickly and more friendly. Every Debian release name is given on the base of the movie toy story.
d. Red HAT Enterprise or Cent operating system:
This Red HAT is known popularly as a Linux distributor. The most commonly used products are Red hat enterprise LINUX (RHEL) and Fedora which are available free in the market. RHEL was well-tested before its release and that version was supported for almost seven years. Fedora offers faster updates without using any support.
Red HAT distribution list mainly uses the trademark law to make it prevent from being redistributed. Cent Operating system is a community project which uses the Red HAT enterprise Linux system and removes all the Red HAT trademarks and it is freely available. We can also say that this is a free version of Red HAT Enterprise LINUX that offers a stable platform over a long period.
e. Fedora:
Fedora is a simple project developed to focus on the free software versions and also used in the latest Linux software version. One of the drawbacks of using Fedora is that it doesn’t contain its desktop environments and is used as “upstream” software. By default, this contains the GNOME3 desktop setup environment. This distribution list is less stable and offers the latest software stuff.
Linux distribution lists and lists:
The below column will explain the Linux distribution lists and their uses:
Distribution list and its uses:
- Ubuntu: this list works as a MAC operating system and offers easy to use.
- LINUX mint: this list works as a window and it should be used by a newbie.
- Debian: this list provides high level stability but not used by newcomers.
- Fedora: If you want to use RedHAT enterprise and latest software versions by that time make use of it.
- Red hat enterprise: this list can be used for commercial purposes.
- CentOS: this list makes use of the Red HAT enterprise without its trademarks.
- OpenSUSE: this list works the same as fedora list but this is an older list and provides more stability.
- Arch Linux: this list is not for newcomers, here each package should be installed on your own.
Linux set Environment Variables:
The Linux set environment variable is nothing but dynamic values that are stored within a system and mainly used by applications that are launched in Shells or subshells. These types of dynamic variables have names and respected values. The Linux set environment variables are mainly used to customize the system performance and the behavior of an application. These types of environment variables are used to track any computer applications and enable them to interact with various systems. The environment variables consist of information about the default application computer system, the system locale, keyboard layout setting, and path of the executable files.
Below are the lists of few common Environment variables:
1. PATH: This PATH variable contains the list of Linux directories that are later used in system files. This variable separates directories by a (:) colon type.
2. USER: This USER variable holds only USERNAME details.
3. HOME: This variable consists of the default path to the user’s home directory files.
4. EDITOR: The EDITOR variable consists of the PATH detail used to specify the editor.
5. UID: This variable consists of path detail which is used to identify the user’s unique ID.
6. TERM: This variable holds the path to specify the default terminal emulator.
7. SHELL: This type of variable consists of path details is used in the default shell and used by the Linux users.
8. ENV: This variable is used to display all the environment variables.
Linux set command:
Linux set command is used to set and unset flag files and setting within the Linux environment variables. These types of flags and environment settings specify the behavior of the script and help to execute certain tasks without facing any problems. These set commands are used to change and display the shell attributes and parameters.
The syntax is as follows:
Set [options]
List of set Commands:
The following are the various list commands:
-a: this set command is used to mark the variables that are later used to export or modify them for export.
-b: this set command is used to notify the job termination tasks.
-e: This type of set command is used to exit any operation immediately only if a command reaches non-zero status.
-f: It is used to disable the file name generation type or globing.
-h: this type of set command is mainly used to save the command locations.
-k: This is used to place any assignment argument variables in the environment setup, and also precede the command name.
-m: this is used to enable the job controls.
-n: it is used to read commands.
-o: it is used to describe option-name.
-p: this set command is used to disable the “$ENV” processing file and also import the shell functions. –p set command is turned on when the real and effective user ids do not match and terminate it.
-t: it is used to exit from command line arguments after finishing executing any one command.
-u: It is used to treat unset variables as an error distribution when you submit them.
-v: this set command is used to print the shell input files.
-x: this type of set command is used to print the commands and their argument variables in a sequential way.
-B: this is used to perform any brace expansion by the shell command.
-C: This set command is used to disallow existing files to be overwritten by output directories.
-E: it is used if the ERR trap is inherited by the shell functions.
-H: this type of set command is used to enable the style history distribution mechanism. By default, it can be done when the shell is interactive.
-P: this set command is used when you don’t follow any symbolic links while executing the commands.
-T: If the flag is set in a file, the DEBUG trap will be inherited by the shell commands.
Shell Variables:
A shell variable is like a character that holds any value. The values can be of any type such as numbers, texts, filenames, or any data types. We can say that this acts as a pointer to point actual data. The shell commands allow users to create, assign, and delete any variables.
The variables types can have any type of letters, numbers, or underscore characters (_). Here user cannot use characters like “!”, “*”, or “-“. This is because these special characters give other meanings for the Shell commands. Users should always use “UPPERCASE” naming conventions.
Examples for set command:
If you want to turn on the debug information:
Use set –x command
If you want to turn off the debug information:
Use set +x command
To disable any bash’s default behavior;
Use the command set –C
To stop the script immediately, use the following command;
Set –e
If you want to access the Help manual command, you should execute the below command:
Set –help command
Linux files:
If you decided to work with LINUX operating system, then everything will be available in a file format. This file doesn’t contain not only text files, images, and compiled programs but also holds the partitions, hardware drivers, and directories.
Lets Start With Adobe Target Training!
Types of files:
- Regular files (-): this type of file contains programs, executables, and text files.
- Directory files (d): This file is shown in blue color, and holds the list of files.
- Special files:
- Block files (b)
- Character device file (c)
- Named pipe file (p)
- Symbolic link file (l)
- Socket file (s)
Linux file commands:
The below column explains the important Linux file commands:
Command type and description:
- File = This command type determines the file types.
- Touch = this command type is used to create a file.
- Rm = this command is used to remove a file type.
- Cp = this is used to copy a file.
- Mv = this command is used to rename or to remove any file type.
- Rename = which is used to rename any file type.
Difference between UNIX and LINUX:
The following are the important differences between Linux and UNIX operating systems:
LINUX:
- Linux is available in both paid and free distributors.
- Linux operating system uses the GUI which includes an optional command-line interface.
- Linux operating system is portable to use which can be executed in different hard drives.
- Linux is designed by the worldwide Linux social community.
- Linux is a free open source system and users can download it from the internet using GNU licenses.
- Linux can be operated at home-based personal computers, phones, tabs, etc.
UNIX:
- UNIX is available on different paid structures for unique levels of UNIX.
- UNIX included only a command-line interface.
- UNIX is not portable to use.
- UNIX is developed by AT & T.
- Most of the UNIX operating systems are not free.
- UNIX is mainly used in server systems.
Linux Directory commands:
The following are the important Linux directory commands:
PWD: the PWD command stands for PRINT WORKING DIRECTORY. This is used to display the current working location or the user directory. It is a built-in command type.
IS: this type of command is used to specifies the list of a folder. This will list out all types of directed folders.
CD: this CD command stands for Change Directory, and used to change the directory which has been already existed.
MKDIR: with the help of the MKDIR command you can create your directory files.
RMDIR: this RMDIR command is used to remove any directory file from your systems.
Conclusion :
In this blog, I have explained complete LINUX concepts and along with its file directories, commands. As I said earlier Linux is independent and runs on any platform which is distributed under open source management. Learning this tutorial will only help you to get some theoretical knowledge but to get some hands-on experience with the technology; you should join our online course which is offered by HKR Trainings. To know more about the information about the please click here Linux Online Course. We are not only going to give you online training but also help you to clear any type of interviews.
What is Shell?
The shell acts as an interface between the user and the operating system. The shell consists of a single operating system that is Kernel this helps to run the scripts simultaneously. When a user enters the command using keyboards, the shell communicates with the kernel operating system and executes it, then displays the outputs to the users. In other words, we can say that a shell is nothing but a computer program that helps to expose an operating system service to human users or other programming languages. In normal, this type of shell use either a command-line interface or a GUI (graphical user interface), this depends on the particular operations or roles.
4 important concepts to be considered in a Shell:
- With the help of shell users can interact with systems.
- Using the script files several operations can be scripted by placing the respective operations.
- Shell can be differentiated as a featured programming language with script variables, and conditional statements.
- Shell allows users to create any UNIX program like any other programming language.
What do you know about Shell scripting?
Shell scripting is a series or sequence of Linux commands which are written in plain text format. Shell scripting in Linux is used to use of list various Linux commands instead of working with specific jobs or commands. The shell script is a computer interface type of program designed to be work on Linux or UNIX operating system. This is also known as a Command-line interpreter. The various form of shell scripts is now considered as scripting languages. The main purpose of using Shell script in Linux or UNIX includes manipulation of files, printing texts, and program execution.
Why write a Shell script?
The following are the reason behind why we need Shell script;
- Shell scripts take input from users, file system, and output them on the screen.
- With the help of a shell script you can create your commands.
- Time-consuming and helps to automate some daily tasks.
- System administration part can also be automated with the help of the Shell script.
Shell Scripting Architecture overview:
Below diagram explains the overall structure of Linux or Unix Shell scripting:
A Linux/ UNIX shell offers an interface that lets the better user interactions with the operating systems by using command line arguments. But Shell scripting in Linux is also a rich programming language. The methods included are flow control, looping, conditionals, named functions, alternation, basic mathematical operations, and two-way communication between shell architecture and commands. The shell can also be used as a terminal or terminal emulator such as Xterm and also reads the commands from a file. Shell supports bash, command-line editing, emacs, or vi-like commands. The main operations in shell pipeline such as reading the input command from the terminal or a script form, the input data is passed through various stages, transformed to each stage, then shell finally executes a command, and collects the return status.
Why we need Shell scripting command?
Below are a few advantages of using shell scripting:
- Thousands of commands included with Fedoras are available in the form of shell scripts. For example: startx command.
- Shell script can save the user time and typing, suppose if you are using the same command lines multiple times each day.
- A shell program is smaller in size than any compiled program.
- The process of creating and testing your shell script command is also simpler and faster than any development compiling process.
- You can learn hundreds of commands when compare to other compiling programming languages.
How to determine a Shell?
You can name your first shell prompt by using the following command:
Syntax:
Echo $SHELL
For Example:
Where $ sign indicates the shell variable and Echo returns the text what you typed in the command line interpreter.
Shell variables:
A shell variable is a type of character string that is used to assign a value. The assigned values can be available in the form of text, number, device, filename, or any other data type. In simple terms, we can define the variable as a pointer which is used as a pointer to refer to actual data types.
Variable names:
The variable name can contain letters from ( a to z or A to Z), numbers ( 0 to 9), or the underscore characters (_). When you are working with UNIX, shell variables have their names in UPPERCASE only.
For example: (valid variables names)
_ECHO
TOKEN _A
VAR_1
VAR_2
Following are the example for invalid variable names in Shell:
3_VAR
-VARIABLE
VAR1-VAR2
VAR_A!
While working with Linux, you cannot use characters like! , *, or – so they will be considered as an invalid variable.
Defining the variables in Shell:
The syntax is as follows:
Variable_name = variable_value
Example:
NAME = “Kavya”
Shell variables enable you to store any value, for example:
VAR1 = “Kavya”
VAR2 = 200
Accessing values:
To access any value stored in a variable, it should start with a prefix name along with a dollar sign ($);
The following example will explain this:
#! / bin/ sh
NAME = “Kavya”
echo $NAME
The above script will produce the following output:
KAVYA
Variable types:
Below are the types of variables:
1. Local variables:
A local variable is a type of variable present within the shell current instance values. These variables are not available to any shell program. They are usually set at the command prompt.
2. Environment variables:
The environment variables only at the child process level in the shell. Some shell programs make use of these environment variables in order to perform any function correctly.
3. Shell variables:
A shell variable is a special variable type that is used by only shell scripts. Some of the shell variables are environment variables whereas others are local variables.
Special variables:
In the previous section, we have explained about basic variables used in the Shell script. Now it’s time to learn more about special variables, let’s get into it:
$ -> this character represents the ID number used to process any special character of the current shell.
For example:
$echo $$
$0 -> this character represents the current script filename
$n -> these variables represents the arguments. Here n is a positive decimal number and also represents the position of an argument. For example, $1 represents the first argument, $2 represents the second argument, and so on.
$# -> this character supplies the number of arguments to the scripts.
$* -> with the help of this character we can double-quote the important script value.
$@ -> this character helps to make the individual argument double-quoted. Suppose if you get $@ is equivalent to $1 $2.
$? -> This represents the status of the last command argument to be executed.
$$-> this character represents the process number of the current shell.
$! -> This represents the last background command process number.
Programming example:
#!/bin/sh
Echo “file name: $0”
Echo “first parameter: $1”
Echo “second parameter: $2”
Echo “quoted values: $@”
Echo “quoted values: $*”
Echo “total number of parameters used: $#”.
Output:
$./test.sh KAVYA GOWDA
File name: ./test.sh
First parameter: KAVYA
Second parameter: GOWDA
Quoted values: KAVYA GOWDA
Quoted values: KAVYA GOWDA
Total number of parameters: 2
Shell arrays:
Shell script supports various types of special variables they are known as array variables. This type of array consists of multiple values at the same time. Array variable in script offers a grouping of various variable sets. The important purpose of using this array variable is instead of creating a new name for each variable, just use a single array variable to store all kinds of shell variables.
Defining array values:
The following example will explain how you define the array values,
NAME01 = “KAVYA”
NAME02 = “GOWDA”
NAME03 = “RAKSHITH”
NAME04 = “INDIA”
NAME05 = “BANGALORE”
We can use a single array value used to store the mentioned names. The following method will explain this;
Array_name [index] = value
Here array_name is the name of the array value, the index is the index of the item in the array set, and value is the name of the value which you want to set for the items.
Use the following command to set the array values:
NAME [0] = “KAVYA”
NAME [1] = “GOWDA”
NAME [2] = “RAKSHITH”
NAME [3] = “BANGALORE”
NAME [4] = “INDIA”
Accessing Array Values:
Once you are done with defining array variables, you access them as follows:
${array-name [index]}
Here array_name is the name of the array, and index is the index of the value to be accessed. Following is an example to access the array values:
#! /bin/sh
NAME [0] = “KAVYA”
NAME [1] = “GOWDA”
NAME [2] = “RAKSHITH”
NAME [3] = “BANGALORE”
NAME [4] = “INDIA”
Echo “first index: ${NAME [0]}”
Echo “second index: ${NAME [1]}”
The above example generates the following result:
$. /test.sh
First Index: KAVYA
Second Index: GOWDA
You can also access the items in this way:
$ {array_name [*]}
${array_name [@]}
Shell basic operators:
There are various operators supported by a shell interpreter. The following are the basic operators of Shell.
- Arithmetic operators
- Relational operators
- Boolean operators
- String operators
- File test operators
First, let me start with
Arithmetic operators:
The following are the different types of arithmetic operators:
+ (addition): this operator is used for the addition of any variables.
- (subtraction): this operator is used to find the differences between any two variables.
*(multiplication): multiplies values on either side of the operators.
/ (Division): this operator divides the left-hand operand by the right hand operands.
%( Modulus): this operator is used to divides the left-hand operands by any right hand operand and return the remainder.
= (Assignment): this operator is used to assign the right hand operand is left operands.
== (Equality): this operator compares any two numbers, if both are the same then returns the true value.
!= (Not equality): compares any two numbers, if both are different then return the true value.
Relational operators:
The following are the different types of relational operators used:
-eq: this operator checks if the two operand values are equal or not, if yes, then the condition becomes true.
-ne: this operator checks if the value of any two operands are equal or not, if the given values are not equal, then the condition becomes true.
-gt: this operator is used to check if the left operand value is greater than the value of right-hand side operand, if yes then the condition becomes true.
-lt: this operator is used to check if the left operand value is lesser than the value of the right hand side operand, if yes then the condition becomes true.
-ge: this operator checks if the value of the left operand is greater than or equal to the right operand value, if yes, then the condition becomes true.
-le: this operator checks if the value of the left operand is less than or equal to the right operand value, if yes, then the condition becomes true.
Boolean operators:
The following are the different types of Boolean operators supported by Shell:
!: This is also known as logical negation. This operator converts a true condition into false, and vice versa.
-o: this is a logical OR operator. If one of the operand values is true, then the condition becomes true. Then the given condition becomes true.
-a: this is a logical AND operator. If both the operands are true, then the given condition becomes true otherwise false.
String operators:
The following are important types of string operators supported by Shell:
=: this operator checks if the given two operand values are equal or not, if yes, then the given condition becomes true.
!=: this operator checks if the value of any two operators are equal or not, if the given values are not equal then the given condition.
-z: this string operator checks if the given string operand is zero, if it is in zero-length, then the condition becomes true.
-n: this string operator checks, if the given string operand is non-zero, if it is in non-zero length, then this operator returns true.
Str: this operator checks if the str is a non-empty value, if the given string value is empty, then it returns false.
File test operators:
The following are the important file test operators by the shell:
-b file: this operator checks if the file blocks any specified file type, then the condition becomes true.
-c file: this operator checks if the file is a special character, then the given condition becomes true.
-d: this operator checks if the file is a directory, then the condition becomes true.
-f file: this operator is used to check if the file is an ordinary type, then the given condition becomes true.
-g file: this file operator checks, if the file has its own set of groups if, if yes, then the condition becomes true.
-k file: this operator checks if the file has its sticky bit type, if yes, then the given condition becomes true.
-t file: this operator checks if the file descriptor type is open and associated with any terminal, then the given condition becomes true.
-u file: this operator checks if the file has its own set User ID, if yes then the condition becomes true.
-r file: this operator checks if the file is readable or not, if yes then the condition becomes true.
Shell decision making:
Unix/Linux supports two types of conditional statements, which are used to perform various actions based on the conditions. Now we are going to discuss two types of decision making:
- The if …. Else statements
- The case …. Esac statement
First, start with if ….. Else statements:
These condition statements are useful decision-making statements, which are useful to select any option from the given set of options.
In the shell, you can see a different type of if….else statements:
- If ….fi statement
- If….else…fi statement
- If….elif...else…fi…statement
Most of the if conditional statements use the relational operators.
The case….esac statement:
Here you can use multiple if….elif statements used to perform multiple branch operations. However, sometimes, this type of condition statement is not always the best, when a branch depends on the value of any single variable.
To overcome this problem, UNIX has come up with a case….esac conditional statement, and it works better than if….elif statements.
The case…esac statement in the UNIX is similar to the switch…case statement which is used in many programming languages like C, C++, And PERL, etc.
Shell looping types:
Till now I have explained what types of conditional statements are used in Shell, now it’s time to learn about looping statements.
There are 4 types of loops available based on the situations:
- While loops
- The for loops
- The until loops
- The select loops
We already know about While and for loops, the while loop executes the statements until the condition is true, if the given condition fails, then it terminates the loop. The while and for loops are available in most of the other well-known programming languages like C, C++, and PERL, etc.
Nesting loop:
All the basic loops support the nesting concepts; nesting means you can put multiple loops inside (similar or different ones). The nesting can be used an unlimited number of times as per your requirements.
Below is an example for Nesting while loops:
While command1; /./ this one is for outer loop
Do
Statement (s) to be executed if the command1 condition is true
While command2; // this one is for loop2, the inner loop command
Do
Statement (s) to be executed if the command2 condition is true
Done
Statement (s) to be executed if the command1 condition is true
Done
Shell loop controls:
In this section, you will be learning important two statements that are used to control shell loops:
- The break statement
- The continue statement
First start with continue or infinite loop statement:
As we know that all the loops have a limited life span and they come out of the loop if the given condition is false or depends on the loop.
This type of loop statement may continue if the required condition is not met the given condition. A loop executes forever without terminates for an infinite number of times. This type of loop statement is known as an infinite or continues the loop.
Example:
#! / bin/ sh
a = 20
until [$a -lt 20]
do
echo $a
a = expr $a + 1
done
The loop continues forever because a is always greater than or equal to 20 and it is never less than 20.
The break statements:
The break statement in the shell is used to terminate the execution of the whole loop once finishes the execution of all line codes executed successfully. If that does not meet the given condition then terminates the program.
Syntax:
The following statement is used to come out of the loop:
break
The following statement is used for the nested loop condition:
break n
Where n specifies the nth enclosing loop which is an exit from.
Example:
#! /bin/sh
a = 0
while [$a -lt 10]
do
echo $a
If [$a -eq 6]
then
break
fi
a = expr $a + 1
done
Output:
0
1
2
3
4
5
6
The continue statement:
You can say that the continue statement is similar to the break command, and this continue statement causes the current loop iterations to exit, rather than the whole loop. This continue statement is useful when an error has occurred.
Syntax:
Continue
To execute n number of iteration loop the syntax is as follows:
Continue n
The programming example is as follows:
#!/bin/sh
NUMS = “1 2 3 4 5 6 7 8”
For NUM in $NUMS
do
Q = ‘expr $NUM % 2’
If [$Q –eq 0]
Then
Echo “number is an even number!!”
Continue
Fi
echo “found odd number in the result”
Done
Output:
Found odd number in the result
Number is an even number!!
Found odd number in the result
Number is an even number!!
Found odd number in the result
number is an even number!!
Shell substitution:
When the shell program encounters an expression by that time substitution can be used. This substitution contains one or more special characters.
The following are the few special characters that are supported by the shell:
\\: backlash
\a: alert
\b: backspace
\c: suppress trailing newline character
\f: form feed
\n: new line
\r: carriage return
\t: horizontal tab
\v: vertical tab
Variable substitution:
Variable substitution helps shell programmers to manipulate the variable value on the basis of the state.
The following are a few examples for variable substitutions:
${var}: substitute the value of var or variable
${var: word} = if the var is null or unset condition, then the word is substituted for var. Here the values of var never change.
${var:? message} = if the var is the null or unset condition, then the message ids printed to the standard error. This also checks for the variables are set correctly or not.
${var:+word} = if the variable is set, the word is substituted for var. The value of var does not change in the program.
Shell files systems:
A file system is nothing but logical collections of various files or disks. Everything in UNIX is considered to be a file, for example, DVD-ROMs, USB devices, and any other floppy devices.
The following are the examples for a file system:
/ = this indicates the root directory which contains only directories at the top level.
/bin = this expression indicates the executable files are located. These types of files are available to all users.
/dev = this indicates the device drivers.
/etc = this contains supervisor directory commands, disk configuration files, valid user lists, and Ethernets.
/lib = contains shared library files and also kernel-related files.
/boot = this contains boot file system.
/usr = this files type for miscellaneous purpose, and used by many users, for example administrative commands, library files, shared files, and others.
/kernel = consists only kernel file types.
Conclusion :
In this shell scripting tutorial, we have explained the definitions, architectures, conditional statements, decision makings, and file system types. From this tutorial, you will gain more knowledge related to UNIX OR Linux shell script program. To become a master in this technology, you must take any course related to LINUX or UNIX. What you are waiting for, let’s begin your shell script programmer journey.
About Author
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.
Upcoming Linux Certification Training Online classes
Batch starts on 25th Nov 2024 |
|
||
Batch starts on 29th Nov 2024 |
|
||
Batch starts on 3rd Dec 2024 |
|