Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unix Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

EXPERIMENT:01

AIM: Write a shell script to display current date, time, username and
directory.
PROGRAM:
echo Current Date:$(date +'%d-%m-%y')
echo Current Time:$(date +'%T')
echo Current UserName:$USER
echo Current Directory:$(pwd)
OUTPUT:
Current Date:05-02-23
Current Time:15:22:09
Current UserName:Abdullah
Current Directory:/home/Abdullah

PIN:20061-CM-017 Page No:1


EXPERIMENT:02

AIM: Write script to determine whether given file exist or not, file name is
supplied as Command line argument, also check for sufficient number of
command line argument
PROGRAM:
a=$1
echo Name of the file:$a
echo The Total Number Of The Arguments are:$#
if [ $# -ge 1 ]
then
if [ -f $a ]
then
echo $a File Existed
else
echo $a File Does Not Existed
fi
else
echo Number Of Argument Are In Sufficient
fi
OUTPUT:
Name of the file:f1
The Total Number Of The Arguments are:3
f1 File Existed

PIN:20061-CM-017 Page No:2


EXPERIMENT:03

AIM: Write a shell script that uses special variables related to a command line
PROGRAM:
echo The Total Number Command Line Arguments are:$#
echo Accessing Command Line Argument With The Help of $*
for token in $*
do
echo $token
done
echo Accessing Command Line Argument With The Help of $@
for token in $@
do
echo $token
done
echo Accessing Command Line Argument With The Help of "$*"
for token in "$*"
do
echo $token
done
echo Accessing Command Line Argument With The Help of "$@"
for token in "$@"
do
echo $token
done

PIN:20061-CM-017 Page No:3


OUTPUT:
The Total Number Command Line Arguments are:3
Accessing Command Line Argument With The Help of f1 f2 f3
f1
f2
f3
Accessing Command Line Argument With The Help of f1 f2 f3
f1
f2
f3
Accessing Command Line Argument With The Help of f1 f2 f3
f1 f2 f3
Accessing Command Line Argument With The Help of f1 f2 f3
f1
f2
f3

PIN:20061-CM-017 Page No:4


EXPERIMENT:04

AIM: Write a shell script to access the array values


PROGRAM:
a=(1 2 3)
n=${#a[@]}
echo Size of the array:$n
i=0
sum=0
while [ $i -lt $n ]
do
sum=$((sum+${a[$i]}))
i=$(($i+1))
done
echo Sum of Arrya Elements are=$sum
OUTPUT:
Size of the array:3
Sum of Arrya Elements are=6

PIN:20061-CM-017 Page No:5


EXPERIMENT:05

AIM: Write shell script to show various system configuration like:


a) Currently logged user name and his long name
b) Current shell
c) Your home directory
PROGRAM:
echo User Name:$USER
echo Current Shell:$SHELL
echo Home Directory:$HOME
OUTPUT:
User Name:Abdullah
Current Shell:/bin/bash
Home Directory:/home/Abdullah

PIN:20061-CM-017 Page No:6


EXPERIMENT:06

AIM: Write shell script to show various system configuration like:


a) Your operating system type
b) Your current path setting
c) Your current working directory
d) Show all available shells
PROGRAM:
echo Current operating system:$(uname)
echo current path:$PATH
echo current working directory:$(pwd)
echo Show all available shells:$(cat /etc/shells)
OUTPUT:
Current operating system:Linux
current path:/opt/swift/swift-5.7.3-RELEASE-
ubuntu22.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
current working directory:/home
Show all available shells:# /etc/shells: valid login shells /bin/sh /bin/bash
/usr/bin/bash /bin/rbash /usr/bin/rbash /usr/bin/sh /bin/dash /usr/bin/dash

PIN:20061-CM-017 Page No:7


EXPERIMENT:07

AIM: Write a Shell script to accept any two file names and check their file
permissions.
PROGRAM:
echo Enter The First File Name:
read f1
echo Enter The Second File Name:
read f2
if [ -f $f1 ]
then
echo $f1 File Existed And The Permissions Are :$(ls -l $f1)
else
echo $f1 File Does Not Existed
fi
if [ -f $f2 ]
then
echo $f2 File Existed And The Permissions Are :$(ls -l $f2)
else
echo $f2 File Does Not Existed
fi
OUTPUT:
Enter The First File Name:
f1
Enter The Second File Name:
f2
f1 File Existed And The Permissions Are :- -rw-r--r--1 Abdullah Abdullah 53 JAN 6 15:53 f1
f2 File Does Not Existed

PIN:20061-CM-017 Page No:8


EXPERIMENT:08

AIM: Write a Shell script to read a file name and change the existing file
permissions.
PROGRAM:
echo Enter The First File Name:
read f1
if [ -f $f1 ]
then
echo $f1 File Existed And The Permissions Are :$(ls -l $f1)
(chmod 777 $f1)
echo $f1 File Permissions Are Changed:$(ls -l $f1)
else
echo $f1 File Does Not Existed
fi
OUTPUT:
Enter The First File Name:
f1
f1 File Existed And The Permissions Are :- -rw-r--r--1 Abdullah Abdullah 53
JAN 6
15:56 f1
f1 File Permissions Are Changed:-rwxrwxrwx 1 ahad ahad 53 JAN 6 15:56 f1

