Shell Scripting https://www.linuxtechi.com Sat, 15 Oct 2022 04:56:23 +0000 en-US hourly 1 https://www.linuxtechi.com/wp-content/uploads/2020/02/cropped-linuxtechi-favicon-32x32.png Shell Scripting https://www.linuxtechi.com 32 32 How to Get Started with Shell Scripting in Linux https://www.linuxtechi.com/get-started-shell-scripting-linux/ https://www.linuxtechi.com/get-started-shell-scripting-linux/#respond Sat, 15 Oct 2022 03:38:22 +0000 http://www.linuxtechi.com/?p=1904 Hello readers, In this post, we will cover how to get started with shell scripting in Linux or UNIX systems. What is a Shell? A shell is an interpreter in UNIX/Linux like operating systems. It takes commands typed by the user and calls the operating ... Read more

The post How to Get Started with Shell Scripting in Linux first appeared on LinuxTechi.]]>
Hello readers, In this post, we will cover how to get started with shell scripting in Linux or UNIX systems.

What is a Shell?

A shell is an interpreter in UNIX/Linux like operating systems. It takes commands typed by the user and calls the operating system to run those commands. In simple terms a shell acts as form of wrapper around the OS. For example , you may use the shell to enter a command to list the files in a directory , such as ls command , or a command to copy ,such as cp.

$ ls
Desktop Documents Downloads Music Pictures playbook.yaml Public snap Templates test5 Videos
$

In this example , when you simply type ls and press enter . The $ is the shell prompt , which tells you the the shell awaits your commands.The remaining lines are the names of the files in the current directory.

What is Shell Prompt?

The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

What are different types of Shells

since there is no monopoly of shells , you are free to run any shell as you wish. That’s all well and good , but choosing a shell without knowing the alternative is not very helpful. Below are lists of shells available in UNIX/Linux.

The Bourne Shell

The Original Unix Shell is known as sh , short for shell or the Bourne shell , named for steven Bourne , the creator of sh. This is available on almost all the UNIX like operating system. The Basic bourne shell supports only the most limited command line editing, You can type the Characters,remove characters one at a time with the Backspace key and Press enter to execute the command. If command line gets messed up , you can press Ctrl-C to cancel the whole command.

The C Shell

It is desgined by Bill Joy at the university of california at Berkeley , the C shell was so named because much of its syntax parallels that of C programming language. This shell adds some neat features to the Bourne shell,especially the ability to recall previous commands to help create future commands.Because it is very likely you will need to execute more than one command to perform a particular task,this C shell capability is very useful.

The Korn Shell

It is created by David Korn at AT&T Bell laboratories , the korn shell or ksh offers the same kind of enhancements offers by the C Shell , with one important difference: The korn shell is backward compatible with the older Bourne shell Synatx. In UNIX like AIX & HP-UX korn shell is the default shell.

Bash (The Bourne Again Shell)

Bash offers command-line editing like the korn shell,file name completion like the C shell and a lot of other advance features. Many Users view bash as having the best of the Korn and C shells in one shell. In Linux and Mac OS X system , bash is the default shell.

tcsh ( The T C Shell)

Linux systems popularized the T C shell ot Tcsh. Tcsh extends the traditional csh to add command line editing,file name completion and more. For example , tcsh will complete the file and directory names when you press Tab key(the same key used in bash). The older C shell did not support this feature.

What is a Shell Script?

A Shell Script is a text file that contains one or more commands. In a shell script, the shell assumes each line of text file holds a separate command. These Commands appear for most parts as if you have typed them in at a shell windows.

Why to use Shell Script ?

Shell scripts are used to automate administrative tasks,encapsulate complex configuration details and get at the full power of the operating system.The ability to combine commands allows you to create new commands ,thereby adding value to your operating system.Furthermore ,combining a shell with graphical desktop environment allows you to get the best of both worlds.

In Linux system admin profile, day to day repeated tasks can be automated using shell script which saves time and allow admins to work on quality work.

Creating first shell script

Create a text file in your current  working directory with a name myscript.sh , all the shell scripts have an “.sh” extension. First line of a shell script is either #!/bin/sh or #!/bin/bash , it is known as shebang because # symbol is called hash and ! Symbol is called a bang. Where as /bin/sh & /bin/bash shows that commands to be executed either sh or bash shell.

Below are the content of  myscript.sh

#!/bin/bash
# Written by LinuxTechi
echo
echo "Current Working Directory: $(pwd)"
echo
echo "Today' Date & Time: $(date)"
DISK=$(df -Th)
echo
echo "Disk Space on System:"
echo "$DISK"

Above shell script will display the current working , today’s date & time along with file system disk space. We have used echo command and other linux commands to build this script.

Assign the executable permissions using below chmod command

$ chmod a+x myscript.sh

Now execute the script.

$ sh myscript.sh
or
$ ./myscript.sh

Note: To execute any shell script available in current directory, use  ./<script-name> as shown below,

output,

shell-script-contents-linux

Taking Input from the user in shell script

Read command is used to take inputs from user via keyboard and assign the value to a variable. echo command is used to display the contents.

Let’s modify above script so that it starts taking input,

#!/bin/bash
# Written by LinuxTechi
read -p "Your Name: " NAME
echo
echo "Today' Date & Time: $(date)"
echo
read -p "Enter the file system:" DISK
echo "$(df -Th $DISK)"

Now, try to execute the script this time it should prompt to enter details.

$ ./myscript.sh
Your Name: Pradeep Kumar

Today' Date & Time: Sat 15 Oct 05:32:38 BST 2022

Enter the file system:/mnt/data
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/volgrp01-lv01 ext4 14G 24K 13G 1% /mnt/data
$

Input-Read-Shell-Script-Examples

Perfect, above output confirms that scripting is prompting for input and processing data.

That’s conclude the post. I hope you have found it informative. Kindly do post your queries and feedback in below comments section.

Read Also: How to Debug a Bash Shell Script in Linux

The post How to Get Started with Shell Scripting in Linux first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/get-started-shell-scripting-linux/feed/ 0
How to Use Encrypted Password in Linux Bash Shell Script https://www.linuxtechi.com/encrypted-password-bash-shell-script/ https://www.linuxtechi.com/encrypted-password-bash-shell-script/#comments Wed, 09 Jun 2021 03:29:13 +0000 https://www.linuxtechi.com/?p=12592 It is always recommended to use encrypted passwords in Linux bash shell scripts. Typically, in bash shell script we may need password for remote user while connecting to remote system, ftp user and proxy user etc. In this article, we will cover how to encrypt ... Read more

