You are on page 1of 14

#Q1.Use the following commands: ls,cat,pwd,tty,cat,who,who am I ,rm,mkdir,rmdir,touch,cd ?

Ans. $ls command allows you to view a list of the files and folders in a given directory.

example:diwaker@MyComputer:~/ubunlist$ ls

File_permission armstrong circle.area eg1 factorial_positive leap_year newfile


reverse_no. who_com

LCD calculator compare eg4 fibonacci login prime_no. smallest.no

Merge3file calendar compare_file even_odd file1 menufile 'pyramid_*'


sum_digit

addition calendar_range date_time factorial largest-no. multi_table raise_power


sum_series

$cat #To show what inside in a file

diwaker@MyComputer:~/ubunlist$ cat eg1

echo "Hello everyone My name is Diwaker Marchhal"

$pwd #pwd stands for Print Working Directory.

It prints the path of the working directory,starting from the root.

diwaker@MyComputer:~/ubunlist$ pwd

/home/diwaker/ubunlist

$tty #tty command of terminal basically prints the file name of the terminal connected to standard
input.

diwaker@MyComputer:~/ubunlist$ tty

/dev/tty1

$rm #Remove file

$mkdir #Create directories

$rmdir #Remove directories

$touch #Used to update access and modification time

$cd #To move inside a subdirectory

example:diwaker@MyComputer:~$ cd ubunlist

diwaker@MyComputer:~/ubunlist$
Q2. Use the following commands: cal , cat(append), cat(concatenate), mv, cp, man, date?

Ans: cal #Display current month

exampel:diwaker@MyComputer:~/ubunlist$ cal

December 2021

Su Mo Tu We Th Fr Sa

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

#cat(append) add the data to the file without erasing existing data. And symbol is >>.

example:diwaker@MyComputer:~/ubunlist$ cat eg1

echo "Hello everyone My name is Diwaker Marchhal"

diwaker@MyComputer:~/ubunlist$ cat eg4

echo "I am working on ubuntu"

diwaker@MyComputer:~/ubunlist$ cat eg1 >> eg4

diwaker@MyComputer:~/ubunlist$ cat eg4

echo "I am working on ubuntu"

echo "Hello everyone My name is Diwaker Marchhal"

#cat(concatenate) display two files

example:diwaker@MyComputer:~/ubunlist$ cat eg1 eg4

echo "Hello everyone My name is Diwaker Marchhal"

echo "I am working on ubuntu"

# mv command is used to move and rename files and directories.

#cp stands for copy. This command is used to copy files or group of files or directory.

#man command isused to display the user manual of any command that we can run on the terminal

$date #Show current day , month , time and year

example: diwaker@MyComputer:~/ubunlist$ date


Tue Dec 7 20:54:17 IST 2021

#Q3. Use the following command: chmod, grep, tput(clear, highlight) , bc?

Ans: #chmod command is used to change the access mode of a file.

example: diwaker@MyComputer:~/ubunlist$ chmod +x LCD #now we acces this file

#grep command searches a file for a particular pattern of characters, and displays all

lines that contain that pattern.

example:diwaker@MyComputer:~/ubunlist$

diwaker@MyComputer:~/ubunlist$ grep "am" eg4

echo "I am working on ubuntu"

echo "Hello everyone My name is Diwaker Marchhal"

#tput(clear, highlight) tput clear command clear the screen and tput highlight the text

eg: tput clear

#bc command is used for command line calculator. It is similar to basic calculator

by using which we can do basic mathematical calculations.

#Q4.Write a shell script to check if the number entered at the command line Prime number or not?

#!\bin\bash

clear

echo " Prime number or not prime number "

read -p"Enter a number :" n

for((i=2;i<=$n/2;i++))

do

if [ $((n%i)) -eq 0 ]

then

echo "$n is not a prime number "

exit

fi

done

echo " $n is the prime number"


#Q5.Write a shell script to modify ‘cal’ command to display of the specified months?

#!\bin\bash

clear

echo "To find the Calendar of a specified month :"

read -p "Enter the Month :" month

read -p "Enter the Year :" year

cal $month $year

