• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Thursday, November 12, 2009

Posted by venu k
5 comments | 1:17 AM

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

5 comments:

  1. nice work!
    i really very impress with ur way to explain.

    ReplyDelete
  2. is_leap_year() { ## USAGE: is_leap_year [year]
    ily_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
    }

    ReplyDelete
  3. Print a reverse the number

    echo "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

    ReplyDelete
  4. 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
  5. Here you can learn a lot. Cool that I hit this blog.

    ReplyDelete