The post How to Use Encrypted Password in Linux Bash Shell Script first appeared on LinuxTechi.]]>
It is always recommended to use encrypted passwords in Linux bash shell scripts. Typically, in bash shell script we may need password for remote user while connecting to remote system, ftp user and proxy user etc. In this article, we will cover how to encrypt password using openssl command and then will see how this encrypted password can be used in bash shell script.

Encrypt Password Using Openssl

Let’s assume we want to connect to remote system over ssh using password inside a shell script. To encrypt a password, use below openssl command in your linux system.

$ echo "Passw0rD@#2" | openssl enc -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 \ 
-salt -pass pass:Secret@123#

Note: String followed by echo command ‘Passw0rD@#2’ is the password string that we want to encrypt it and ‘Secret@123#’ is the password that is used during the encryption. If the openssl version is 1.1.0 or less then skip these two options ‘-pbkdf2 -iter 100000

To save the encrypted password to a file use the following command,

$ echo "Passw0rD@#2" | openssl enc -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 \
-salt -pass pass:Secret@123# > secret.txt

Set the following permissions on secret.txt file using chmod command,

$ chmod 600 secret.txt

Decrypt Encrypted Password Using Openssl

To decrypt the password, run below

$ cat secret.txt | openssl enc -aes-256-cbc -md sha512 -a -d -pbkdf2 -iter 100000 \
 -salt -pass pass:Secret@123#
Passw0rD@#2
$

Note: If you have noticed carefully, we have used ‘-d’ option to decrypt.

Use Encrypted Password in Bash Shell Script

Use the below sample shell script which will use encrypted password while connecting to remote system over ssh.

$ vi sample.sh
#!/bin/bash
USERNAME=devops
PASSWD=`cat secret.txt | openssl enc -aes-256-cbc -md sha512 -a -d -pbkdf2 \ 
-iter 100000 -salt -pass pass:Secret@123#`

REMOTE=10.20.0.20

sshpass -p $PASSWD ssh -o StrictHostKeyChecking=no $USERNAME@$REMOTE \
 'dmesg -Tx | grep -i error' > /tmp/a.tmp

save and close the file.

Make the script executable by running beneath command,

$ chmod +x sample.sh

Now run the script to verify whether encrypted is successfully used to connect to remote system.

[devops@host1 ~]$ ./sample.sh
Or
[devops@host1 ~]$ bash -x sample.sh
+ USERNAME=devops
++ openssl enc -aes-256-cbc -md sha512 -a -d -pbkdf2 -iter 100000 -salt \ 
-pass pass:Secret@123#
++ cat secret.txt
+ PASSWD=Passw0rD@#2
+ REMOTE=10.20.0.20
+ sshpass -p Passw0rD@#2 ssh -o StrictHostKeyChecking=no devops@10.20.0.20 \ 
'dmesg -Tx | grep -i error'

Perfect, above output confirms that encrypted is decrypted during the execution.

Let’s verify the contents of /tmp/a.tmp file,

[devops@host1 ~]$ cat /tmp/a.tmp
kern  :info  : [Thu Jun  3 13:36:51 2021] RAS: Correctable Errors collector\
 initialized.
kern  :err   : [Thu Jun  3 13:36:53 2021] [drm:vmw_host_log [vmwgfx]] *ERROR*\ 
 Failed to send log
kern  :err   : [Thu Jun  3 13:36:53 2021] [drm:vmw_host_log [vmwgfx]] *ERROR* \
 Failed to send log
[devops@host1 ~]$

Above output confirms that script is able to capture output of dmesg command. That’s all from article. I hope you got an idea how we can use encrypted password inside a shell script.

The post How to Use Encrypted Password in Linux Bash Shell Script first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/encrypted-password-bash-shell-script/feed/ 6
Learn and use fork(), vfork(), wait() and exec() system calls across Linux Systems https://www.linuxtechi.com/learn-use-fork-vfork-wait-exec-system-calls-linux/ https://www.linuxtechi.com/learn-use-fork-vfork-wait-exec-system-calls-linux/#comments Wed, 27 Feb 2019 04:13:37 +0000 https://www.linuxtechi.com/?p=8247 It is found that in any Linux/Unix based Operating Systems it is good to understand fork and vfork system calls, how they behave, how we can use them and differences between them. Along with these wait and exec system calls are used  for process spawning ... Read more

The post Learn and use fork(), vfork(), wait() and exec() system calls across Linux Systems first appeared on LinuxTechi.]]>
It is found that in any Linux/Unix based Operating Systems it is good to understand fork and vfork system calls, how they behave, how we can use them and differences between them. Along with these wait and exec system calls are used  for process spawning and various other related tasks.

Learn-fork-vfork-wait-exit-linux-systems

Most of these concepts are explained using programming examples. In this article, I will be covering what are fork, vfork, exec and wait system calls, their distinguishing characters and how they can be better used.

fork()

fork(): System call to create a child process.

shashi@linuxtechi ~}$ man fork

This will yield output mentioning what is fork used for, syntax and along with all the required details.

The syntax used for the fork system call is as below,

pid_t fork(void);

Fork system call creates a child that differs from its parent process only in pid(process ID) and ppid(parent process ID). Resource utilization is set to zero. File locks and pending signals are not inherited. (In Linux “fork” is implemented as “copy-on-write()“).

Note:-Copy on write” -> Whenever a fork() system call is called, a copy of all the pages(memory) related to the parent process is created and loaded into a separate memory location by the Operating System for the child process. But this is not needed in all cases and may be required only when some process writes to this address space or memory area, then only separate copy is created/provided.

Return values:-  PID (process ID) of the child process is returned in parents thread of execution and “zero” is returned in child’s thread of execution. Following is the c-programming example which explains how fork system call works.

shashi@linuxtechi ~}$ vim 1_fork.c
#include<stdio.h>
#include<unistd.h>
Int main(void)
{
printf("Before fork\n");
fork();
printf("after fork\n");
}
shashi@linuxtechi ~}$ 
shashi@linuxtechi ~}$ cc 1_fork.c
shashi@linuxtechi ~}$ ./a.out
Before fork
After fork
shashi@linuxtechi ~}$

Whenever any system call is made there are plenty of things that take place behind the scene in any unix/linux machines.

First of all context switch happens from user mode to kernel(system) mode. This is based on the process priority and unix/linux operating system that we are using. In the above C example code we are using  “{” opening curly brace which is the entry of the context and “}” closing curly brace is for exiting the context. The following table explains context switching very clearly.

Fork-vfork-system-calls-linux

vfork()

vfork –> create a child process and block parent process.

Note:- In vfork, signal handlers are inherited but not shared.

shashi@linuxtechi ~}$ man vfork

This will yield output mentioning what is vfork used for, syntax and along with all the required details.