#Q6.Write a shell script to modify ‘cal’ command to display calenadars of the specified range of
month?

#!\bin\bash

clear

echo "Find the calendar upto specified range of month"

first=`echo $* |tr -s "."|cut -d"." -f1`

last=`echo $* |tr -s "."|cut -d"." -f2`

month=`echo $first | tr '[a-z]''[A-Z]'`

case $month1 in

JAN) m1=1;;

FEB) m1=2;;

MAR) m1=3;;

APR) m1=4;;

MAY) m1=5;;

JUN) m1=6;;

JUL) m1=7;;

AUG) m1=8;;

SEP) m1=9;;

OCT) m1=10;;

NOV) m1=11;;

DEC) m1=12;;

esac
month2=`echo $last | tr '[a-z]''[A-Z]'`

case $month2 in

JAN) m1=1;;

FEB) m2=2;;

MAR) m3=3;;

APR) m4=4;;

MAY) m5=5;;

JUN) m6=6;;

JUL) m7=7;;

AUG) m8=8;;

SEP) m9=9;;

OCT) m10=10;;

NOV) m11=11;;

DEC) m12=12;;

esac

if [ $m1 =gt $m2 ]

then

echo -e "Invalid range of month \n"

else

set `date`

y=`echo $6`

fi

#Q7.Write a shell script to accept a login name. If not valid login name display message “Entered
login name is Inavlid”?

#!\bin\bash

echo "To accept login name and display message....."

read -p "Enter a login name :" lname

if who -u | grep -q " ^$lname "


then

top -u "$user"

else

echo "Entered login name is invalid"

fi

#Q8.Write a shell script to display date in mm/dd/yy format?

#!\bin\bash

now="$(date)"

echo "current date and time %s\n" "$now"

now="$(date +'%m/%d/%Y' )"

echo "current date in mm/dd/yyyy formate %s\n " "$now"

#Q9.Write a shell script to display on the screen sorted output of “who” command along with total
number of users?

#!\bin\bash

clear

nuser=`who | wc -1`

echo "The stored result of who command is as :"

who | sort

echo "--------------------"

echo "The total number of users are : $nuser"

#Q10.Write a shell script to display the multiplication table of any number?

#!\bin\bash

echo "Multiplication Table."

read -p "Enter a number : " x

echo " The multiplication table of $x "


c=1

while [ $c -le 10 ]

do

m=$((x * c))

echo " $x $c = $m "

c=$((c + 1))

done

#Q11.Write a shell script to compare two files and if found equal asks the user to delete the
duplicate file?

#!\bin\bash

echo "Compare two files and if found equal then remove the dublicate file..."

read -p "Enter the first file name :" file1

read -p "Enter the second file name :" file2

if cmp -s -- "$file1" "$file2"

then

echo "same"

rm -i -- "$file2"

else

echo "Diffenent file"

fi

#Q12.Write a shell script to find the sum of digits of given number?

#!\bin\bash

echo " To find the sum of two digit number :"

read -p " Enter a number :" n

sum=0

if [ $n -lt 0 ]

then

echo "It is not a positive number "

else

while [ $n -gt 0 ]
do

k=$(( $n % 10 ))

n=$(( $n / 10 ))

sum=$(( $k + $sum ))

done

echo "The sum of two digit number is $sum"

fi

#Q13.Write a shell script to merge the contents of three files, sort the contents and then display
them page by page?

#!\bin\bash

echo "Merge three Files"

read -p "Enter first file name :" file1

read -p "Enter second file name :" file2

read -p "Enter third file name :" file3

out="outputs.$$"

count=0

