0% found this document useful (0 votes)
6 views6 pages

Shell Script

Unix is a multi-user, multitasking operating system developed in the 1970s, with an interactive user interface known as the shell. The document outlines various shell types, shell scripts, and provides examples of shell scripting functions, conditional statements, loops, and mathematical computations. It emphasizes the utility of shell scripts for automating repetitive command executions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Shell Script

Unix is a multi-user, multitasking operating system developed in the 1970s, with an interactive user interface known as the shell. The document outlines various shell types, shell scripts, and provides examples of shell scripting functions, conditional statements, loops, and mathematical computations. It emphasizes the utility of shell scripts for automating repetitive command executions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Unix:UNIX is a popular multi-user, multitasking

operating system (OS) developed at Bell Labs in


the early 1970s.
_______________________________________________
_______________________________________________
_______________________________________________
________________

SHELL

Shell is a UNIX term for the interactive user


interface with an operating system. The shell
is the layer of programming that understands
and executes the commands a user enters. In
some systems, the shell is called a command
interpreter. A shell usually implies an
interface with a command syntax (think of the
DOS operating system and its "C:>" prompts and
user commands such as "dir" and "edit").

Shell Types

In Unix, there are two major types of shells −

Bourne shell − If you are using a Bourne-


type shell, the $ character is the default
prompt.

C shell − If you are using a C-type shell,


the % character is the default prompt.
_______________________________________________
_______________________________________________
_______________________________________________
__________________

SHELL SCRIPT
Usually shells are interactive that mean, they
accept command as input from users and execute
them. However some time we want to execute a
bunch of commands routinely, so we have type in
all commands each time in terminal.
As shell can also take commands as input from
file we can write these commands in a file and
can execute them in shell to avoid this
repetitive work. These files are called Shell
Scripts or Shell Programs. Shell scripts are
similar to the batch file in MS-DOS. Each shell
script is saved with .sh file extension eg.
myscript.sh
_______________________________________________
_______________________________________________
_______________________________________________
__________________

1) Function Call

#!/bin/bash

sum ()
{
x=$1
y=$2
k=0
k=$(( $x + $y ))
return $k
}
sum 7 5
echo "Sum of two no is:$?"
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________
2) Command line argument

#!/bin/bash
x=$1
y=$2
k=$(( $x + $y ))
echo "sum is:$k"
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________

3) If-else(Conditional statement)

#!/bin/bash
big_num()
{
x=$1
y=$2
z=$3
if [ $x -gt $y ] ; then
if [ $x -gt $z ] ; then
echo "Biggest no is:$x"
else
echo "Biggest no is:$z"
fi
else
if [ $y -gt $z ] ; then
echo "Biggest no is:$y"
else
echo "Biggest no is:$z"
fi
fi
}
big_num 2 3 4
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________

4) while loop

#!/bin/bash
x=5

while [ $x -ge 1 ] ; do
echo $x
x=$(( $x - 1 ))
done
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________

5) For loop

#!/bin/bash
printf "enter the no which table u want "
read x
y=1
k=0
echo "Multiplication table of $x is as
below"
for ((y=1;y<=10;y++)) do
k=$(( $x * $y ))
echo "$x X $y=$k"
done
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________

6) Switch statement

#!/bin/bash
x=$1
case $x in
1)
echo "this is level 1"
;;
2)
echo "this is level 2"
;;
3)
echo "this is level 3"
;;
*)
echo "this is my default level"
;;
esac
exit 0

_______________________________________________
_______________________________________________
_______________________________________________
________________

7) Mathematical computation

#!/bin/bash
a=5.66
b=8.67
c=`echo $a + $b|bc`
echo $c
exit 0
_______________________________________________
_______________________________________________
_______________________________________________
________________

8) List the content of current directory

#!/bin/bash
for x in `ls` ; do
echo $x
done
exit 0

You might also like