Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Intro To Shell Scripts

Uploaded by

kkrrcb.8745
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Intro To Shell Scripts

Uploaded by

kkrrcb.8745
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

TOPIC:

Bash script/ shell script & Shell programming


________________________________________________
To know where the bash is located type the command:
$ which bash
Output:
/bin/bash
Touch command: - is used to create a text file. (file can be written and
edited using any text editor like-nano, vim, vi editor)
Steps to write and execute a script.
1. Open terminal.
2. Go to the directory where you want to create your script or create any directory
where your file will store.
3. Create a file with .sh extension.
4. Write the script in the file using any editor. (vi, vim, nano)
Syntax: $nano demofile .sh (hit enter)
File created and opened in a text editor
or
$ touch demo.sh
File will be created but doesn’t open, open it yourself using any editor

Write Script to display a message:


#!/bin/bash
Echo “Hello world”

5. Save the file (CTRL+X, press Y)


6. Run this file on terminal
7. Output: access denied:
8. Make this file executable with command
Syntax: chmod +x demo.sh
9. Now execute this file again using following command
10. ./demo.sh
11. Output: Hello world

Steps to execute a shell script in Linux


The procedure is as follows:
 Create a new file called demo.sh using a text editor such as nano or vi in Linux:
nano demo.sh
 Add the following code:
#!/bin/bash
echo "Hello World"
 Set the script executable permission by running chmod command in Linux:
chmod +x demo.sh
 Execute a shell script in Linux: ./demo.sh
Again explaining:-
Open the terminal and clear the screen.
$ touch hello.sh
(sh is not necessary to write)
Syntax:-
$ touch filename.sh
(it will create a file)
Now you can open the file which you have recently created using touch
command.
File can be opened using any text editor available on your system. And now you
can write your shell script here in this file.
Your First Script:
#!/bin/bash (this lines tells the location of your shell and
name of the shell that is bash)
Echo “Hello world” (echo command is used to display output or
massage on the screen)
Above script have two lines which are:
1. First line is for the location
2. Second line is for echoing or showing the massage i.e Hello world
Now let’s execute the script:
1. Go to the terminal
2. Go to the path where you have created file using cd command.
3. Run the command
$ ./hello.sh
Output:
Bash:./hello.sh: permission denied
This massage will come when you first time execute your script, as the file
is not on executable mode. You need to make it executable after running
the following command.
$ chmod +x hello.sh
$ ls –al or $ll (this command shows read/write permissions)
$ ./hello.sh (now your file will execute)

(Screen to execute chmod command)

Output:
Hello world
$touch try.sh
or
$nano try.sh
//nano editor opened//
//Write the script in this editor//
echo “hello Linux”
echo “please enter name:”
read name
echo “entered Name is: $name”
echo “thats it”

date
Save = CTRL+X, Y
Execute on terminal:
$./try.sh
Output:
Hello Linux
Please Enter name:
Enterd name is: jims
Thats it
Thurs Apr 16 03:55:17
Now run the write the following in your script with echo
echo $ BASH VERSION

echo $ HOME

echo$ PWD

OUTPUT OF THE ABOVE SCIRPT:

1. Shows the bash version


2. Shows the home directory
3. Shows the present working directory

READ INPUT FROM YOUR TERMINAL IN SCRIPT


READ

Read takes input from the keyboard & assign it to a variable

To get input from the keyboard you use read command

See how to use it:-

//Allow user to enter name//

echo “Enter name :”

read name

echo “Entered Name is :$name”

Go to terminal-

Enter name:

Aparna

Entered Name is : Aparna

//Another program to take user input using multiple variable//

echo “Enter Names :”

read name1 name2 name3

echo “Entered Names are :$name1,$name2,$name3”

Execute your file now on the terminal

$ ./file.sh

Enter Names:

Aparna Ayushi shefali

What is comment?
We can write # (hash) when we want to write a comment in a
script. Line starting with # would not execute.
Example for showing the system variables and user defined variables:
Use of if...
Keywords:
if, else, then, elif, fi

//Write Shell Script to compare, if numbers are equals//


Nano editor

Vi editor
If [ $s != $b ]; also can be used

//To compare variable string//


Username=$ (whoami)
echo $username
if [[ “${username}” == “root” ]];
then
echo “you are root”
else
echo “you are not root”
fi

Output:
root
you are logged in with root

Output:
root
you are not root
//comparing age using if-else//
Loops
Keywords: for, while, do, done, in
Syntax of for loop
Example.1

Example.2

Example.3
Note- ..Increment option can be used in above version 4.0
To Bash version – echo ${BASH_VERSION}

You can use “for loop” in various types according to your


requirement.
While loop
Syntax:

#While (( $n <= 10 )) - for these parenthesis you can use (<=) rather (–le)
#(( n++ )) - For increament it also can be used
Output:
1
2
3
4
5
6

Output:
1

Output:
1
2

//Print 0 to 10 numbers using while loop//


//Program to find factorial//

No=10
V=$no
While [ $a –lt $no ]
Do
Temp= ‘expr $temp \* $no’
No= ‘expr $no ‘ 1
Done
Echo “Factorial of $v is $temp”
Run:
$ sh whileloop.sh
Output:
0
1
2
3
4
5
6
7
8
9
10

// write a shell script to check number is Prime or Not//


// write a shell script to find reverse of number //

Output

//write shell scrip to display number is Odd or even//

Output:
//write a shell script to find sum of Digits//

Output:

//write a Shell script to find Fibonacci series//

Output:
//write a shell script to find numbers are equals and to find the largest
number//

Output:

Enter three numbers

11

11

11

Numbers are equal

$ ./demo.sh

Enter three Numbers

12

13

14

3rd number is larger


Passing value to variables

Passing arguments as an array

You might also like