pid_t vfork(void);

vfork is as same as fork except that behavior is undefined if process created by vfork either modifies any data other than a variable of type pid_t used to store the return value p of vfork or calls any other function between calling _exit() or one of the exec() family.

Note: vfork is sometimes referred to as special case of clone.

Following is the C programming example for vfork() how it works.

shashi@linuxtechi ~}$ vim 1.vfork.c
#include<stdio.h>
#include<unistd.h>
Int main(void)
{
printf("Before fork\n");
vfork();
printf("after fork\n");
}
shashi@linuxtechi ~}$ vim 1.vfork.c
shashi@linuxtechi ~}$ cc 1.vfork.c
shashi@linuxtechi ~}$ ./a.out
Before vfork
after vfork
after vfork
a.out: cxa_atexit.c:100: __new_exitfn: Assertion `l != NULL' failed.
Aborted

Note:– As explained earlier, many a times the behaviour of the vfork system call is not predictable. As in the above case it had printed before once and after twice but aborted the call with _exit() function. It is better to use fork system call unless otherwise and avoid using vfork as much as possible.

Differences between fork() and vfork()

Difference-between-fork-vfork

Vfork() behaviour explained in more details in the below program.

shashi@linuxtechi ~}$ cat vfork_advanced.c
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
    int n =10;
    pid_t pid = vfork(); //creating the child process
    if (pid == 0)          //if this is a chile process
    {
        printf("Child process started\n");
    }
    else//parent process execution
    {
        printf("Now i am coming back to parent process\n");
    }
    printf("value of n: %d \n",n); //sample printing to check "n" value
    return 0;
}
shashi@linuxtechi ~}$ cc vfork_advanced.c
shashi@linuxtechi ~}$ ./a.out
Child process started
value of n: 10
Now i am coming back to parent process
value of n: 594325573
a.out: cxa_atexit.c:100: __new_exitfn: Assertion `l != NULL' failed.
Aborted

Note: Again if you observe the outcome of vfork is not defined. Value of “n” has been printed first time as 10, which is expected. But the next time in the parent process it has printed some garbage value.

wait()

wait() system call suspends execution of current process until a child has exited or until a signal has delivered whose action is to terminate the current process or call signal handler.

pid_t wait(int * status);

There are other system calls related to wait as below,

1) waitpid(): suspends execution of current process until a child as specified by pid arguments has exited or until a signal is delivered.

pid_t waitpid (pid_t pid, int *status, int options);

2) wait3(): Suspends execution of current process until a child has exited or until signal is delivered.

pid_t wait3(int *status, int options, struct rusage *rusage);

3) wait4(): As same as wait3() but includes pid_t pid value.

pid_t wait3(pid_t pid, int *status, int options, struct rusage *rusage);

exec()

exec() family of  functions or sys calls replaces current process image with new process image.

There are functions like execl, execlp,execle,execv, execvp and execvpe are used to execute a file.

These functions are combinations of array of pointers to null terminated strings that represent the argument list , this will have path variable with some environment variable combinations.

exit()

This function is used for normal process termination. The status of the process is captured for future reference. There are other similar functions exit(3) and _exit()., which are used based on the exiting process that one is interested to use or capture.

Conclusion:-

The combinations of all these system calls/functions are used for process creation, execution and modification. Also these are called “shell” spawning set-of functions. One has to use these functions cautiously keeping in mind the outcome and behaviour.

The post Learn and use fork(), vfork(), wait() and exec() system calls across Linux Systems first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/learn-use-fork-vfork-wait-exec-system-calls-linux/feed/ 2
How to define and use functions in Linux Shell Script https://www.linuxtechi.com/define-use-functions-linux-shell-script/ https://www.linuxtechi.com/define-use-functions-linux-shell-script/#comments Sun, 19 Aug 2018 14:03:02 +0000 https://www.linuxtechi.com/?p=7909 Function is a reusable block of code. Often we put repeated code in a function and call that function from various places. Library is a collection of functions. We can define commonly used function in a library and other scripts can use them without duplicating ... Read more

The post How to define and use functions in Linux Shell Script first appeared on LinuxTechi.]]>
Function is a reusable block of code. Often we put repeated code in a function and call that function from various places. Library is a collection of functions. We can define commonly used function in a library and other scripts can use them without duplicating code.

In this article we’ll discuss more about functions and recipes. For demonstration purpose I’ll be using Bourne Again SHell (Bash) on Ubuntu machine.

Calling function

In Shell calling function is exactly same as calling any other command. For instance, if your function name is my_func then it can be execute as follows:

$ my_func

If any function accepts arguments then those can be provided from command line as follows:

$ my_func arg1 arg2 arg3

Defining function

We can use below syntax to define function:

 function function_name {
            Body of function
 }

Body of function can contain any valid command, loop constrain, other function or script. Now let us create simple function which displays message on screen.

 function print_msg {
       echo "Hello, World"
 }

Now let us execute this function:

 $ print_msg
 Hello, World

As expected, this function displays message on screen.

In above example we have created function directly on terminal. We can store this function in file as well. Below example demonstrates this.

 #! /bin/bash
 function print_msg {
       echo "Hello, World"
 }
 print_msg

We have defined this function inside function.sh file. Now let us execute this script:

 $ chmod +x function.sh
 $ ./function.sh
 Hello, World

If you observe, above output is exactly identical to previous one.

More about functions

In previous section we have defined very basic function. However during software development we need more advanced functions which can accept various parameters and return values. In this section we’ll discuss such functions.

Passing arguments to function

We can provide arguments to function same as other commands. We can access these arguments from function using dollar($) symbol. For instance, $1 represents first argument, $2 represents second argument and so on.

Let us modify above function to accept message as an argument. Our modified function will look like this:

 function print_msg {
       echo "Hello $1"
 }

In above function we are accessing first argument using $1. Let us execute this function:

 $ print_msg "LinuxTechi"

When you execute this function, it will generate following output:

 Hello LinuxTechi

Returning value from function

Like other programming languages, Bash provides return statement using that we can return value to the caller. Let us understand this with example:

function func_return_value {
      return 10
 }

Above function returns value 10 to its caller. Let us execute this function:

 $ func_return_value
 $ echo "Value returned by function is: $?"

When you execute above function, it will generate following output:

 Value returned by function is: 10

NOTE: In bash we have to use $? to capture return value of function

Function recipes

So far we got fair idea about bash functions. Now let us create some useful bash functions which can be used to make our lives easier.

Logger

Let us create logger function which will print date and time along with log message.

 function log_msg {
        echo "[`date '+ %F %T'` ]: $@"
 }

Let us execute this function:

 $ log_msg "This is sample log message"

