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 () { #code to execute } or function function_name { #code to execute } 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 function_name : The name of the function. Curly braces {} : Enclose the code block. Functions are declared before being called.   Passing Arguments I...

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...

Writing a Bash Shell Script

Bash shell scripts in Linux are text files that contain a series of commands that can be executed by the Bash shell. Bash (Bourne Again Shell) is a popular shell in Linux and UNIX systems, and shell scripts are used to automate tasks, configure systems, or perform a sequence of operations. How to Write a Bash Shell Script Create a New File: You can create a new script using any text editor like nano , vim , or gedit . gedit myscript.sh Write the Script: A basic shell script begins with a "shebang" ( #!/bin/bash ) to specify the interpreter that will be used to execute the script. The rest of the file contains the commands to be run. Example of a simple script: #!/bin/bash # This is a comment echo "Hello, World!" # Print "Hello, World!" #!/bin/bash : Specifies that the script will be executed using the Bash shell. echo "Hello, World!" : A command that prints the string "Hello, World!" to the terminal. Comments: Any line starting ...

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

Linux organizes its file system in a hierarchical structure, starting from the root directory ( / ) . Each directory serves a specific purpose, ensuring a clear and organized layout. Here’s an overview of the most important directories in the Linux file system: Important Directories in Linux / (Root Directory) The topmost directory in the Linux file system. All other directories and files stem from this directory. Accessible only by the root user for critical modifications. /bin (Binary Files) Contains essential binary executables used by all users. Examples include common commands like ls , cat , cp , mv , etc. These binaries are available even in single-user mode (emergency mode). /boot (Boot Loader Files) Stores files needed to boot the operating system, including: Kernel files ( vmlinuz ). Boot loader configurations (e.g., GRUB files). Example: /boot/grub/grub.cfg . /dev (Device Files) Contains device files representing hardware devices (e.g., disks, printers, terminals). Examp...

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...