• 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.

Tuesday, October 27, 2009

Posted by venu k
23 comments | 8:41 AM

Method 1:


#!/bin/bash
# fact1
# Finding factorial of a given number
#

echo "Enter a number"
read num
fact=1
n=$num
while [ $num -ge 1 ]
do

fact=`echo $fact \* $num|bc`

# You can use bellow commands also
# fact=$((fact*num))
# fact=`expr $fact \* $num`
# But maximum integer value that bash can handle is
# 9223372036854775807.
# An operation that takes a variable outside these
# limits will give an erroneous result. To solve this
# problem store numbers as strings and use bc for math.


let num--

done
echo "factorial of $n is $fact"

Method 2:


#!/bin/bash
# fact2
# Usage : sh fact2 Number

factorial ()
{

local number=$1
if [ $number -eq 0 ]
then
Factorial=1
else
let "next = number - 1"
factorial $next
Factorial=`echo $number \* $Factorial | bc`
# let "Factorial = $number * $Factorial"
fi
return $Factorial 2>/dev/null
}

# Bash function returns integer value only.
# But maximum integer value that bash can handle is
# 9223372036854775807.
# An operation that takes a variable outside these limits
# will give an erroneous result. That's why I redirected
# stderror output to /dev/null


# Main program starts here.
if [ $# -ne 1 ]
then
echo "Invalid Arguments"
echo "Usage: ./fact2.sh Number "
exit 1
fi
factorial $1
echo "Factorial of $1 is $Factorial"

Output:
[root@localhost shell]# ./fact1
Enter a number
8
factorial of 8 is 40320
[root@localhost shell]# ./fact1
Enter a number
23
factorial of 23 is 25852016738884976640000

[root@localhost shell]# ./fact2
Invalid Arguments
Usage: ./fact2.sh Number
[root@localhost shell]# ./fact2 7
Factorial of 7 is 5040
[root@localhost shell]# ./fact2 33
Factorial of 33 is 8683317618811886495518194401280000000

23 comments:

  1. when i run method 2 in terminal. my terminal just closes without any result..

    ReplyDelete
  2. I think you are running script in current shell without arguments, I mean

    . fact

    after parameter testing "exit 1 " terminates your shell. Try to use following formats

    ./fact
    sh fact

    ReplyDelete
  3. thanx i got concept of recursion in bash shell script,which i tried to learn from to many places but in vain........

    ReplyDelete
  4. hi. i need shell program to print all the prime numbers between 1 to 100. can u give the coding? pls..

    ReplyDelete
  5. I already posted it here
    http://bashscript.blogspot.com/2009/11/shell-script-to-produce-prime-numbers.html

    ReplyDelete
  6. Great Job... Thanks for your valuable job.. i reached a right place...


    www.tipsinside.com

    ReplyDelete
  7. nice tips i can learn very well

    ReplyDelete
  8. good job yar and thank you....!

    ReplyDelete
  9. good job.... bharath

    ReplyDelete
  10. fact=`echo $fact \* $num|bc

    before * there is \ ..cany anybody give me the explanation regarding this..please

    ReplyDelete
  11. it is to remove the effect of special character *

    ReplyDelete
  12. can any one post the shell script to find the factorial of a given number using for loop

    ReplyDelete
  13. clear
    echo -n " enter a no. "
    read n
    i=1
    mul=1
    until [ $i -gt $n ]
    do
    mul=`expr $mul \* $i `
    i=`expr $i + 1 `
    done
    echo " factorial of $n is $mul "

    ReplyDelete
  14. There is no need to call bc more than once. An Optimization would be:

    #!/bin/bash
    if [ $# -ne 1 ]
    then
    echo "Invalid Arguments"
    echo "Usage: $(basename $0) Number"
    exit 1
    fi

    seq -s'*' 1 "$1" | bc
    # End of Script

    There's no for loops or infinite calculations... Run as fast as ANSI C ...

    ReplyDelete
  15. But they want for loops....
    Would be:

    #!/bin/bash
    # using one command line parameter
    if [ $# -ne 1 ]
    then
    echo "Invalid Arguments"
    echo "Usage: $(basename $0) Number"
    exit 1
    fi

    echo "a=1;for (i=1;i<=$1;i++) a=a*i;a" | bc
    # End of script

    Also as fast as ANSI C

    ReplyDelete
  16. i completely disappointed .......

    ReplyDelete
  17. HAVE A LOOK ON THIS
    X=1
    Y=1
    echo "enter the number N"
    READ N
    WHILE [ $X -LE $N ]
    DO
    Y=`EXPR $Y \* $X`
    X=`EXPR $X + 1`
    DONE
    ECHO " THE FACTORIAL OF $N IS $Y"

    ReplyDelete
  18. you guys are utterly useless..... i just want a bloody range and you won't give me it. Commit

    ReplyDelete
  19. Hi There,

    Smokin hot stuff! You’ve trimmed my dim. I feel as bright and fresh as your prolific website and blogs!

    I'm new to the world of programming and I am really under pressure to learn python at the moment for a mature student college course. So im looking for help on how to get python pandas installed on ubuntu 16.04, specifically pandas datareader. I might need more help later too, to get my head around this. I have no programming experience and very little linux experience.

    I'm doing the basic introduction on python and stuck on the second part.

    import pandas_datareader.data as web







    Thank you very much and will look for more postings from you.


    Thanks a heaps,
    Irene Hynes

    ReplyDelete