When you execute this function, it will generate following output:

 [ 2018-08-16 19:56:34 ]: This is sample log message

Display system information

Let us create a function to display information about GNU/Linux system

 function system_info {
       echo "### OS information ###"
       lsb_release -a

       echo
       echo "### Processor information ###"
       processor=`grep -wc "processor" /proc/cpuinfo`
       model=`grep -w "model name" /proc/cpuinfo  | awk -F: '{print $2}'`
       echo "Processor = $processor"
       echo "Model     = $model"

       echo
       echo "### Memory information ###"
       total=`grep -w "MemTotal" /proc/meminfo | awk '{print $2}'`
       free=`grep -w "MemFree" /proc/meminfo | awk '{print $2}'`
       echo "Total memory: $total kB"
       echo "Free memory : $free kB"
 }

When you execute above function it will generate following output:

### OS information ###
No LSB modules are available.
Distributor ID:           Ubuntu
Description:   Ubuntu 18.04.1 LTS
Release:         18.04
Codename:    bionic

### Processor information ###
Processor = 1
Model     =  Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

### Memory information ###
Total memory: 4015648 kB
Free memory : 2915428 kB

Find file or directory from current directory

Below function searches file or directory from current directory:

 function search {
      find . -name $1
 }

Let us search directory namely dir4 using below command:

 $ search dir4

When you execute above command, it will generate following output:

 ./dir1/dir2/dir3/dir4

Digital clock

Below function creates a simple digital clock on terminal

 function digital_clock {
            clear
            while [ 1 ]
            do
                  date +'%T'
                  sleep 1
                  clear
            done
 }

Creating library

Library is a collection of functions. To create library – define functions in a file and import that file in current environment.

Let us suppose we have defined all functions in utils.sh file then use below command to import functions in current environment:

$ source utils.sh

Hereafter you can execute any function from library just like any other bash command.

Conclusion

In this article we discussed few useful recipes which will improve your productivity. I hope this articles inspires you to create your own recipes.

Read AlsoHow to Use if else Conditionals Statement in Bash Script

The post How to define and use functions in Linux Shell Script first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/define-use-functions-linux-shell-script/feed/ 2
How to Compare Numbers and Strings in Linux Shell Script https://www.linuxtechi.com/compare-numbers-strings-files-in-bash-script/ https://www.linuxtechi.com/compare-numbers-strings-files-in-bash-script/#comments Thu, 27 Apr 2017 04:57:13 +0000 http://www.linuxtechi.com/?p=5487 In this tutorial on Linux bash shell scripting, we are going to learn how to compare numbers, strings and files in shell script using if statement. Comparisons in a script are very useful & after comparison result, script will execute the commands and we must ... Read more

The post How to Compare Numbers and Strings in Linux Shell Script first appeared on LinuxTechi.]]>
In this tutorial on Linux bash shell scripting, we are going to learn how to compare numbers, strings and files in shell script using if statement. Comparisons in a script are very useful & after comparison result, script will execute the commands and we must know how we can use them to our advantage.

Syntax of comparisons in shell script

if [ conditions/comparisons]
    then
         commands
fi

An example

if [2 -gt 3]
     then
     print "2 is greater"
     else
     print "2 is not greater"
fi

This was just a simple example of numeric comparison & we can use more complex statement or conditions in our scripts. Now let’s learn numeric comparisons in bit more detail.

Compare Numbers in Linux Shell Script

This is one the most common evaluation method i.e. comparing two or more numbers. We will now create a script for doing numeric comparison, but before we do that we need to know the parameters that are used to compare numerical values . Below mentioned is the list of parameters used for numeric comparisons

  • num1 -eq num2                  check if 1st  number is equal to 2nd number
  • num1 -ge num2                  checks if 1st  number  is greater than or equal to 2nd number
  • num1 -gt num2                  checks if 1st  number is greater than 2nd number
  • num1 -le num2                   checks if 1st number is less than or equal to 2nd number
  • num1 -lt num2                   checks if 1st  number  is less than 2nd number
  • num1 -ne num2                  checks if 1st  number  is not equal to 2nd number

Now that we know all the parameters that are used for numeric comparisons, let’s use these in a script,

#!/bin/bash
# Script to do numeric comparisons
var1=10
var2=20
if [ $var2 -gt $var1 ]
    then
        echo "$var2 is greater than $var1"
fi
# Second comparison
If [ $var1 -gt 30]
    then
        echo "$var is greater than 30"
    else
        echo "$var1 is less than 30"
fi

This is the process to do numeric comparison, now let’s move onto string comparisons.

Compare Strings in Linux Shell Script

When creating a bash script, we might also be required to compare two or more strings & comparing strings can be a little tricky. For doing strings comparisons, parameters used are

  • var1 = var2     checks if var1 is the same as string var2
  • var1 != var2    checks if var1 is not the same as var2
  • var1 < var2     checks if var1 is less than var2
  • var1 > var2     checks if var1 is greater than var2
  • -n var1             checks if var1 has a length greater than zero
  • -z var1             checks if var1 has a length of zero

Note :-  You might have noticed that greater than symbol (>) & less than symbol (<) used here are also used for redirection for stdin or stdout in Linux. This can be a problem when these symbols are used in our scripts, so what can be done to address this issue.

Solution is simple , when using any of these symbols in scripts, they should be used with escape character i.e. use it as “/>” or “/<“.

Now let’s create a script doing the string comparisons.

In the script, we will firstly be checking string equality, this script will check if username & our defined variables are same and will provide an output based on that. Secondly, we will do greater than or less than comparison. In these cases, last alphabet i.e. z will be highest & alphabet a will be lowest when compared. And capital letters will be considered less than a small letter.

#!/bin/bash
# Script to do string equality comparison
name=linuxtechi
if [ $USER = $name ]
        then
                echo "User exists"
        else
                echo "User not found"
fi
# script to check string comparisons
var1=a
var2=z
var3=Z
if [ $var1 \> $var2 ]
        then
                echo "$var1 is greater"
        else
                echo "$var2 is greater"
fi
# Lower case  & upper case comparisons
if [ $var3 \> $var1 ]
        then
                echo "$var3 is greater"
        else
                echo "$var1 is greater"
fi

We will now be creating another script that will use “-n” & “-z” with strings to check if they hold any value

#!/bin/bash
# Script to see if the variable holds value or not
var1=" "
var2=linuxtechi
if [ -n $var1 ]
        then
                echo "string  is not empty"
        else
                echo "string provided is empty"
fi

Here we only used ‘-n’ parameter but we can also use “-z“. The only difference is that with ‘-z’, it searches for string with zero length while “-n” parameter searches for value that is greater than zero.

