Unix Record
Unix Record
Unix Record
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
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
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
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
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
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
(cat fcal1)
d=$(date + '%d')
a=${d:0:1}
echo a=$a
b=${d:1:1}
b=$(($b+1))
echo b=$b
OUTPUT:
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
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
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
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
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
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
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
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
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
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