"Unlock the Power of Shell Scripting: A Beginner's Guide"

"Unlock the Power of Shell Scripting: A Beginner's Guide"

Shell Script

In the simplest terms, a shell script is a file contaning a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.

Normally the shell scripts has the file extension .sh.

To create a shell script, you use a text editor. There are many, many text editors available for your linux sytem, both for the command line environment and the GUI environment. Here is a list of some common ones.

  • vi or vim is the command line interface text editor.

  • gedit is the GUI text editor.

Example: first shell script

#! /bin/bash
echo "Hello World"

o/p:

Hello World

The first line is called a shabang. It is nothing but the absolute path to the Bash interpreter . It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. If you do not specify an interpreter line, the default is usulayy the /bin/sh

In order to execute the shell script make sure that you have execute permission. Give execute permission as follows.

Case Sensitive:

As you know Linux is case sensitive, the file names, variables, and arrays used in shell scripts.

#! /bin/bash
string1=10
string2=20
echo " the value of string1 is $string1 "
echo " the value of string2 is $string2 "

o/p:

the value of string1 is 10

the value of string2 is 20

Comments:

  • Comments are used to escape from the code.

  • This part of the code will be ignored by the program interpreter.

  • Adding comments makes things easy for the programmer while editing the code in the future.

  • Single line comments can be done using #.

  • Multiline comments can be done using the HERE DOCUMENT feature.

#! /bin/bash
echo " hello world"  #this is basic shell script by anas
<<COMMENT1
i am commenting here
using here document feature
COMMENT1
echo " multiple comment done "

o/p:

hello world

multiple comment done

User defined Variables:

These variables created and maintained by user. This type of variable defined may use any valid variable name, but it is good practice to avoid all uppercase names as many are used by shell.

#! /bin/bash
training_course=DEVOPS
echo ' displaying the user defined variable value is:' $training_course

o/p:

displaying the user defined variable value is: DEVOPS

Command line Argument:

During shell script execution, value passing through command prompt is called as command line arguments. For example while running a shell script, we can specify the command line arguments as ". file.sh arg1 arg2 arg3"

while using command line arguments follow the below important files.

  • we can specify n number of arguments, there is no limitations.

  • Each argument is separated by space.

#! /bin/bash
echo "username: $1"
echo "age: $2"
echo "fullname: $3"

o/p:

username: ilahianas

age: 24

fullname: Anas_Ansari

Strings:

Strings are scaler and there is no-limit to the size of a string. Any characters, symbols, or words can be used to makeup your string.

  • How to define a string?

we use single or double quotations to define a string.

"Double Quotes"- Anything enclose in double quotes removed meaning of that characters (except \ and $).

'Single Quotes'- Enclosed in single quotes remains unchanged.

#! /bin/bash
MYVAR=sometext
echo "double quotes gives you $MYVAR"
echo 'single quotes gives you $MYVAR'

o/p:

double quotes gives you sometext

single quotes gives you $MYVAR

User interaction using Read command:

  • In some cases, the script needs to interact with the user and accept inputs.

  • In shell scripts, we use the read statement to take input from the user.

  • read - read command used to get the input from the user (making scripts interactive)

#! /bin/bash
echo "please eneter your name"
read username
echo the name you entered is $username

o/p:

please enter your name

Anas Ansari

the name you entered is Anas Ansari

Conclusion:

Shell scripting is a powerful tool for automating tasks in a Linux environment. By understanding the basics such as creating scripts, using comments, defining variables, handling command line arguments, and interacting with users, you can significantly enhance your productivity and efficiency. Whether you are a beginner or an experienced user, mastering shell scripting can open up a world of possibilities for managing and optimizing your system.