File comparison in Linux Shell Script

This might be the most important function of comparison & is probably the most used than any other comparison. The Parameters that are used for file comparison are

  • -d file                        checks if the file exists and is it’s a directory
  • -e file                        checks if the file exists on system
  • -w file                       checks if the file exists on system and if it is writable
  • -r file                        checks if the file exists on system and it is readable
  • -s file                        checks if the file exists on system and it is not empty
  • -f file                         checks if the file exists on system and it is a file
  • -O file                       checks if the file exists on system and if it’s is owned by the current user
  • -G file                        checks if the file exists and the default group is the same as the current user
  • -x file                         checks if the file exists on system and is executable
  • file A -nt file B         checks if file A is newer than file B
  • file A -ot file B          checks if file A is older than file B

Here is a script using the file comparison

#!/bin/bash
# Script to check file comparison
dir=/home/linuxtechi
if [ -d $dir ]
        then
                echo "$dir is a directory"
                cd $dir
                ls -a
        else
                echo "$dir is not exist"
fi

Similarly we can also use other parameters in our scripts to compare files. This completes our tutorial on how we can use numeric, string and file comparisons in bash scripts. Remember, best way to learn is to practice these yourself.

Read Also : How to Create Hard and Soft (symlink) Links on Linux Systems

The post How to Compare Numbers and Strings in Linux Shell Script first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/compare-numbers-strings-files-in-bash-script/feed/ 3
How to Pass Command Line Arguments to Bash Script https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/ https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/#comments Tue, 02 Jun 2015 05:17:03 +0000 http://www.linuxtechi.com/?p=3133 In this tutorial, we will learn how to pass command line arguments to a bash shell script in Linux. Command line arguments are the arguments specified at the command prompt with a command or script to be executed. The locations at the command prompt of ... Read more

The post How to Pass Command Line Arguments to Bash Script first appeared on LinuxTechi.]]>
In this tutorial, we will learn how to pass command line arguments to a bash shell script in Linux.

Command line arguments are the arguments specified at the command prompt with a command or script to be executed. The locations at the command prompt of the arguments as well as the location of the command, or the script itself, are stored in corresponding variables. These variables are special shell variables. Below picture will help you understand them.

command-line-arguments

command-line-shell-variables

Let’s create a shell script with name “arguments.sh”, it will show the command line arguments that were supplied and count number of arguments, value of first argument and Process ID (PID) of the Script.

$ vi arguments.sh
#!/bin/bash
#This Script demonstrate the usage of command line arguments in bash script

echo "There are $# arguments pass at command line"
echo "The arguments supplied are : $*"
echo "The first command line argument is: $1"
echo "The PID of the script is: $$"

Save and close the  file.

Assign Executable permissions  to the script using chmod command

$ chmod +x arguments.sh

Now execute the script with following command line arguments

$ ./arguments.sh Debian RockyLinux Ubuntu RHEL SUSE

output,

Pass-command-line-arguments-bash-script

In above script, we can also use ‘$@’ in place of $* to get all the arguments. The key difference between these two variables is that ‘$*’ represent all the parameters in single string whereas ‘$@’ represent arguments as a array or $@ expands to multiple arguments.

‘$@’ is always preferred over ‘$*’, to understand their difference , let’s create following script,

$ vi arguments-new.sh
#!/bin/bash
echo "With *:"
for arg in "$*"
do
echo "<$arg>"
done
echo
echo "With @:"
for arg in "$@"
do
echo "<$arg>"
done

Save & exit the file.

Execute the script and notice the difference

Arguments-Example-Bash-Script

Shifting Command Line Arguments

The shift command is used to move command line arguments one position to the left. During this move, the first argument is lost. The above script would look like below after adding shift command to it.

$ cat arguments.sh
#!/bin/bash
#This Script demonstrate the usage of command line arguments in bash script

echo "There are $# arguments pass at command line"
echo "The arguments supplied are : $*"
echo "The first command line argument is: $1"
echo "The PID of the script is: $$"
shift
echo "New first argument after first shift: $1"
shift
echo "New first argument after second shift: $1"

Let’s re-run the script with following command line arguments,

$ ./arguments.sh Windows Linux MacOS HPUX
There are 4 arguments pass at command line
The arguments supplied are : Windows Linux MacOS HPUX
The first command line argument is: Windows
The PID of the script is: 2193
New first argument after first shift: Linux
New first argument after second shift: MacOS
$

Multiple shifts in a single attempt may be performed by furnishing the desired number of shifts to the shift command as an argument.

Note: Command line arguments are also known as positional parameters.

That’s all from this tutorial, I hope you have understood how to pass command line arguments to a bash script.

Also Read: How to Use Conditional Statements in Bash Script

The post How to Pass Command Line Arguments to Bash Script first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/command-line-arguments-in-linux-shell-scripting/feed/ 5
How to Debug a Bash Shell Script in Linux https://www.linuxtechi.com/debugging-shell-scripts-in-linux/ https://www.linuxtechi.com/debugging-shell-scripts-in-linux/#respond Mon, 23 Feb 2015 03:17:16 +0000 http://www.linuxtechi.com/?p=2956 In most of the programming languages, debugger tool is available for debugging. A debugger is a tool that can run a program or script that enables you to examine the internals of the script or program as it runs. In this post, we will learn ... Read more

The post How to Debug a Bash Shell Script in Linux first appeared on LinuxTechi.]]>
In most of the programming languages, debugger tool is available for debugging. A debugger is a tool that can run a program or script that enables you to examine the internals of the script or program as it runs.

In this post, we will learn how to debug a bash shell script line by line in linux. In the shell scripting we do not have any debugger tool but with the help of bash command line options like -n, -v and -x we can do the debugging.

Checking Shell Script for Syntax Error

When we run the script using -n option in bash command then it will not execute the script but it will read the script and validate the syntax and will report errors if any.

In other words, we can say -n option, shot for noexec (as in no execution), tells the shell to not run the commands. Instead, the shell just checks for syntax errors.

Let’s create a script with following content,

$ vi debug_quotes.sh
#!/bin/bash
echo "USER=$USER"
echo "Today's Date: $(date)
echo "SHELL=$SHELL"

save and close the file.

Now try run the script with -n option,

