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
Tuesday, October 27, 2009
Posted by venu k
23 comments | 8:41 AM
Monday, October 26, 2009
Posted by venu k
1 comment | 9:50 AM
#!/bin/bash
# Usage: forloop2.sh or forloop2.sh number
# Example using for loop
# Prints following format
# *
# * *
# * * *
# * *
# *
n=${1-10} # If command line argument not supplied default is 10
t=`expr $((2*$n - 1 ))`
a=$n
b=$(($a+1))
for (( i=1 ; i<=$n ;i++ ))
do
echo -e -n "\033[47m"
a=$(($b-1))
b=$a
k=1
for (( j=1 ; j<=$t ; j++ ))
do
if [ $j -eq $a ]
then
echo -e "\033[43m * \033[47m\c"
if [ $k -lt $i ]
then
a=$((a+2))
let k++
fi
else
echo -n " "
fi
done
echo ""
done
n=$(($n-1))
a=1
b=$a
for (( i=$n ; i>=1 ;i-- ))
do
echo -e -n "\033[47m"
a=$(($b+1))
b=$a
k=1
for (( j=1 ; j<=$t ; j++ ))
do
if [ $j -eq $a ]
then
echo -e "\033[43m * \033[47m\c"
if [ $k -lt $i ]
then
a=$((a+2))
let k++
fi
else
echo -n " "
fi
done
echo ""
done
echo -e "\033[0m"
Output:
Saturday, October 10, 2009
Posted by venu k
8 comments | 9:55 AM
Defining the Shell Type
To make a bash script crate a new file with a starting line like:
#!/bin/bash
It is important that the path to the bash is proper and
first two characters must be “#!” . The shell from which you
are starting the script will find this line and hand the whole
script over to bash. Without this line the script would be
interpreted by the same type of shell as the one, from which
it was started.But since the syntax is different for all shells,
it is necessary to define the shell with that line.
Some typical interpreters for shebang lines:
• #!/bin/bash — Execute using the Bourn-again shell
• #!/bin/sh — Execute using the Bourn shell (if available) or a
Bourne compatible shell such as Bourne-again shell
• #!/usr/bin/ksh --- Execute using korn shell
Note:
In computing, a shebang (also called a hashbang, hashpling,
pound bang, or crunchbang) refers to the characters "#!" when
they are the first two characters in a text file. In a Unix-like
operating system, the program loader takes the presence of these
two characters as an indication that the file is a script, and
tries to execute that script using the interpreter specified by
the rest of the first line in the file. For instance, shell
scripts for the Bourne shell start with the first line:
#!/bin/sh
Executing a shell script
Once a shell script is created, there are several ways to
execute it. However, before executing a shell script you must
assign proper permissions. Using chmod command you can change
file permissions.For example giving execute permission to the
file “temp”:
chmod +x temp
First create a sample shell script “temp”.
$cat > temp
#!/bin/bash
echo "My name is `whoami`"
echo "Users are `users`"
echo "OS name is `uname -s`"
echo "current shell is $SHELL"
ctrl+d
Method 1 to execute a shell script:
./scriptname
You must assign execute permission to the script.
With this method script is executed by interpreter which is
defined at sha-bang line.
Note: Script path should be relative to the current
directory then only this method works. If you want to execute a
script in the current directory you use ./scriptname. In fact if
you want to execute a script in the parent directory of your
current location you can use ../scriptname , as . means current
and .. means parent.
Run temp script.
[root@localhost shell]# ./temp
My name is root
Users are root root
OS name is Linux
current shell is /bin/bash
now change sha-bang line with #!/bin/ksh and execute the script
[root@localhost shell]# ./temp
bash: ./temp: /bin/ksh: bad interpreter: No such file or directory
now change sha-bang line with #!/bin/zsh and execute the script
[root@localhost shell]# ./temp
My name is root
Users are root root
OS name is Linux
current shell is /bin/bash
So wrong path to interpreter or bad interpreter gives error message.
Method 2 to execute a shell script:
you can execute the bash script by specifying the filename
as an argument to the bash,sh,zsh commands.
This is a bad way to call a script, since it will override
the #! at the beginning of the script.Depending on command you
used, current shell spawns relative subshell.
$sh temp
or
$bash temp
or
$zsh temp
Output:
My name is root
Users are king root root sai venu
OS name is Linux
Current shell is /bin/bash
Note: It is good to use proper command. Suppose your script is
korn shell script,then run it as:
ksh temp
Caution: In this method shell runs script in sub shell.
So you can't access some shell builtin variable values.
For example run bellow shell script:
#!/bin/bash
lines=$LINES
cols=$COLUMNS
echo $lines
echo $cols
Output:
$ sh sample
It prints nothing
Note: To solve this problem export current shell variables
using “export” command.Before executing script, export all
variables you need.
Example:
$ export LINES COLUMNS
$ sh sample
24
80
Method 3 to execute a shell script:
you can execute the bash script by specifying the filename
as an argument to the . (dot) or source command. In this method
script will be run in current shell.
Example:
$ . temp
or
$ source temp
Output:
[root@localhost shell]# . temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash
[root@localhost shell]# source temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash
Caution:
Try to unset variables at the end of the script. Otherwise
you will get add results. Variable and functions used in the
script will be alive after execution of the script.
Example:
create two files
cat > sample2
n=100
m=200
echo "n=$n m=$m"
ctrl+d
cat > sample3
n=$((n+m))
m=$((m+25))
echo "n=$n m=$m" ctrl+d
Execute scripts using sh command
[root@localhost shell]# sh sample2
n=100 m=200
[root@localhost shell]# sh sample3
n=0 m=25
Now execute scripts using . Or source command
[root@localhost shell]# . sample2
n=100 m=200
[root@localhost shell]# . sample3
n=300 m=225
So be careful. Try to unset variables at the end of the script.
Method 4 to execute a shell script:
You can also execute scripts by just typing its name alone.
However, for this method to work, the directory containing the
script must be defined in your PATH variable and file must has
executable permission.
For example run “temp” script directly
[root@localhost shell]# temp
bash: temp: command not found
Now add your home directory to PATH variable(I am assuming
script located at home directory).
PATH=${PATH}:$HOME
Now execute script with name, you will get result.
[root@localhost ~]# temp
My name is root
Users are king root root sai venu
OS name is Linux
current shell is /bin/bash
Suppose you changed “temp” script name to “ls”, what will
happen, which result you will get.
[root@localhost ~]# mv temp ls
[root@localhost ~]# ls
Desktop ls sample sample2 sample3
So you will get “ls” command output. Why ?
When you type name of any command at $ prompt, what shell
do is it first look that command in it's internal part(called
as internal command,which is part of shell itself, and always
available to execute),if found as internal command shell will
execute it, if not found then shell will look PATH setting,
and try to find our requested commands executable file in all
of the directories mentioned in PATH settings,if found it will
execute it, other wise it will give message
“bash:XXXX:command not found”.
So don't use command name as shell script.
Note:If you have two different files with same name in different
directories, which file will execute?
The directory which comes first in PATH setting will be executed.
Example:
First create a file “testscript” in home directory.
$ cat > testscript
whoami
ctrl+d
Then create a directory “bin” in home directory
$ mkdir bin
Now create a file with same name “testscript” in bin dirctory.
$ cd bin
$ cat > testscript
date
ctrl+d
Then add “bin” directory to PATH variable.If your “bin” directory
already in PATH setting leave it.
PATH=${PATH}:$HOME/bin
now check your path settings
[root@localhost ~]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:
/usr/lib/ccache:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/
bin:/usr/bin:/bin:/root/bin:/root
/root and /root/bin added to your PATH.
I added /root/bin directory before /root directory.
now execute “testscript” script
[root@localhost ~]# testscript
Sat Oct 10 06:57:14 IST 2009
Wednesday, October 7, 2009
Posted by venu k
4 comments | 8:19 AM
#!/bin/bash
# Usage: script.sh number
# A for loop example produces following output
#
# *
# * *
# * * *
# * * * *
# * * * * *
#
Method1
c=1
n=$1
echo -e "\033[47m\c" #colourizing output
for ((row=1;row<=n;row++))
do
for ((i=row;i<n;i++))
do
echo -n ' '
done
for ((k=1;k<=c;k++))
do
if [ $((k%2)) -eq 0 ]
then
echo -n " "
else
echo -e "\033[43m * \033[47m\c"
fi
done
for ((i=row;i<n;i++))
do
echo -n ' '
done
c=$((c+2))
echo
done
echo -e "\033[0m" #Restoring colours
unset c i k n row
Method2
#!/bin/bash
n=$1
t=$((2*n - 1 ))
a=$n
b=$((a+1))
echo -e -n "\033[47m"
for (( i=1 ; i<=$n ;i++ ))
do
a=$(($b-1))
b=$a
k=1
for (( j=1 ; j<=$t ; j++ ))
do
if [ $j -eq $a ]
then
echo -e "\033[43m * \033[47m\c"
if [ $k -lt $i ]
then
a=$((a+2))
let k++
fi
else
echo -n " "
fi
done
echo
done
echo -e "\033[0m"
Output:Posted by venu k
2 comments | 7:58 AM
A 'for loop' is a bash programming language statement which allows code to be repeatedly
executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process
within a bash script.
executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process
within a bash script.
For example, you can run UNIX command or task 5 times or read and process list of files using
a for loop. A for loop can be used at a shell prompt or within a shell script itself.
for loop syntax
Numeric ranges for syntax is as follows:
for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
done
Examples:
#!/bin/bah
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
Using “seq” command
#!/bin/bash
for i in $(seq 1 2 20)
do
echo "Welcome $i times"
done
To know more about “seq” command check manual using “man seq” command
Bash 3.0+ version supports following syntax also
#!/bin/bash
for i in {1..5}
do
echo "Welcome $i times"
done
Three expression syntax
for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done
Example:
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times..." done
Posted by venu k
3 comments | 6:10 AM
#!/bin/bash
# Usage: scriptname argument
# Here argument is height of pyramid
# Output would be pyramid pattern of stars
# 0 *
# 1 ***
# 2 *****
# 3 *******
# 4 *********
# 5 ***********
# 6 *************
# . ***************
# . *****************
# . *******************
# n-1 *********************
# ---\/--- | ---\/---
# n - 1 n - 1
#
clear
n=$1
row=1;
echo -e "\033[47m"
while [[ $row -le $n ]]
do
loop=1;
spaces=$((n-row))
stars=$((2*row - 1))
while [[ $loop -le $spaces ]]
do
echo -n ' '
let loop++
done
loop=1;
while [[ $loop -le $stars ]]
do
echo -e '\033[43m*\033[47m\c'
let loop++
done
loop=1;
while [[ $loop -le $spaces ]]
do
echo -n ' '
let loop++
done
echo
let row++
done
echo -e "\033[0m"
unset row loop spaces stars n
Output:[root@localhost blog]# sh while_pyramid 22
Posted by venu k
6 comments | 5:35 AM
#!/bin/bash
# Usage: scriptname argument
# Here argument is height of pyramid
# Output would be pyramid pattern of stars
# 0 *
# 1 ***
# 2 *****
# 3 *******
# 4 *********
# 5 ***********
# 6 *************
# . ***************
# . *****************
# . *******************
# n-1 *********************
# ---\/--- | ---\/---
# n - 1 n - 1
#
clear
n=$1
echo -e "\033[47m" #used for colourizing output
for ((row=1;row<=n;row++))
do
spaces=$((n-row))
stars=$((2*$row - 1))
for ((i=1;i<=spaces;i++))
do
echo -n ' '
done
for ((i=1;i<=stars;i++))
do
echo -e '\033[43m*\033[47m\c'
done
for ((i=1;i<=spaces;i++))
do
echo -n ' '
done
echo
done
echo -e "\033[0m"
unset n i spaces stars row
# Unset variables is a good programming practice
Output:
[root@localhost blog]# sh for_pyramid 22
Subscribe to:
Posts (Atom)