PIN:20061-CM-017 Page No:9


EXPERIMENT:09

AIM: Write a shell script to read a file name and check if it is a directory or
block special file or character special file
PROGRAM:
echo Enter The First File Name:
read f1
if [ -d $f1 ]
then
echo It is a Directory Special File
elif [ -b $f1 ]
then
echo It is a Block Special File
elif [ -c $f1 ]
then
echo It is a Character Special File
else
echo Invalid File
fi
OUTPUT:
Enter The First File Name:
f1
It is a Directory Special File

PIN:20061-CM-017 Page No:10


EXPERIMENT:10
AIM: Write a shell script to replace next day with * in calendar
PROGRAM:
cal fcal1

echo befor replacement calendar is

(cat fcal1)

d=$(date + '%d')

echo current date $d

a=${d:0:1}

echo a=$a

b=${d:1:1}

b=$(($b+1))

echo b=$b

(sed -t "s/[$a][$b]/*/" fcal1)

OUTPUT:

PIN:20061-CM-017 Page No:11


EXPERIMENT:11

AIM: Write a shell program to illustrate command substitution.


PROGRAM:
echo Current Operating System:$(uname)
echo Current Path:$(PATH)
echo Current date:$(date)
OUTPUT:
Current Operating System:Linux
Current Path:/home/Abdullah
Current date: Mon Mar 6 04:05:35 PM UTC 2023

PIN:20061-CM-017 Page No:12


EXPERIMENT:12

AIM: Write a shell script to print all Arguments with script name and total
number of arguments passed
PROGRAM:
echo Current Script Name:$0
echo The Arguments Are:$*
echo The Total Number Of The Arguments Are:$#
OUTPUT:
Current Script Name:Demo.sh
The Arguments Are:f1 f2 f3
The Total Number Of The Arguments Are:3

PIN:20061-CM-017 Page No:13


EXPERIMENT:13

AIM: Write a shell script to access command line arguments by shifting


position
PROGRAM:
i=1
echo "The argument values without shift command:"
for value in "$@"
do
echo "Argument no. $i = $value"
((i++))
done
i=1
start=${1:-""}

echo "The argument values by using shift command:"


while [ "$start" != "" ]
do
echo "Argument no. $i = $start"
shift 2
start=$1
((i++))
done
OUTPUT:
The argument values without shift command:
Argument no. 1 = f1
Argument no. 2 = f2
Argument no. 3 = f3
Argument no. 4 = f4

PIN:20061-CM-017 Page No:14


Argument no. 5 = f5
Argument no. 6 = f6
Argument no. 7 = f7
Argument no. 8 = f8
Argument no. 9 = f9
Argument no. 10 = f10
The argument values by using shift command:
Argument no. 1 = f1
Argument no. 2 = f3
Argument no. 3 = f5
Argument no. 4 = f7
Argument no. 5 = f9

PIN:20061-CM-017 Page No:15


EXPERIMENT:14

AIM: Write a shell script to read two numbers and perform arithmetic
operations
PROGRAM:
echo Enter the first number
read num1
echo Enter the second number
read num2
add=$((num1 + num2))
sub=$((num1 - num2))
mul=$((num1 * num2))
div=$((num1 / num2))
echo "Addition of $num1 and $num2 is $add"
echo "Subtraction of $num1 and $num2 is $sub"
echo "Multiplication of $num1 and $num2 is $mul"
echo "Division of $num1 and $num2 is $div"
OUTPUT:
Enter the first number
1
Enter the second number
2
Addition of 1 and 2 is 3
Subtraction of 1 and 2 is is
Multiplication of 1 and 2 is
Division of 1 and 2 is is

PIN:20061-CM-017 Page No:16


EXPERIMENT:15

AIM: Write a shell script to read two numbers and check their relation using
relational operators
PROGRAM:
echo Enter the first number:
read num1
echo Enter the second number:
read num2
if [ "$num1" -eq "$num2" ]
then
echo "$num1 is equal to $num2"
elif [ "$num1" -gt "$num2" ]
then
echo "$num1 is greater than $num2"
elif [ "$num1" -lt "$num2" ]
then
echo "$num1 is less than $num2"
else
echo "Invalid Number"
fi
OUTPUT:
Enter the first number:
1
Enter the Second number:
2
1 is less than 2

PIN:20061-CM-017 Page No:17


EXPERIMENT:16

AIM: Write a shell script to read two numbers and apply Boolean operators(
logical AND,OR and negation) on them
PROGRAM:
echo "Enter first number: "
read num1
echo "Enter second number: "
read num2
if [ $num1 -gt 0 -a $num2 -gt 0 ]; then
echo "$num1 and $num2 are both positive numbers"
else
echo "At least one of the numbers is not positive"
fi
if [ $num1 -gt 0 -o $num2 -gt 0 ]; then
echo "At least one of the numbers is positive"
else
echo "Both numbers are not positive"
fi
if [ ! $num1 -eq 0 ]; then
echo "$num1 is not equal to zero"
else
echo "$num1 is equal to zero"
fi
OUTPUT:
Enter first number:
1
Enter second number:
2

