0% found this document useful (0 votes)
74 views5 pages

Local and Global Variables

The document contains examples of shell script code that demonstrate various programming concepts: - Local and global variables in shell scripts and how they differ in scope - Performing arithmetic operations on user-input numbers - Using a for loop to calculate the sum of the first 5 numbers - Creating a menu-driven shell program using case statements - Checking if a user is logged in using who and grep - Printing a multiplication table using a while loop - Building a basic calculator script that evaluates expressions using user input operators and numbers - Calculating the factorial of a number with a for loop

Uploaded by

Mrinal Kaushik
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)
74 views5 pages

Local and Global Variables

The document contains examples of shell script code that demonstrate various programming concepts: - Local and global variables in shell scripts and how they differ in scope - Performing arithmetic operations on user-input numbers - Using a for loop to calculate the sum of the first 5 numbers - Creating a menu-driven shell program using case statements - Checking if a user is logged in using who and grep - Printing a multiplication table using a while loop - Building a basic calculator script that evaluates expressions using user input operators and numbers - Calculating the factorial of a number with a for loop

Uploaded by

Mrinal Kaushik
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/ 5

LOCAL AND GLOBAL VARIABLES:

#!/bin/bash

func()

{ local lvar="Local content

echo -e "Local variable value with in the function"

echo $lvar

gvar="Global content changed"

echo -e "Global variable value with in the function" echo $gvar

gvar="Global content

echo -e "Global variable value before calling function

echo $gvar

echo -e "Local variable value before calling function"

echo $lvar

func

echo -e "Global variable value after calling function"

echo $gvar

echo -e "Local variable value after calling function

echo $lvar

ARITHMETIC OPERATIONS ON 2 NUMBERS:

$ cat arithmetic.sh

#!/bin/bash

echo -n Enter the first number: ; read x

echo -n Enter the second number: ; read y

add=$(($x + $y))
sub=$(($x - $y))

mul=$(($x * $y))

div=$(($x / $y))

mod=$(($x % $y))

# print out the answers:

echo Sum: $add

echo Difference: $sub

echo Product: $mul

echo Quotient: $div

echo Remainder: $mod

SUM OF FIRST 5 NUMBERS

#!/bin/bash

let sum=0

for num in 1 2 3 4 5

do

let sum = $sum + $num

done

echo $sum

MENU DRIVEN PROGRAM

#!/bin/bash

clear ; loop=y

while [ $loop = y ] ;

do

echo Menu
echo D: print the date

echo W: print the users who are currently log on.

echo P: print the working directory

echo Q: quit.

echo

read choice

case $choice in

D | d) date ;;

W | w) who ;;

P | p) pwd ;;

Q | q) loop=n ;;

*) echo Illegal choice. ;;

esac

echo

done

CHECK IF A USER HAS LOGGED IN

#!/bin/bash

clear

echo -n "Enter name of the user :"

read user

c=$(who | grep -ci $user)

if [ $c -gt 0 ]

then

echo "User is logged in to the system"

else
echo "User is not logged in to the system"

fi

Shell Program to Print Multiplication Table

#!/bin/bash

clear

echo "Enter a Number"

read n

i=0

while [ $i -le 10 ]

do

echo " $n x $i = `expr $n \* $i`"

i=`expr $i + 1`

done

Shell script for a simple calculator.

#!/bin/bash
clear
echo "Enter number1: "
read n1
echo "Enter operator(+, -, /, *): "
read op
echo "Enter number2:"
read n2
if [ "$op" = "+" ];
then
calc=`echo $n1 + $n2|bc`
echo "$n1 + $n2 = $calc"
elif [ "$op" = "-" ];
then
calc=`echo $n1 - $n2|bc`
echo "$n1 - $n2 = $calc"
elif [ "$op" = "*" ];
then
calc=`echo $n1 \* $n2|bc`
echo "$n1 * $n2 = $calc"
elif [ "$op" = "/" ];
then
calc=`echo $n1 / $n2|bc`
echo "$n1 / $n2 = $calc"
else
echo "Invalid operator!"
fi

How do I write Shell Script to find factorial of a number

#!/bin/bash

fact=1
echo -e "enter a number"
read n
if [ $n -le 0 ] ; then
echo "invalid number"
exit
fi
#factorial logic
if [ $n -gt 0 ] ; then
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
fi
echo "The factorial of $n is $fact"

You might also like