You are on page 1of 17

Write a shell script to check if the number entered at the command line is

prime or not.

echo "enter number"


read num
function prime
{
for((i=2; i<=num/2; i++))
do
if [ $((num%i)) -eq 0 ]
then
echo "$num is not a prime number."
exit
fi
done
echo "$num is a prime number."
}
r=`prime $number`
echo "$r"

Method 1: Using cal command


If a user wants a quick view of the calendar in the Linux terminal, cal is the command for you. By default, the
cal command shows the current month calendar as output.
The cal command in Linux is used to view the calendar for a specific month or for the entire year. A single
argument in the cal command defines the four-digit year (1-9999) to be shown. Month (1-12) and Year(1 –
9999) is represented by two parameters. If no arguments are given, then the current month is shown.
We can use the following command to know more about cal utility in Linux:
man cal
For example:
Approach:
1. Start
2. Check whether arguments are passed or not
3. If no arguments are passed then display “Invalid arguments and exit program”
4. If 2 arguments are passed then
5. check whether 1st arguments are greater than 12 and the second argument is greater than 2021
(current year).
6. If it is greater, then display”invalid year or month” and go to step 12
7. Else if only one argument is passed then check whether it is greater than 12.
8. If it is greater than 12 then it is considered a year.
9. Then display a calendar of the specified year.
10. else if the argument is smaller than 12 then the passed argument is a month.
11. Display calendar of the specified month of the current year.
12. Exit.
Below is the implementation using cal command:-
# Shell Script to Enhance the Calendar to Accept
# MM, MMM, YYYY using cal command

# check whether arguments are passed or not


if [ $# -eq 0 ]
then

# if arguments are not passed then display this


echo "Invalid Arguments"

# exit the program


exit
fi

# if 2 arguments are passed


if [ $# -eq 2 ]
then

# if argument 1 is greater than 12 or argument 2


# is greater than 2021
if [ $1 -gt 12 -o $2 -gt 2021 ]
then

# then display invalid month or year


echo "invalid Year or month"

# else display calendar of the specified month


# and year
else
ncal $1 $2
fi

# if only one argument is passed then


else if [ $# -eq 1 ]
then

# if argument is greater than 12


if [ $1 -gt 12 ]
then
cal $1 # display calendar of specified year

# else display calendar of specified month


else
case $1 in #start switch case
01) m = jan;;
02) m = feb;;
03) m = mar;;
04) m = apr;;
05) m = may;;
06) m = jun;;
07) m = jul;;
08) m = aug;;
09) m = sep;;
10) m = oct;;
11) m = nov;;
12) m = dec;;
esac # end switch case
echo \" Calander for $1 Month : \"

# display calendar of specified month using -m


cal -m $1

fi
fi
fi

1. Print the current year’s calendar

You can print the calendar of the entire current year using the -y option of cal command.

cal -y

You can also specify the year to print the calendar of a particular year.

cal -y <YYYY>
2. Print the calendar of a month in current year

Suppose you want to print the calendar of June, this year. You can do that in several ways.

You can specify the month’s name.

cal -m june

You can also specify the month’s number.

cal -m6

View a
specific month

Note that only first three letters of a month’s name are taken into account. So cal -m jun, cal -m june and cal -m junta,
all three will yield the same output.

3. Print the calendar of a particular month and/or year

If you want to print the calendar of a given month an year, you can do so by providing the month name or number
and the year.

For example, to print the calendar of June 2017, you can use any of the below three formats.

 cal june 2017


 cal 6 2017
 cal -d 2017-6
4. Print three months view

You can print the previous month, current month and the next month’s calendar in one go with -3 option.

Previous
month, current month and the next month in one view

You can specify the month and the year as well with the -3 option.

cal -3 june 2017

5. Print the calendar with specific number of months before and after a
certain month

If you are not satisfied with the three months view, you can specify a custom range of months before and after a
certain months.

For example, if you want to print the calendar of two months before and six months after the current month, you can
use this command:
cal -A6 -B2

You can combine this with any month of any year.

cal -A1 -B1 june 2015

Bonus Tip: Print the calendar with day numbers

You can print the calendar in day numbers with -j option.

What I mean is that instead of giving you the day of the month, it will display the day in the year.

So the 15th of Feb will be displayed as 46, 16th February as 47 and so on.

You can combine -j option with other options as well.


Write a shell script to accept a login name. If not a valid login namedisplay
message – “Entered login name is invalid

echo "Enter username"

read name

username='anshulkansal'

if [ $name = $username ]

then

echo "Valid"

else

echo "Entered login name is invalid"

fi

output:Enter usernameubuntu

Entered login name is invalid

date command in Linux with examples


date command is used to display the system date and time. date command is also used to set date and time
of the system. By default the date command displays the date in the time zone on which unix/linux operating
system is configured.You must be the super-user (root) to change the date and time.
Syntax:

date [OPTION]... [+FORMAT]


date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Options with Examples
1: date (no option) : With no options, the date command displays the current date and time, including the
abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time
zone name, and the year.

