# Method 1:

#!/bin/bash

#

# SCRIPT : leapyear.sh

# PURPOSE: Checks whether given year is a leap year or not

# USAGE : sh leapyear.sh

# If Year value not supplied, it takes current year as default

#

# This script based on, One year has the length of 365 days, 5 hours,

# 48 minutes and 47 seconds. Because this is rather non-functional,

# a normal year has been given 365 days and a leap year 366 days.

#####################################################

############### DEFINE FUNCTIONS HERE ####################

#####################################################

Leap()

{

# This function tells you if the argument is a leap year or not...

YEAR=$1

NO_OF_DAYS=$(cal $YEAR |egrep "^[ 0-9][0-9]| [ 0-9][0-9]$" |wc -w)

# A single parameter to cal specifies the year (1 - 9999) to be displayed;

# note the year must be fully specified: “cal 89” will not display a calendar

# for 1989.

# Use egrep or grep -E both are same

# cal command pipes total months and dates to egrep, egrep remove all lines

# not starting with number or ending with number, then pipes remaining lines

# to wc command. wc command counts total number of words(days).

if [[ $NO_OF_DAYS -eq 365 ]]

then

echo "$YEAR Is Not A Leap Year "

else

echo "$YEAR Is A Leap Year"

fi

}

#####################################################

############### MAIN PROGRAM STARTS HERE #################

#####################################################

Year=${1:-$(date +%Y)}

if [ $Year -lt 1 -o $Year -gt 9999 ]

then

echo "Illegal Year Value: Use 1-9999"

exit 1

fi

Leap $Year

# Method 2:

#!/bin/bash

#

# SCRIPT : leapyear.sh

# PURPOSE: Checks whether given year is a leap year or not

# USAGE : sh leapyear.sh

# If Year value not supplied, it takes current year as default

#

# This script based on,A leap year comes every 4 years,

# but not every 100 years, then again every 400 years.

#####################################################

############### ARGUMENTS CHECKING #####################

#####################################################

YEAR=${1:-`date +%Y`}

if [ $YEAR -lt 1 -o $YEAR -gt 9999 ]

then

echo "Illegal Year Value: Use 1-9999"

exit 1

fi

#####################################################

############### DECLARE VARIABLES HERE ###################

#####################################################

rem1=$((YEAR%4))

rem2=$((YEAR%100))

rem3=$((YEAR%400))

#####################################################

############## MAIN PROGRAM STARTS HERE ##################

#####################################################

if [ ${rem3} = "0" ]

then

echo "$YEAR Is A Leap Year"

exit

fi

if [ ${rem2} = "0" -a ${rem3} != "0" ]

then

echo "$YEAR Is Not A Leap Year"

exit

fi

if [ ${rem1} = "0" -a ${rem2} != "0" ]

then

echo "$YEAR Is A Leap Year"

else

echo "$YEAR Is Not A Leap Year"

fi

**OUTPUT:**

[root@localhost shell]# ./leapyear.sh

2009 Is Not A Leap Year

[root@localhost shell]# ./leapyear.sh 2100

2100 Is Not A Leap Year

[root@localhost shell]# ./leapyear.sh 2104

2104 Is A Leap Year

[root@localhost shell]# ./leapyear.sh 21000

Illegal Year Value: Use 1-9999

[root@localhost shell]# ./leapyear.sh 1996

1996 Is A Leap Year

nice work!

ReplyDeletei really very impress with ur way to explain.

is_leap_year() { ## USAGE: is_leap_year [year]

ReplyDeleteily_year=${1:-`date +%Y`}

case $ily_year in

*0[48] |\

*[2468][048] |\

*[13579][26] |\

*[02468][048]00 |\

*[13579][26]00 ) _IS_LEAP_YEAR=1

return 0 ;;

*) _IS_LEAP_YEAR=0

return 1 ;;

esac

}

Print a reverse the number

ReplyDeleteecho "Enter a number"

read n

sd=0

rev=0

while [ $n -gt 0 ]

do

sd=$(( $n % 10 ))

rev=$(( $rev *\ 10 + $sd ))

n=$(( $n / 10 ))

done

echo "Reverse number of entered digit is $rev"

$ vi fibonacci.sh echo “Enter n for Fibonacci series: - “

read n

echo “Fibonacci series is: - “

echo “0”

echo “1”

i=0

j=1

cnt=2

while [ $cnt –le $n ]

do

k=`expr $i + $j`

i= $j

j= $k

echo $k

cnt=`expr $cnt + 1`

done

Run the above script as follows: $ /bin/sh fibonacci.sh

Code:

Output –

Enter n for Fibonacci Series:-

10

Fibonacci Series is:-

0

1

1

2

3

5

8

clear

echo "How many number of terms to be generated ?"

read n

x=0

y=1

i=2

echo "Fibonacci Series up to $n terms :"

echo "$x"

echo "$y"

while [ $i -lt $n ]

do

i=`expr $i + 1 `

z=`expr $x + $y `

echo "$z"

x=$y

y=$z

done

Write a shell program in unix to add two numbers?

#!/bin/sh

echo “Enter the number”

A=5

B=10

C=`echo "$A + $B"|bc -l`

echo "$A + $B = $C"

Or

echo "enter the value of a"

read a

echo "enter the value of b"

read b

c=`expr $a + $b`

echo "the sum of a and b is $c"

shell script for check whether the given no is prime or not??

echo "input any number:"

read no

no=`expr $no`

i=`expr 2`

while [ $i -lt $no ]

do

if [ `expr $no % $i` -eq 0];

then

echo "$no is not a prime no.."

break 2

fi

i=`expr $i +1`

done

if [ $i -eq $no ];

then

echo "$no is a PRIME no..."

fi

factorial of a number in Unix

echo "Total no of factorial wants"

read fact

ans=1

counter=0

while [ $fact -ne $counter ]

do

counter=`expr $counter + 1`

ans=`expr $ans \* $counter`

done

echo "Total of factorial is $ans"

-------------------------------------------------------------------

output

-------------------------------------------------------------------

Total no of factorial wants

5

Total of factorial is 120

Or

clear

i=1

f=1

echo "Enter the n value:"

read n

while [ $i -le $n ]

do

f=`expr $f \* $i`

i=`expr $i + 1`

done

echo "The factorial of $n is":$f

/*-------------------------INPUT/OUTPUT----------

Enter the n value:

6

The factorial of 6 is:720

Grateful to check out your website, I seem to be ahead to more excellent sites and I wish that you wrote more informative post for us. Well done work.

ReplyDelete