$ bash -n debug_quotes.sh
debug_quotes.sh: line 4: unexpected EOF while looking for matching `"'
debug_quotes.sh: line 5: syntax error: unexpected end of file
$

Output  above shows that there is syntax error, double quotes ‘”‘ is missing. To fix this, put double quotes at end of line which shows today’s date.

Debug-Quotes-Shell-Script-Linux

Running Shell Script in Verbose Mode

The -v option in bash command tells the shell script to run in verbose mode. In practice, this means that shell will echo each command prior to execute the command. This is very useful in that it can often help to find the errors.

Let’s us a shell script with the name “listusers.sh” with following content,

$ vi listusers.sh
#!/bin/bash
cut -d : -f1,5,7 /etc/passwd | grep -v sbin | grep sh | sort > /tmp/users.txt
awk -F':' ' { printf ( "%-12s %-40s\n", $1, $2 ) } ' /tmp/users.txt

#Clean up the temporary file.
/bin/rm -f /tmp/users.txt

Execute the script with -v option,

$ bash -v listusers.sh

output,

list-users-script-output-linux

In output above , script output gets mixed with commands of the scripts. But however , with -v option , at least you get a better view of what the shell is doing as it runs your script.

Running Script with -n and -v option

We can combine the command line options ( -n & -v ). This makes a good combination because we can check the syntax of a script while viewing the script output.

Let us consider a previously used script “debug_quotes.sh”

$ bash -nv debug_quotes.sh
#!/bin/bash
echo "USER=$USER"
echo "Today's Date: $(date)
echo "SHELL=$SHELL"
debug_quotes.sh: line 4: unexpected EOF while looking for matching `"'
debug_quotes.sh: line 5: syntax error: unexpected end of file
$

Debug Shell Script Line by Line

The -x option, short for xtrace or execution trace, tells the shell to echo each command after performing the substitution steps. Thus , we can see the values of variables and commands. Often, this option alone will help to diagnose a problem.

In most cases, the -x option provides the most useful information about a script, but it can lead to a lot of output. The following example show this option in action.

$ bash -x listusers.sh
+ sort
+ grep sh
+ grep -v sbin
+ cut -d : -f1,5,7 /etc/passwd
+ awk -F: ' { printf ( "%-12s %-40s\n", $1, $2 ) } ' /tmp/users.txt
linuxtechi linuxtechi,,,
root root
+ /bin/rm -f /tmp/users.txt
$

As we can see that shell has inserted a + sign in front of each command.

Debug Shell Script with ShellCheck

Shellcheck is a free tool that provides warnings and suggestions for our bash or shell scripts. It points out the syntax issues and  semantic problems of our shell script.

To use shellcheck, first install it using following command,

$ sudo apt install shellcheck     // Ubuntu & Debian

For RHEL / Fedora / CentOS

$ sudo dnf install epel-release -y
$ sudo dnf install ShellCheck

Let’s use the above debug script and run shellcheck on it,

$ shellcheck debug_quotes.sh

ShellCheck-Script-Output

To suppress the warning message use -e flag as shown below,

$ shellcheck -e SC1078 debug_quotes.sh

Exclude-Warning-Shellscheck-Script

Output above confirms that there is a syntax error on line 3, if we put double quotes at end of line 3 then syntax error will be fixed.

That’s all from this post. I you have found it informative and useful. Please do not hesitate to post your queries and feedback in below comments section.

Also Read: How to define and use functions in Linux Shell Script

The post How to Debug a Bash Shell Script in Linux first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/debugging-shell-scripts-in-linux/feed/ 0
25 Linux Shell Scripting Interview Questions and Answers https://www.linuxtechi.com/linux-shell-scripting-interview-questions-answers/ https://www.linuxtechi.com/linux-shell-scripting-interview-questions-answers/#comments Mon, 02 Feb 2015 02:47:14 +0000 http://www.linuxtechi.com/?p=2927 Q:1 What is Shell Script and why it is required ? Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish day to day tasks, we can ... Read more

The post 25 Linux Shell Scripting Interview Questions and Answers first appeared on LinuxTechi.]]>
Q:1 What is Shell Script and why it is required ?

Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish day to day tasks, we can add these all commands together in a text file (Shell Script) to complete daily routine task. In other words we can shell is required for the automation.

Q:2 What is the default login shell and how to change default login shell for a specific user ?

Ans: In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” command . Example is shown below :

# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

Q:3 What are the different type of variables used in a shell Script ?

Ans: In Linux shell script we can use two types of variables :

  • System defined variables
  • User defined variables

System defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command. To display the value of system defined variables use echo command, examples are  echo $PWD or echo $HOME
User defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo $<Name_of_Variable>

Q:4 How to redirect both standard output and standard error to the same location ?

Ans: There two methods to redirect std output and std error to the same location. These methods are listed below:

Method 1)  2>&1 (# ls /usr/share/doc > out.txt 2>&1 )

Method 2)  &> (# ls /usr/share/doc &> out.txt )

Q:5 What is the Syntax of “nested if statement” in shell scripting ?

Ans : Basic Syntax is shown below :

if [ Condition ]
then
command1
command2
…..
else
if [ condition ]
then
command1
command2
….
else
command1
command2
…..
fi
fi

Q:6 What is the use of “$?” sign in shell script ?

Ans: While writing a shell script , if you want to check whether previous command is executed successfully or not , then we can use “$?”. Use echo command to print the vlaue of ‘$?’ variable. This variable is generally used in if statement to check the exit status of previous command. Basic example is shown below :

root@localhost:~# ls /usr/bin/shar
/usr/bin/shar
root@localhost:~# echo $?
0

If exit status is 0 , then command is executed successfully

root@localhost:~# ls /usr/bin/share

ls: cannot access /usr/bin/share: No such file or directory
root@localhost:~# echo $?
2

If the exit status is other than 0, then we can say command is not executed successfully.

Q:7 How to compare numbers in Linux shell Scripting ?

Ans: We can compare the numbers in shell script by using parameters like ‘-gt’ (greater than), ‘-eq’ (equals to) and  ‘-lt’ (less than) in if statement. Example is shown below :

#!/bin/bash
x=10
y=20

if [ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is greater than x”
fi

Q:8 What is the use of break command ?

Ans: The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops.

Q:9 What is the use of continue command in shell scripting ?

Ans The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop.

Q:10 Tell me the Syntax of “Case statement” in Linux shell scripting ?

Ans: The basic syntax is shown below :

case word in
value1)
command1
command2
…..
last_command
!!
value2)
command1
command2
……
last_command
;;
esac

Q:11 What is the basic syntax of while loop in shell scripting ?

Ans: Like the for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition is no longer true. The basic syntax is :

while [ test_condition ]
do
commands…
done

Q:12 How to make a shell script executable ?

Ans: Using the chmod command we can make a shell script executable. Example is shown below :

# chmod a+x myscript.sh

Q:13 What is the use of “#!/bin/bash” ?

Ans: #!/bin/bash is the first of a shell script , known as shebang , where # symbol is called hash and ‘!’ is called as bang. It shows that command to be executed via /bin/bash.

