Skip to main content

Posts

Showing posts from 2018

String Hadling in Bash Shell

Strings can be handled efficiently using string functions in bash shell. The following are the commonly used functions Finding length of the string length: will return the length of the string Eg: str=”This is a test string” len=`expr length “$str”` echo $len o/p:21 In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion.   Parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. We can use the parameter expansion concept for string manipulation operations.   Syntax:${#string} Eg: str="This is a test string" len=${#str} echo $len Extracting a substring substr Eg: this will extract 3 characters from 2 pos ie; his str="This is a test string" sstr=`expr substr "$str" 2 3` echo $sstr In the parameter expansion position starts from 0.

Functions in Bash shell

Functions in Bash Scripting are a great way to reuse code. In this section you'll learn how they work and what you can do with them. Think of a function as a small script within a script. It's a small chunk of code which you may call multiple times within your script. They are particularly useful if you have certain tasks which need to be performed several times. Instead of writing out the same code over and over you may write it once in a function then call that function every time. Creating a function is fairly easy. They may be written in two different formats:  function_name () { } or function function_name { } The function can be called with just function_name Example1: f() { echo “This is a test function...f” } f Example2: function f { echo “This is a test function...f” } f Passing Arguments It is often the case that we would like the function to process some data for us. We may send data to the function in a similar way to passing co

Flow Control in shell script - if and case statements

Shell script usually consist of sequence of commands that starts at the first line and continues line by line until it reaches the end. Most programs do much more than this. They make decisions and perform different actions depending on conditions. The shell provides several commands that we can use to control the flow of execution in our program. In this lesson, we will look at the following:  test if case exit   test The test command is used most often with the if command to perform true/false decisions. The command is unusual in that it has two different syntactic forms: # First form test expression # Second form [ expression ] The test command works simply. If the given expression is true, test exits with a status of zero; otherwise it exits with a status of 1. Common numerical test conditions -gt   Greater than -lt    Less than -ge  Greater than or equal to -le    Less than or equal to -eq   Equal to -ne   Not equal to -a AND -o OR Files and strings can also be

Writing a Bash Shell Script

What Are Shell Scripts? In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line. The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line. Writing Your First Script And Getting It To Work To successfully write a shell script, you have to do three things: Write a script Give the shell permission to execute it Put it somewhere the shell can find it Writing A Script A shell script is a file that contains ASCII text. To create a shell script, you use a text editor . A text editor is a program, like a word processor, that reads and writes ASCII text files. There are

I/O Redirection

In this we will explore a powerful feature used by many command line programs called input/output redirection . As we have seen, many commands such as ls print their output on the display. This does not have to be the case, however. By using some special notations we can redirect the output of many commands to files, devices, and even to the input of other commands. Standard Output Most command line programs that display their results do so by sending their results to a facility called standard output . By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this: $ ls > file_list.txt In this example, the ls command is executed and the results are written in a file named file_list.txt. Since the output of ls was redirected to the file, no results appear on the display. Each time the command above is repeated, file_list.txt is overwritten from the beginning with the output

Important Directories in Linux File System

Directory Description / The root directory where the file system begins. In most cases the root directory only contains subdirectories. /boot This is where the Linux kernel and boot loader files are kept. The kernel is a file called vmlinuz . /etc The /etc directory contains the configuration files for the system. All of the files in /etc should be text files. Points of interest: /etc/passwd The passwd file contains the essential information for each user. It is here that users are defined. /etc/fstab The fstab file contains a table of devices that get mounted when your system boots. This file defines your disk drives. /etc/hosts This file lists the network host names and IP addresses that are intrinsically known to the system. /etc/init.d This directory contains the scripts that start various system services typically at boot time.

Basic Linux Commands For Beginner's

Basic Linux Commands for Beginners Linux is an Operating System’s Kernel. You might have heard of UNIX. Well, Linux is a UNIX clone. But it was actually created by Linus Torvalds from Scratch. Linux is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name! There are several Linux Distributions, commonly called “distros”. A few of them are: Mint Ubuntu Linux Red Hat Enterprise Linux Debian Fedora Kali Linux is Mainly used in Servers. About 90% of the Internet is powered by Linux Servers. This is because Linux is fast, secure, and free! The main problem of using Windows Servers are their cost. This is solved by using Linux Servers. Forgot to mention, the OS that runs in about 80% of the Smartphones in the World, Android, is also made from the Linux Kernel. Yes, Linux is amazing! A simple example of its security is that most of the viruses in the world run on Windows, but not on Linux