Shell Scripting

Shell Scripting

Shell Scripting is an open-source operating system.

Our Shell Scripting tutorial includes all topics of Scripting executing scripting, loops, scripting parameters, shift through parameters, sourcing, getopts, case, eval, let etc. There is also given Shell Scripting interview questions to help you better understand the Shell Scripting operating system.

What is Shell Scripting

In Linux, shells like bash and korn support programming construct which are saved as scripts. These scripts become shell commands and hence many Linux commands are script.

A shell script is a type of computer program developed to be executed by a Unix shell, which is also known as a command-line interpreter.

Several shell script dialects are treated as scripting languages.

Classic operations implemented by shell scripts contain printing text, program execution, and file manipulation.

A script configures the environment, executes the program, and does necessary logging or clean-up is known as a wrapper.

Also, the term is more generally used to define the automated mode of executing an operating system shell; all operating systems use a specific name for the functions such as mainframe operating systems, shell scripts (third-party derivatives and Windows NT stream like 4NT), VMS (command procedures), and batch files (OS/2, MSDos-Win95 stream) are related to several terms.

Commonly, shells represent in Unix-like and Unix systems, such as the GNU Bash, the Bourne shell, and the Korn shell. These shells are generally represented for backward compatibility, while the Unix operating system may contain a distinct default shell like MacOS upon zsh.

Capabilities of Shell Script

  • Comments