if [ $# -ne 3 ]

then

echo "$(basename $0) file1 file2 file3"

exit 1

fi

if [! -f $file1 ]

then

echo "$file1 not a file!"

exit 2

fi

if [! -f $file2 ]

then

echo "$file2 not a file!"

exit 3

fi
if [! -f $file3 ]

then

echo "$file3 not a file!"

exit 4

fi

cat $file1 $file2 $file3 >> $out

count=$(cat $out | wc -w)

echo "$count words written to $out"

#Q14.Write a shell script to find the LCD (least common divisor) of two numbers?

#!\bin\bash

echo "To Find the LCD(Least Common Divisor): "

read -p "Enter the first number :" a

read -p "Enter the second number :" b

if [ $a -gt $b ]

then

num=$a

den=$b

else

num=$b

den=$a

fi

r=$(echo "($num % $den)"| bc)

while [ $r -ne 0 ]

do

num=$den

den=$r

r=$(echo "($num % $den)"| bc)

done

rev=$den
lcd=`expr $a \* $b / $rev`

echo "The LCD of $a and $b is : $lcd"

#Q15.Write a shell script to the performs the tasks of basic calculator?

#!/bin/bash

echo"CALCULATOR"

echo "Select your Arithemtic Option :"

echo " 1. Addition "

echo " 2. Subtraction"

echo " 3. Multiplication "

echo " 4. Division "

echo " 5. Exponent"

echo " 6. EXIT "

echo -n "Enter your option [1-6] :"

while :

do

read option

case $option in

1) echo "You have selected the option 1 Addition"

read -p "Enter a first number :" a

read -p "Enter a second number :" b

add=$(echo "($a + $b )"| bc)

echo "The sum of $a and $b is :$add";;

2) echo "You have selected the option 2 Subtraction"

read -p "Enter a number you want to subtract :" x

read -p "Enter a number from you want to subtract :" y

sub=$(echo "($x - $y)"| bc)


echo "The subtraction of $x from $y is : $sub";;

3) echo "You have selected the option 3 Multiplication"

read -p "Enter a number :" a

read -p "Enter a number :" b

multi=$(echo "($a * $b)"| bc)

echo "The multiplication of $a and $b is : $multi";;

4) echo "You have selected the option 4 Division ."

read -p "Enter a number you want to divide :" a

read -p "Enter a number you want to divide with :" b

div=$(echo "($a / $b)"| bc)

echo "The division of $a and $b is : $div";;

5)echo "You have selected the option 5 Exponent "

read -p"Enter the Number :" n

read -p"Enter the power you want :" p

power=$(echo "($n ^ $p)"|bc)

echo "The power of given number is $power ";;

6) echo "Quit..."

exit;;

esac

echo -n "Enter your option [1-6] :"

done

#Q16.Write a shell script to find the power of a given number?

#!\bin\bash

echo " Raise to the power "

read -p"Enter the Number :" n

read -p"Enter the power you want :" p


a=$(echo "($n ^ $p)"|bc)

echo "The power of given number is $a"

#Q17.Write a shell script to find the factorial of a given number?

#!\bin\sh

echo "To find the factorial of a number."

read -p "Enter a number :" n

f=1

while [ $n -gt 1 ]

do

f=$((f * n))

n=$((n - 1))

done

echo "The factorial = $f"

#Q18.Write a shell script to check whether the number is Armstrong or not?

#!\bin\bash

echo "Check the number is armstrong or not :"

read -p "Enter a number :" n

a=0

b=$n

while [ $n -ne 0 ]

do

dig=$(( $n % 10 ))

a=$(($a + $dig*dig*dig ))

n=$(($n / 10))

done

if [ $a -eq $b ]

then

echo " It is a armstrong "

else
echo " It is not a armstrong "

fi

#Q19.Write a shell script to check whether The file permission or not?

!\bin\bash

echo "To check whether the file have all permission or not..."

read -p "Enter the file name :" fname

if test -f "$fname"

then

if test -r $fname -a -w $fname -a -x $fname

then

echo "File has Read,Write and execute permission"

ls -l $fname

fi

else

echo "File doesn't Exit"

fi

#Q20.Program to show the pyramid of special character “*”?

#!\bin\bash

echo " Pyramid of Star(*)"

read -p "Enter a number: " n

for(( i=0 ; i<n ; i++))

do

for(( j=0 ; j<n ; j++))

do

if [ `expr $i + $j` -ge `expr $n - 1` ]

then

echo -n "*"

else

echo -n " "


fi

done

for((k=0 ; k<i ; k++))

do

echo -n "*"

done

echo -e "\n"

done

You might also like