PIN:20061-CM-017 Page No:18


1 and 2 are both positive numbers
At least one of the numbers is positive
1 is not equal to zero

PIN:20061-CM-017 Page No:19


EXPERIMENT:17

AIM: Write a shell script to read two strings and check whether the two strings
equal or not
PROGRAM:
echo Enter String1:
read str1
echo Enter String2:
read str2
if [ $str1 == $str2 ]
then
echo $str1 and $str2 are equal
else
echo $str1 and $str2 are Not equal
fi
OUTPUT:
Enter String1:
unix
Enter String2:
linux
unix and linux are Not equal

PIN:20061-CM-017 Page No:20


EXPERIMENT:18

AIM: Write a shell program to print the sum of first n natural numbers
PROGRAM:
echo Enter Size
read n
sum=0
for((i=1;i<=n;i++))
do
sum=$((sum+i))
done
echo sum=$sum
OUTPUT:
Enter Size
5
sum=15

PIN:20061-CM-017 Page No:21


EXPERIMENT:19

AIM: Write a shell program to check if the read number is Armstrong number
or not
PROGRAM:
echo Enter a number:
read n
num=$n
len=${#n}
sum=0
while [ $n -gt 0 ]
do
digit=$((n % 10))
power=$((digit ** len))
sum=$((sum + power))
n=$((n / 10))
done
if [ $sum -eq $num ]
then
echo $num is an Armstrong number
else
echo $num is not an Armstrong number
fi
OUTPUT:
Enter a number:
153
153 is an Armstrong number

PIN:20061-CM-017 Page No:22


EXPERIMENT:20

AIM: Write a shell program to factorial of a given number using for loop
PROGRAM:
echo Enter a number
read num
fact=1
for((i=1;i<=num;i++))
do
fact=$((fact*$i))
done
echo Factorial of $num is $fact
OUTPUT:
Enter a number
5
Factorial of 5 is 120

PIN:20061-CM-017 Page No:23


EXPERIMENT:21

AIM: Write a shell program to demonstrate select loop and case


PROGRAM:
echo Enter a Number
read $digit
case $digit in
0) echo "Zero"
;;
1) echo "one"
;;
2) echo "Two"
;;
3) echo "Three"
;;
4) echo "Four"
;;
5) echo "Five"
;;
6) echo "Six"
;;
7) echo "Seven"
;;
8) echo "Eight"
;;
9) echo "Nine"
;;
*) echo "Invalid Number"
;;

PIN:20061-CM-017 Page No:24


esac
OUTPUT:
Enter a Number
1
one

PIN:20061-CM-017 Page No:25


EXPERIMENT:22

AIM: Write a shell program to print the following output using nested loops
11111
22222
33333
44444
PROGRAM:
echo Enter Number Of rows
read r
echo Enter Number Of column
read c
for((i=1;i<=r;i++))
do
for((j=1;j<=c;j++))
do
echo -n $i
done
echo “ ”
done
OUTPUT:
Enter Number Of rows
4
Enter Number Of column
4
1111
2222
3333
4444

PIN:20061-CM-017 Page No:26


EXPERIMENT:23

AIM: Write a shell program to demonstrate the use of break and continue
PROGRAM:
for i in {1..10}
do
if [ $(($i%2)) -eq 0 ]
then
echo Even Numbers Are i=$i
continue
fi
if [ $i -eq 5 ]
then
echo Brak Loop At i=$i
break
fi
done
OUTPUT:
Even Numbers Are i=2
Even Numbers Are i=4
Brak Loop At i=5

PIN:20061-CM-047 Page No:27


EXPERIMENT:24

AIM: To Write a shell program to find various arithmetic operations using a


function
PROGRAM:
arithematic_operations(){
parm1=$1
parm2=$2
echo $parm1+$parm2 is $(($parm1+$parm2))
echo $parm1-$parm2 is $(($parm1-$parm2))
echo $parm1*$parm2 is $(($parm1*$parm2))
echo $parm1/$parm2 is $(($parm1/$parm2))
}
echo Enter two operands
read a b
arithematic_operations $a $b
OUTPUT:
Enter two operands
45
4+5 is 9
4-5 is -1
4*5 is 20
4/5 is 0

PIN:20061-CM-047 Page No:28


EXPERIMENT:25

AIM: To Write a shell program to find sum of n numbers using a function


PROGRAM:
sum_of_n_numbers(){
sum=0
for((i=0;i<=$1;i++))
do
sum=$(($sum+$i))
done
return $sum
}
echo Enter a number
read a
sum_of_n_numbers $a
echo sum of $a numbers is $?
OUTPUT:
Enter a number
5
sum of 5 numbers is 15

PIN:20061-CM-047 Page No:29


PIN:20061-CM-004 Page No:30

You might also like