Comments are avoided by the shell. Typically, they start with the hash (#) symbol and continue until the completion of the line.

  • Scripting language configurable choice

The hash-bang or shebang is a unique type of comment that the system applies to decide what interpreter to utilize to run the file. The hash-bang must be the file's first line and begin with the "#!" symbol. Following the "#!" prefix, the characters are specified as a path to any executable program that will specify the script in Unix-like operating systems.

  • Shortcuts

A shell script can offer an easy variation of the system command in which unique environment settings, post-processing, or command options automatically apply, but in a form that permits the new script to act as a completely normal Unix command.

An example would be to make an ls version, the command for listing files, providing a reduced 'l' command name, which would normally be saved in the bin directory of a user as /home/username/bin/l, and the command option's default set pre-supplied.

  1. #!/bin/ sh  

  2. LC_COLLATE=C ls -FCas "$@"  

In the above example, the first line applies shebang to represent which interpreter should run the rest of the script. The other line creates a listing using options for several file format columns, indicators, all files, and sizes in blocks. The LC_COLLATE=C configures the default collation sequence for not folding lower and uppercase together.

Another shell script example that could be run as a shortcut would be to show all file and directory lists in a given directory.

  1. #!/bin/ sh

  2. clear  

  3. ls -al  

In the above example, the shell script would begin with its common beginning line of #!/bin/ sh. Following it, the script runs the clear command, which clears the command-line of every text before the next line. The ls -al command will list the directories and files in the directory through which the script is being executed. The attributes of the ls command could be modified to reflect the requirements of the user.

Important: We can use the clr command if an implementation doesn't have a clear command.

  • Batch jobs

A shell script permits many commands that would be manually entered at a command-line interface to be automatically executed without needing to wait for any user to generate all sequence stages.

  • Generalization

Common batch jobs are usual for separated tasks, but using shell variables, tests, and loops gives much more compliance to users. The POSIX sh script is used to transform JPEG images into PNG images, in which the image titles are given possibly by wildcards in the command-line rather than all being listed in the script; it can be made with this file, generally saved inside a file such as /home/username/bin/jpg2png.

  • Programming

Also, several modern shells supply many features generally detected only in more practical general-purpose programming languages like subroutines, arrays, comments, variables, control flow constructs, and so on. With these types of features available, it's possible to reasonably write practical applications as shell scripts.

Although, they are still restricted by the fact that almost all shell languages have no or little support for complex math, data typing systems, threading, classes, and other basic full language aspects, and are also much slower than interpreted languages or compiled code specified with speed as an administration goal.

The standard awk and sed Unix tools give additional capabilities for shell programming. Also, Perl can be installed in shell scripts as other scripting languages such as Tcl. Tcl and Perl also provide graphics toolkits.

How to determine Shell

You can get the name of your shell prompt, with following command :

Syntax:

  1. echo $SHELL  

    Linuxss How to determine shell 1

Look at the above snapshot, with the help of above command we got the name of our shell which is 'bash'.

The $ sign stands for a shell variable, echo will return the text whatever you typed in.

Shell Scripting She-bang

The sign #! is called she-bang and is written at top of the script. It passes instruction to program /bin/sh.

To run your script in a certain shell (shell should be supported by your system), start your script with #! followed by the shell name.

Example:

  1. #!/bin/bash  -->gitbash #!/bin/sh-->shell-scripting

  2. echo Hello World

  3. #!/bin/ksh  

  4. echo Hello World  

    Shell Scripting Comments

    Any line starting with a hash (#) becomes comment. Comment means, that line will not take part in script execution. It will not show up in the output.

    Shell Scripting Variables

    Scripts can contain variables inside the script.

    Linux Shell Scripting Variables 1

Look at the above snapshot, two variables are assigned to the script $var1 and $var2.

As scripts run in their own shell, hence variables do not survive the end of the script.

Linux Shell Scripting Variables 2

Look at the above snapshot, var1 and var2 do not run outside the script.

Shell Scripting Sourcing a file.

A file is sourced in two ways. One is either writting as source <fileName> or other is writting as . ./<filename> in the command line. When a file is sourced, the code lines are executed as if they were printed on the command line.

The difference between sourcing and executing a script is that, while executing a script it runs in a new shell whereas while sourcing a script, file will be read and executed in the same shell.

In sourcing, script content is displayed in the same shell but while executing script run in a different shell.

Shell Scripting Sourcing a file 1

Look at the above snapshot, we have sourced the file var with one of the method.

Shell Scripting Sourcing a file 2

Look at the above snapshot, we have sourced the file var with another method.

Troubleshooting a shell script

There is one more way other than script execution to run a script in a different shell. Type bash with the name of the script as parameter.

Syntax:

  1. bash  <fileName>

Example:

bash exm

Linux Troubleshooting a script 1

Look at the above snapshot, it displays the exm script content with bash command.

Linux Troubleshooting a script 2

Look at the above snapshot, this is the exm script we have written.

By expanding bash command with -x, shell allows us to see commands that the shell is executing.

Linux Troubleshooting a script 3

Look at the above snapshot, with command bash -x, we can see the shell expansion.

Shell Scripting Prevent setuid root spoofing

Spoofing is a technique through which a user tries to grant unauthorized access on a system by pretending to be the root user This is called setuid root spoofing.

To prevent from spoofing you can add -- after #!/bin/bash. It disables further option processing so that shell will not accept any options.

Linux Shell Scripting Prevent setuid root spoofing 1

Look at the above snapshot, any argument after -- are treated as filenames and arguments. An argument of - is equivalent to --.

Steps to write and execute a script

  • Open the terminal. Go to the directory(Folder) where you want to create your script.

  • Create a file with .sh extension.

  • Write the script in the file using an editor.(E.g->Vim editor)

  • Make the script executable with command chmod +x <fileName>.

  • Run the script using ./<fileName>.

Note: In the last step you have to mention the path of the script if your script is in other directory.

The chmod (CHange MODe) command is used to change permissions for a file or directory on a Unix machine.


Hello World script

Here we'll write a simple programme for Hello World.

First of all, create a simple script in any editor or with echo. Then we'll make it executable with chmod +x command. To find the script you have to type the script path for the shell.

Linuxss Steps to write and execute a script 1

Look at the above snapshot, script echo Hello World is created with echo command as hello_world. Now the command chmod +x hello_world is passed to make it executable. We have given the command ./hello_world to mention the hello_world path. And output is displayed.

Shell Script Parameters

A bash shell script have parameters. These parameters start from $1 to $9.

When we pass arguments into the command line interface(CLI), a positional parameter is assigned to these arguments through the shell.

The first argument is assigned as $1, second argument is assigned as $2 and so on...

If there are more than 9 arguments, then tenth or onwards arguments can't be assigned as $10 or $11.

You have to either process or save the $1 parameter, then with the help of shift command drop parameter 1 and move all other arguments down by one. It will make $10 as $9, $9 as $8 and so on.

Shell Parameters

ParametersFunction
$1-$9Represent positional parameters for arguments one to nine
${10}-${n}Represent positional parameters for arguments after nine
$0Represent name of the script
$∗Represent all the arguments as a single string
$@Same as $∗, but differ when enclosed in (")
$#Represent total number of arguments
$$PID of the script
$?Represent last return code

Example:

Linux Shell Scripting parameters 1

Look at the above snapshot, this is the script we have written to show the different parameters.

Linux  Script parameters 2

Look at the above snapshot, we have passed arguments 1, 5, 90. All the parameters show their value when script is run.

Shell Scripting Shift Through Parameters

Shift command is a built-in command. Command takes number as argument. Arguments shift down by this number.

For example, if number is 5, then $5 become $1, $6 become $2 and so on.

Example:

The shift command is mostly used when arguments are unknown. Arguments are processed in a while loop with a condition of (( $# )). this condition holds true as long as arguments are not zero. Number of arguments are reduced each time as the shift command executes.

Shell Scripting Shift through parameters 1

Look at the above snapshot, this is our script.

Shell Scripting Shift through parameters 2

Look at the above snapshot, this is the output of the above script.


read command

The read command allows a user to provide the runtime input.

Shell Scripting Shift through parameters 3

Look at the above snapshot, this is our script using read command.

Shell Scripting Shift through parameters 4

Look at the above snapshot, a user can enter the name in the shell.

Shell Scripting Sourcing a config file

Many programs use external configuration files. Use of external configuration files prevents a user from making changes to a script. Config file is added with the help of source command.

If a script is shared in many users and every user need a different configuration file, then instead of changing the script each time simply include the config files.

Example:

We have two files, one is parent file (main.sh) and other is configuration file (config.sh). We have to source this configuration file into our parent file.

Script for config.sh

Shell Scripting Sourcing a config file 1

Script for main.sh

Linux Shell Scripting a config file 2

Look at the above snapshot, we've included config.sh file with source command.

Note: We can also use ( . config.sh ) command instead of ( source config.sh ) .

Now on running main.sh file, config.sh file is included.

Shell Scripting Sourcing a config file 3

Look at the above snapshot, on running main.sh file, content of config.sh file is imported via source command.

Shell Scripting getopts options

The getopts options are used in shell scripts to parse arguments passed to them. When arguments are passed on the command line, getopts parse those arguments instead of command lines.

Options are written starting with a hyphen (-), followed by the letter. For example, -a, -b, -c, -d, etc.

Example 1 without argument:

Linux Shell Scripting Get script options with getopts 1

Look at the above snapshot, we have created three options -a, -b and-c. And no arguments are required for any option.

Linux Shell Scripting Get script options with getopts 2

Look at the above snapshot, this is the sample output from the above script.

Example 2 with argument:

Linux Shell Scripting Get script options with getopts 3

Look at the above snapshot, argument is required with option b.

Linux Shell Scripting Get script options with getopts 4

Look at the above snapshot, this is the sample output of above script.

Shell Loops


The if then else condition loop states that if condition meets, output goes to if part otherwise it goes to else part.

The word fi represents if loop termination .

Syntax:

Syntax of if then else is shown in the snapshot below,

Linux Shell Scripting If then else 1

Example if then else:

We have shown the example of voting. If user's age will be greater than 18 then he or she will be eligible to vote, otherwise not.

  1. if condition:

  2. if [ "$age" -ge 18 ];

Linux Shell Scripting If then else 2

Look at the above snapshot, we have shown the script of file voter.

Linux Shell Scripting If then else 3

Look at the above snapshot, with age 17 it displays the message "you are younger !!" and with age 30 it displays the message "you are eligible to vote".

Shell Scripting if then elif

A new if can be nested inside an elif.

Syntax:

Syntax of if then elif is shown in the snapshot below,

Shell scripting If then elif 1

Example if then elif:

We have shown the example of choosing color.

Condition:

  1. if [ $color == Red ]  

  2. elif [ $color == Blue ]  

Shell scripting If then elif 2

Look at the above snapshot, we have shown the script.

Shell scripting If then elif 3

Look at the above snapshot, on Red color it goes to if part, on Blue color it goes to elif part and on other colors it goes to else part.

Shell Scripting for loop

The for loop moves through a specified list of values until the list is exhausted.

1) Syntax:

Syntax of for loop using in and list of values is shown below. This for loop contains a number of variables in the list and will execute for each item in the list. For example, if there are 10 variables in the list, then loop will execute ten times and value will be stored in varname.

Shell scripting For loops 1

Look at the above syntax:

  • Keywords are for, in, do, done

  • List is a list of variables which are separated by spaces. If list is not mentioned in the for statement, then it takes the positional parameter value that were passed into the shell.

  • Varname is any variable assumed by the user.

Example for:

We have shown an example to count 2's table within for loop.

Shellscripting For loops 2

Look at the above snapshot, our varname is table, list is specified under curly braces. Within the curly braces, first two will initialize the table from 2, 20 represents maximum value of $table and last 2 shows the increment by value 2.

Shellscripting For loops 3

Look at the above snapshot, it displays the 2's table as the output.

2) Syntax:

Syntax of for like C programming language.

Shellscripting For loops 4

Look at the above snapshot, condition1 indicates initialization, cond2 indicates condition and cond3 indicates updation.

Example for:

We have shown an example to count the number in reverse direction.

Shellscripting For loops 5

Look at the above snapshot, this is the loop script. $i will initialize with 10 and will go till 1, decrementing with 1 value.

Shellscripting For loops 6

Look at the above snapshot, this is the output of the script.

Shell Scripting while loop(condition statement is true)

Linux scripting while loop is similar to C language while loop. There is a condition in while. And commands are executed till the condition is valid. Once condition becomes false, loop terminates.

Syntax:

Syntax of while loop is shown in the snapshot below,

Linux Shell Scripting While loop 1

Example:

We have shown the example of printing number in reverse order.

Shell Scripting While loop 2

Output is displayed in the below snapshot,

Shell Scripting While loop 3


while infinite loop:

Infinite loop is also called endless loop. It is made with while true (it means condition will always be true) or while : (it means an empty expression), where colon (:) is equivalent to no operation.

Shell Scripting While loop 4

Look at the above snapshot, this script includes while truesyntax.

Linux Shell Scripting While loop 5

Look at the above snapshot, this script includes while: syntax.

Both of them display the same output.

Shell Scripting until loop(condtiona statement is false)

It is similar to while loop. The only difference is that until statement executes its code block while its conditional expression is false, and while statement executes its code block while its conditional expression is true.


Difference between while and until

Until loop always executes at least once. Loop while executes till it returns a zero value and until loop executes till it returns non-zero value.

Syntax:

Syntax of until loop is shown in the snapshot below:

Linux Shell Scripting Until loop 1

Example:

We have shown an example to display number from 5 to 15.

Linux Shell Scripting Until loop 2

Look at the above snapshot, it shows the script.

Linux Shell Scripting Until loop 3

Look at the above snapshot, it displays the output until the condition is false.

ADVANCE SHELL

Shell Functions

With the help of functions, overall functionality of a function can be divided into smaller or logical parts, which can be called to perform their task. It helps us to check our program part by part. We can reuse the function whereever we want.

To create a function

Syntax:

  1.  function functionName () {  

  2.         Commands to be executed  

  3. }  

You will call your function with their function name.

Example:

  1. function Welcome () {  

  2. }  

Linux Shell function 1

Look at the above snapshot, we have created a function Welcome. We have called it in the above script.

Linux Shell function 2

Look at the above snapshot, on executing the above script, function Welcome is called.


Passing Parameters

You can pass one or more parameters in a function. Parameters will be defined as $1, $2 and so on.

Linux Shell function 3

Look at the above snapshot, $1 is a parameter passed to the function.

Linux Shell function 4

Look at the above snapshot, script produces the above output.

Shell Scripting case

A case construct helps us to simplify nested if statement. You can match several variables against one variable. Each case is an expression matching a certain pattern.

Syntax:

Linux Shell Scripting Case 1

Look at the above snapshot, you can write one pattern or more than one pattern together according to the situation. Let's see an example to understand it more clearly.

Example:

Linux Shell Scripting Case 2

Look at the above snapshot, we have shown a script to show the capital of different states. States Punjab and Haryana are written together as they share same capital.

Output:

Linux Shell Scripting Case 3

Look at the above snapshot, user can enter a state name and script will display its capital respectively.

Shell Scripting eval command

The eval command is a built-in command. It takes a string as its argument and evaluate it, then run the command stored in the argument. It allows using the value of a variable as a variable.

Example 1:

  1. eval echo \${$User}  

Linux Shell Scripting eval 1

Look at the above snapshot, command "echo \${$User}" runs $User as a shell variable and displays its output.

But command "eval echo \${$User}" runs the parameter passed to eval. After expansion, remaining parameters are echo and ${Hello}. Hence eval command runs the command echo ${Hello}. And so the output is Mr. X

Double quotes must be used around variable and command substitution. Without the double quotes, shell may perform field splitting on different words of the variable.

Linux Shell Scripting eval 2

Example 2:

Linux Shell Scripting eval 3

Look at the above snapshot, we have passed a parameter (1 week ago) to the date command. This is the last week date and time displayed.

But when we set this command in a variable (lastweek) and run it, the command fails to print the date. Look down,

Linux Shell Scripting eval 4

Look at the above snapshot, command "$lastweek" fails whereas, command "eval $lastweek" successfully runs.


(( ))

This sign is mainly used for numerical evaluation. It is a compound command.

Linux Shell Scripting eval 5

Look at the above snapshot, single bracket ( ) gives error while double bracket (( )) successfully executes the command.

Shell Scripting let command

The let command is an arithmetic operator. It is almost same as (( )). Only difference is that, let is an arithmetic command while (( )) is a compound command.

It is a built-in command which instructs shell to perform an evaluation of arithmetic expressions. No spaces should be used around the arithmetic operant with let command.

Linux Shell Scripting let command

Look at the above snapshot, let command can be applied in different ways as shown.