Command:
$date
Output:
Tue Oct 10 22:55:01 PDT 2017
Note : Here unix system is configured in pacific daylight time.
2:-u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time
zone.

Command:
$date -u
Output :
Wed Oct 11 06:11:31 UTC 2017
3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the
system’s actual date and time value.Rather it uses the date and time given in the form of string.
Syntax:

$date --date=" string "

Command:
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output:
Tue Feb 2 00:00:00 PST 2010
Tue Feb 2 00:00:00 PST 2010
4:Using –date option for displaying past dates:

 Date and time of 2 years ago.

Command:
$date --date="2 year ago"
Output:
Sat Oct 10 23:42:15 PDT 2015

 Date and time of 5 seconds ago.

Command:
$date --date="5 sec ago"
Output:
Tue Oct 10 23:45:02 PDT 2017

 Date and time of previous day.

Command:
$date --date="yesterday"
Output:
Mon Oct 9 23:48:00 PDT 2017

 Date and time of 2 months ago.

Command:
$date --date="2 month ago"
Output:
Thu Aug 10 23:54:51 PDT 2017

 Date and time of 10 days ago.

Command:
$date --date="10 day ago"
Output:
Sat Sep 30 23:56:55 PDT 2017

5:Using –date option for displaying future date:

 Date and time of upcoming particular week day.

Command:
$date --date="next tue"
Output:
Tue Oct 17 00:00:00 PDT 2017

 Date and time after two days.

Command:
$date --date="2 day"
Output:
Fri Oct 13 00:05:52 PDT 2017

 Date and time of next day.

Command:
$date --date="tomorrow"
Output:
Thu Oct 12 00:08:47 PDT 2017

 Date and time after 1 year on the current day.

Command:
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018
6:-s or –set Option: To set the system date and time -s or –set option is used.
Syntax:

$date --set="date to be set"

Command:
$date
Output:
Wed Oct 11 15:23:26 PDT 2017
Command:
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output:
Tue Nov 13 15:23:34 PDT 2018
7:–file or -f Option: This is used to display the date string present at each line of file in the date and time
format.This option is similar to –date option but the only difference is that in –date we can only give one date
string but in a file we can give multiple date strings at each line.
Syntax:

$date --file=file.txt
$cat >> datefile
Sep 23 2018
Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov 3 00:00:00 PDT 2019
8:-r Option: This is used to display the last modified timestamp of a datefile .
Syntax:

$date -r file.txt
We can modify the timestamp of a datefile by using touch command.

$touch datefile

$date -r datefile
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date -r datefile
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile
9: List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.


%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.
Syntax:
$date +%[format-option]
Examples:

Command:
$date "+%D"
Output:
10/11/17
Command:
$date "+%D %T"
Output:
10/11/17 16:13:27
Command:
$date "+%Y-%m-%d"
Output:
2017-10-11
Command:
$date "+%Y/%m/%d"
Output:
2017/10/11
Command:
$date "+%A %B %d %T %y"
Output:
Thursday October 07:54:29 12 17

write a shell script program to generate multiplication table

echo "which number to generate multiplication table"


read number
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done
//Sum of all digits - Shell Script
echo "Enter a number"
read num

sum=0

while [ $num -gt 0 ]


do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done

echo $sum

//Calculator programme

# !/bin/bash

# Take user Input

echo "Enter Two numbers : "

read a

read b

# Input type of operation

echo "Enter Choice :"

echo "1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

read ch
# Switch Case to perform

# calculator operations

case $ch in

1)res=`echo $a + $b | bc`

;;

2)res=`echo $a - $b | bc`

;;

3)res=`echo $a \* $b | bc`

;;

4)res=`echo "scale=2; $a / $b" | bc`

;;

esac

echo "Result : $res"

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


#!/bin/bash

#function to find "a" to the power "b"

power()

num=$1

pow=$2

counter=1

result=1

if((pow==0)); then

result=1

fi

if ((num==0)); then

result=0

fi
if((num>=1&&pow>=1)); then

while((counter<=pow))

do

result=$((result*num))

counter=$((counter + 1))

done

fi

#Printing the result

echo "$1 to the power $2 is $result"

#main script

read -p "Enter number:" num

read -p "Enter power:" pow

#calling above function

power $num $pow

Output
# Shell Program to find Largest of Three Numbers

clear

echo "Enter first number: "

read a

echo "Enter second number: "

read b

echo "Enter third number: "

read c

if [ $a -gt $b ] && [ $a -gt $c ]

then

echo "$a is greater"

elif [ $b -gt $a ] && [ $b -gt $c ]

then

echo "$b is greater"


elif [ $c -gt $a ] && [ $c -gt $b ]

then

echo "$c is greater"

fi

//factorial of anumber

#!/bin/bash
2
# A shell script to find the factorial of a number
3
4
read -p "Enter a number" num
5
fact=1
6
7
while [ $num -gt 1 ]
8
do
9
fact=$((fact*num))
10
num=$((num-1))
11
done
12
13
echo $fact
//Armstrong number

#!/bin/bash
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi

You might also like