Q:14 What is the syntax of for loop in shell script ?

Ans: Basic Syntax of for loop is given below :

for variables in list_of_items
do
command1
command2
….
last_command
done

Q:15 How to debug a shell script ?

Ans: A shell script can be debug if we execute the script with ‘-x’ option ( sh -x myscript.sh). Another way to debug a shell script is by using ‘-nv’ option ( sh -nv myscript.sh).

Q:16 How compare the strings in shell script ?

Ans: test command is used to compare the text strings. The test command compares text strings by comparing each character in each string.

Q:17 What are the Special Variables set by Bourne shell for command line arguments ?

Ans: The following table lists the special variables set by the Bourne shell for command line arguments.

Special-Variables-Linux-Shell-Scripting

Q:18 How to test files in a bash shell script ?

Ans: test command is used to perform different test on the files. Basic test are listed below :

Test-Conditions-Linux-Shell-Script

Q:19 How to put comments in your bash shell script ?

Ans: Comments are the messages to yourself and for other users that describe what a script is supposed to do and how it works. To put comments in your script, start each comment line with a hash sign (#) . Example is shown below :

#!/bin/bash
# This is a command
echo “I am logged in as $USER”

Q:20 How to get input from the terminal for shell script ?

Ans: ‘read’ command reads in data from the terminal (using keyboard). The read command takes in whatever the user types and places the text into the variable you name. Example is shown below :

# vi /tmp/test.sh

#!/bin/bash
echo ‘Please enter your name’
read name
echo “My Name is $name”

# ./test.sh
Please enter your name
LinuxTechi
My Name is LinuxTechi

Q:21 How to unset or de-assign variables in Linux ?

Ans: ‘unset’ command is used to de-assign or unset a variable. Syntax is shown below :

# unset <Name_of_Variable>

Q:22 How to perform arithmetic operation in Linux ?

Ans: There are two ways to perform arithmetic operations :

1. Using expr command (# expr 5 + 2 )
2. using a dollar sign and square brackets ( $[ operation ] )

Example : test=$[16 + 4] ; test=$[16 + 4]

Q:23 Basic Syntax of do-while statement ?

Ans: The do-while statement is similar to the while statement but performs the statements before checking the condition statement. The following is the format for the do-while statement:

do
{
statements
} while (condition)

Q:24 How to define functions in shell scripting ?

Ans: A function is simply a block of of code with a name. When we give a name to a block of code, we can then call that name in our script, and that block will be executed. Example is shown below :

$ diskusage () { df -h ; }

Q:25 How to use bc (bash calculator) in a bash shell script ?

Ans: Use the below Syntax to use bc in shell script.

variable=`echo “options; expression” | bc`

Read Also: 20 Linux Commands Interview Questions and Answers

The post 25 Linux Shell Scripting Interview Questions and Answers first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/linux-shell-scripting-interview-questions-answers/feed/ 4
Working with Input Output and Error Redirection in Linux https://www.linuxtechi.com/standard-input-output-error-in-linux/ https://www.linuxtechi.com/standard-input-output-error-in-linux/#respond Thu, 04 Dec 2014 07:53:48 +0000 http://www.linuxtechi.com/?p=2764 Every process in Linux is provided with three open files( usually called file descriptor). These files are the standard input, output and error files. By default : Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts. Standard ... Read more

The post Working with Input Output and Error Redirection in Linux first appeared on LinuxTechi.]]>
Every process in Linux is provided with three open files( usually called file descriptor). These files are the standard input, output and error files. By default :

  • Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
  • Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
  • Standard error is the same as standard output:the shell window or terminal from which the script runs.

A file descriptor is simply a number that refers to an open file. By default , file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin. File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr). These numbers are important when you need to access a particular file , especially when you want to redirect these files to the other locations, File descriptors numbers go up from zero.

Redirecting Standard Output

Syntax to redirect the output of a command to a file.

# Command_options_and_arguments > output_file

Example :

linuxtechi@localhost:~$ cat /proc/cpuinfo > command.txt

We can see the data that would have gone to the screen with more command :

linuxtechi@localhost:~$ more command.txt 
processor         : 0
vendor_id         : GenuineIntel
cpu family        : 6
model             : 37
model name        : Intel(R) Core(TM) i3 CPU       M 370  @ 2.40GHz
stepping          : 5
microcode         : 0x616
cpu MHz           : 0.000
cache size        : 6144 KB
physical id       : 0
siblings          : 2
core id           : 0
cpu cores         : 2
apicid            : 0
initial apicid    : 0
fpu               : yes
fpu_exception     : yes
cpuid level       : 5
wp                : yes

The > operator tells the shell to redirect the output of the command to the given file. If the file exists , the deletes the old contents of the file and replaces it with the output of the command.

Redirecting a Command’s Input

Syntax to redirect the input of a command to come from a file.

# Command_options_and_arguments < input_file

Use the < operator to redirect the input for a command , example is shown below :

linuxtechi@localhost:~$ wc -l < command.txt
52

In this example , the input to the ‘wc‘ command comes from the file named command.txt. The shell sends the contents of the file command.txt as a standard input for the wc command.

Note : We can also combine both redirections with following syntax :

# command_options_and_agruments < input_file > output_file.

Redirecting Standard Error

In addition to redirecting the standard input and output for a script or a command, we can also redirect standard error. Even though standard error by defaults goes to the same place as the standard output – the shell window or terminal. There are good reasons why stdout and stderr are treated separately. The main reason is that we can redirect the output of a command or commands to a file but you have no way of knowing whether an error occurred. Separating stderr from stdout allows the error message to appear on your screen while output still goes to a file.

Syntax to redirect stderr from a command to a file.

# command_options_and_agruments 2> output_file.

The 2 in 2> refers to the file descriptor 2, the descriptor number for stderr.

Example:

linuxtechi@localhost:~$ lsash /usr/bin 2> commands-error.txt
linuxtechi@localhost:~$ cat commands-error.txt
No command 'lsash' found, did you mean:
Command 'sash' from package 'sash' (universe)
lsash: command not found

Redirecting both Standard Output & Standard Error

Use 2>&1 Syntax to redirect standard error to the same location as standard output .

Example:1

linuxtechi@localhost:~$ ls /usr/bin > command.txt 2>&1

Above Command has three parts.

  • ls /usr/bin is the command run
  • > command.txt redirects the output of the ls command
  • 2>&1 sends the output of the file descriptor 2, stderr , to the same location as the file descriptor 1, stdout.

Example: 2

linuxtechi@localhost:~$ ls /usr2222/bin > command.txt 2>&1
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory

Note that above example assumes that your system doesn’t have directory names “/usr2222/bin”

