Practical Answer
Practical Answer
prime or not.
fi
fi
fi
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.
cal -m june
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.
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.
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.
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
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.
read name
username='anshulkansal'
if [ $name = $username ]
then
echo "Valid"
else
fi
output:Enter usernameubuntu
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:
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:
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:
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:
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:
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
sum=0
echo $sum
//Calculator programme
# !/bin/bash
read a
read b
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`
;;
;;
esac
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
#main script
Output
# Shell Program to find Largest of Three Numbers
clear
read a
read b
read c
then
then
then
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