Redirecting Both stderr & stdout at Once

linuxtechi@localhost:~$ ls /usr2222/bin &> command.txt
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory

In the above command ls is the command , /usr2222/bin is the argument to the ‘ls‘ command and ‘&> command.txt‘ redirect both stdout and stderr to a file named command.txt.

Appending To Files

Use the ‘>>’ operator to redirect the output of a command , but append to the file , if it exists. The syntax is given below :

# Command >> file_to_append.

Example:

linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ more sysload.txt
11:49:17 up 1:22, 3 users, load average: 0.28, 0.12, 0.11
11:49:28 up 1:22, 3 users, load average: 0.40, 0.15, 0.12
11:49:36 up 1:23, 3 users, load average: 0.33, 0.14, 0.12

Truncating Files :

We can use a shorthand syntax for truncating files by omitting the command before > operator . The Syntax is given below :

# > file_name

We can also use an alternate format with a colon :

# : > file_name

Both of these command-less command will create the file if it does not exist and truncate the file to zero bytes if the file does exist.

linuxtechi@localhost:~$ ls /usr/bin > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 19713 Dec 2 12:18 command.txt
linuxtechi@localhost:~$ > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Dec 2 12:18 command.txt

Sending Output to Nowhere Fast

There are some scenarios where you not only want to redirect the output of a command , you want to throw the output away. You can do this by redirecting a command’s output to the null file “/dev/null” The null file consumes all output sent to it , as if /dev/null is a black hole star.

linuxtechi@localhost:~$ ls /usr/bin > /dev/null

Note : The file /dev/null is often called a bit bucket.

Recommended Read : How to Use Encrypted Password in Linux Bash Shell Script

The post Working with Input Output and Error Redirection in Linux first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/standard-input-output-error-in-linux/feed/ 0
How to Use Conditional Statements in Bash Script https://www.linuxtechi.com/shell-scripting-checking-conditions-with-if/ https://www.linuxtechi.com/shell-scripting-checking-conditions-with-if/#comments Sun, 26 Oct 2014 11:21:26 +0000 http://www.linuxtechi.com/?p=2669 In this guide, we will learn how to use conditional statements like if, if-else, If-elif-else, netsted if and case in the bash script. Conditional statements plays an important role in bash script as it helps to take decision based on the condition. In a bash ... Read more

The post How to Use Conditional Statements in Bash Script first appeared on LinuxTechi.]]>
In this guide, we will learn how to use conditional statements like if, if-else, If-elif-else, netsted if and case in the bash script. Conditional statements plays an important role in bash script as it helps to take decision based on the condition.

In a bash script, if statement checks whether a condition is true or not. If so, the shell executes the block of code associated with the if statement. If the statement is not true , the shell jumps beyond the end of the if statement block & Continues on.

if Statement 

Syntax: 

if [ condition_command ]
then
        command1
        command2
        ……..
        last_command
fi

In below bash script example, we are comparing two numbers using if condition statement.

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -lt 150 ]
then
echo "Number is $n"
fi

When we run this script, it will print the number if it is less than 150.

If-statement-Bash-Script-Example

if-else Statement

In addition to the normal if statement , we can extend the if statement with an else block. The basic idea is that if the statement is true , then execute the if block. If the statement is false, then execute the else block.

Syntax :

if [ condition_command ]
then
       command1
       command2
       ……..
       last_command
else
       command1
       command2
       ……..
       last_command
fi

If we modify the above script and insert else block then it would look like below,

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
    echo "Number $n is greater than 150"
else
    echo "Number $n is smaller than 150"
fi

If we run the script and enter the number as 129 then it will execute else block as shown below,

If-else-statement-example-bash-script

If-elif-else Statement

In bash script, if you wish to apply multiple conditions using if statement then use ‘ if elif else’. In this type of conditional statement, if the first condition is met then code below it will be executed otherwise next if condition will checked and if it is not matched then commands mentioned below else statement will be executed. It’s syntax and example is shown below.

Syntax :

if [ condition_command ]
then
       command1
       command2
       ……..
       last_command
elif [ condition_command2 ]
then
        command1
        command2
        ……..
        last_command
else
command1
command2
……..
last_command
fi

Example :

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
    echo "Number $n is greater than 150"
elif [ $n -lt 150 ]
then
    echo "Number $n is smaller than 150"
else
    echo "Number $n is equal to 150"
fi

Script Execution output,

if-elif-else-statement-example-bash-script

Nested if Statement

If statement and else statement can be nested in a bash script. The keyword ‘fi’ shows the end of the inner if statement and all if statement should end with the keyword ‘fi’.

Basic syntax of nested if is shown below :

if [ condition_command ]
then
        command1
        command2
        ……..
        last_command
else
if [ condition_command2 ]
then
        command1
        command2
        ……..
        last_command
else
        command1
        command2
         ……..
         last_command
      fi
fi

When we modify above script to use nested-if statement then its code would look like below,

#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
   echo "Number $n is greater than 150"
else
if [ $n -lt 150 ]
then
   echo "Number $n is smaller than 150"
else
  echo "Number $n is equal to 150"
  fi
fi

Above Script execution output,

Nested-if-statement-examples-bash-script

Case Statement

Case statement is similar to if statement with multiple elif. case statement in bash script expands the expression and then tries to to find the match with all the patterns. When a match is found then all the statements will be executed till the double semicolon (;;). In case it does not find the match then statements mentioned under *) pattern will be executed.

Syntax of Case Statement

case expression in 
    pattern1) 
       statements 
       ;; 
    pattern2) 
       statements 
       ;; 
    pattern3) 
       statements 
       ;; 
    pattern-n) 
       statements 
       ;;
    *) 
       statements 
       ;; 
esac

Example,

#!/bin/bash

echo "Which is Your Favorite Linux Distro?"
echo "Debian, Ubuntu, SUSE, RHEL, Arch"
read -p "Type Your Linux Distro:" OS

case $OS in
    Debian)
        echo "Most Stable Linux OS, good choice "
        ;;
    Ubuntu)
        echo "Best Linux OS for Desktop and Servers"
        ;;
    SUSE)
        echo "Top Linux OS for SAP Application"
        ;;
    Arch)
        echo "Flexible OS for experienced Linux users"
        ;;
    *)
        echo "Please Enter Correct OS from the list"
        ;;
esac

Script Execution output,

Case-Statement-Example-Bash-Script

Also Read16 Quick Cat Command Examples in Linux

The post How to Use Conditional Statements in Bash Script first appeared on LinuxTechi.]]>
https://www.linuxtechi.com/shell-scripting-checking-conditions-with-if/feed/ 2