Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

12 January 2014

Shell Script to print Pyramid of Numbers

22 comments


Q. How do I print pyramid of numbers in Bash


Ans.
how to print number pyramid in ubuntu

Before writing bash script lets understand how we going to print this pattern. We do it in two part, first we are going to print part 1 and then we print the part 2. As you can notice from above figure that, in 2nd part we are printing number in reverse order till 1.

#!/bin/bash

#Taking input
read -p "Enter Number:" number

#Outer loop for printing number of rows in pyramid
for((row=1;row<=number;row++))
do

    #Loop for printing required spaces
    for((spaces=row;spaces<=number;spaces++))
    do
        echo -ne " "
    done

    #Loop for printing 1st part
    for((j=1;j<=row;j++))
    do
        echo -ne "$j"
    done

    #Loop for printing 2nd part
    for((l=(row-1);l>=1;l--))
    do
        echo -ne "$l"
    done

    #echo for printing new line
    echo 
done
    
Read More...

02 August 2013

Shell Script to print pyramid of Stars

84 comments

Q. How do I print pyramid of stars using function in Bash


Ans.

bash script to print pyramid of stars
Before writing bash script lets understand how we going to print this pattern. We do it in two part, first we are going to print part 1 and then we print the part 2.

#!/bin/bash
makePyramid()
{
  #Here $1 is the parameter you passed with the function i,e 5
  n=$1;

  #outer loop is for printing number of rows in the pyramid
  for((i=1;i<=n;i++))
  do

      #This loop print spaces required
      for((k=i;k<=n;k++))
      do
        echo -ne " ";
      done

      #This loop print part1 of the the pyramid
      for((j=1;j<=i;j++))
      do
      echo -ne "*";
      done

      #This loop print part 2 of the pryamid.
      for((z=1;z<i;z++))
      do
      echo -ne "*";
      done
      
      #This echo used for printing new line
      echo;
  done
}

#calling function

#change number according to your need
makePyramid 5

Read More...

13 April 2013

Shell Script to find Greatest of four numbers using If-Else-If Statement

22 comments

Q: How do I write Bash Script to find Greatest of Four Numbers using If-Else-If Statement


Ans:

#!/bin/bash

read -p "Enter first Number:" n1
read -p "Enter second Number:" n2
read -p "Enter third Number:" n3
read -p "Enter fourth Number:" n4

if((n1>n2)) ; then
 if((n1>n3)); then
   if((n1>n4)); then
     echo "$n1 is a Greatest Number"
   else
     echo "$n4 is a Greatest Number"
   fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
elif((n2>n3)); then
  if((n2>n4)); then
     echo "$n2 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
fi
Read More...

08 December 2012

Shell Script to find Armstrong number between 1 to 500

3 comments

Q. How do I write Bash Script to find Armstrong number


Ans:

#!/bin/bash
#Script to find armstrong number till 500, you can change it 
i=1
while((i<=500))
do
c=$i
d=$i
b=0
a=0
#checking each number
while((c>0))
do
#separating each digit
a=$((c%10))
#finding cube of each digit and adding them
b=$((b + a*a*a))
c=$((c/10))
done
if((b==d)); then
echo "$i"
fi
i=$((i+1))
done
Read More...

03 December 2012

Shell Script to find factorial of a number using Recursion

2 comments

Q. How do I find factorial of a number using recursion function in Bash


Ans:

#!/bin/bash
#Recursive factorial function
factorial()
{
local=$1
if((local<=2)); then
echo $local
else
f=$((local -1))
#Recursive call
f=$(factorial $f)
f=$((f*local))
echo $f
fi
}
#main script
read -p "Enter the number:" n
if((n==0)); then
echo 1
else
#calling factorial function
factorial $n
fi
Read More...

30 November 2012

Shell Script to find "a" to the power "b" using function

1 comment

Q. How do I write Shell Script function to find "a" to the power "b"


Ans.

#!/bin/bash
#function to find "a" to the power "b"
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
#Printing the result
echo "$1 to the power $2 is $result"
}

#main script
read -p "Enter number:" num
read -p "Enter power:" pow

#calling above function
power $num $pow
Read More...

24 November 2012

Shell Script to check whether a given String is Palindrome or not

1 comment

Q: How do I check whether a Input String is Palindrome or not in Linux and Unix


Ans:

#!/bin/bash
read -p "Enter the String:" n
len=${#n}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${n:$i:1}"
c2="${n:$len-$i-1:1}"
#comparing single single charcters from begining and end
if [ $c1 != $c2 ];then
flag=0
echo "String is not palindrome"
break
fi
done
if(( $flag==1)); then
echo "Input String is Palindrom"
fi
Read More...

23 November 2012

Shell Script to reverse a String

8 comments

Q: How do I reverse a string in Linux/Unix?


Ans:

#!/bin/bash
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"
Read More...

05 November 2012

Shell Script to Print Fibonacci Series

1 comment

Q. How do I print fibonacci series in Bash


Ans:

#!/bin/bash
c=0
a=1
b=1
read -p "Enter limit of fibonacci Series:" n
echo -n "$a "
echo -n "$b "
#Fibonacci series logic
while((c<n))
do
c=$((a+b))
echo -n "$c "
a=$b
b=$c
done
echo -e "\n"
Read More...

25 October 2012

Find the length of the String in Bash

1 comment

Q: How do I find the length of the String in Bash


Ans: There are number of ways to find the length of the String in Bash.

Method 1:

$ word="technical world for you"
$ echo "${#word}"
23

Read More...

23 October 2012

Shell Script to Check Hard disk Revolutions Per Minute (RPM) Speed

3 comments

Q: What is RPM of Hard disk?


Ans: RPM Short form for Revolutions Per Minute, RPM is used to help determine the access time on computer hard drives. RPM is a measurement of how many complete revolutions a computer's hard drive makes in a single minute. The higher the RPM, the faster the data will be accessed; for example, if you were comparing two hard drives, one with 5400 RPM and another with 7200 RPM, the hard drive with a 7200 RPM will be capable of accessing data much faster than the 5400 RPM drive. However, it is also important to note that a 5400 RPM drive will be much cheaper than a 7200 RPM drive.

While it may not be a noticeable speed difference when loading small files, the RPM of a hard drive can make a signiicant difference when loading large files or several hundred or thousand files.

Q: How do I check my Hard disk RPM speed using terminal?

Ans: To check RPM of your Hard disk open terminal (Ctrl + Alt + t) and write following command

$ sudo hdparm -I /dev/sda | grep Rotation
Nominal Media Rotation Rate: 5400

Enjoy :)

Read More...

22 October 2012

Swapping two numbers using Multiplication and Divide

5 comments

Q. How do I swap two numbers without using addition and subtraction in Bash


Ans:

#!/bin/bash
read -p "Enter first number :" first
read -p "Enter second number:" sec
echo ""
echo -e "Number before swapping is $first and $sec \n"
#swapping logic
first=$((first*sec))
sec=$((first/sec))
first=$((first/sec))
echo -e "Number after swapping is $first and $sec \n"
Read More...

19 September 2012

Multi Line comment in Shell Script

413 comments

Q. How do I use Multi Line comment in Bash


Ans:

We know that we can comment a line using '#' in Shell Script, but what if we want to comment a huge block of code? If we use '#' to comment that block then this can be very tedious task. Rather than using single line comment we can use multi line comment. In this tutorial i will show you how to use multi-line comment in Shell Script.

In Shell Script multi line comment can be of form

<<'COMMENT'

what ever written here is a comment

COMMENT

Let see example of multi line comment

#!/bin/bash
echo "Hii guys"
#starting of mullti line commnent
<<'COMMENT'
read a
echo "No line in this block will execute"
COMMENT
echo "Hello Guys"
exit 
Read More...

11 August 2012

Selection Sort using Shell Script

Leave a Comment

Q. How do I write selection sort in Bash


Ans:

#!/bin/bash
echo "enter the number"
read n
echo "enter number in an array"
for((i=0;i<n;i++))
do
read arr[$i]
done
#logic for selection sort
for((i=0;i<n-1;i++))
do
small=${arr[$i]}
index=$i
for((j=i+1;j<n;j++))
do
if((arr[j]<small))
then
small=${arr[$j]}
index=$j
fi
done
temp=${arr[$i]}
arr[$i]=${arr[$index]}
arr[$index]=$temp
done
#printing sorted array
echo "printing sorted array"
for((i=0;i<n;i++))
do
echo ${arr[$i]}
done
Read More...

10 August 2012

Insertion Sort Using Shell Script

3 comments

Q. How do I write Insertion Sort in Bash


Ans:

#!/bin/bash
echo "enter the number"
read n
echo "enter number in an array"
for((i=0;i<n;i++))
do
read arr[$i]
done
#logic for insertion sort
for((i=1;i<n;i++))
do
j=$i-1
temp=${arr[$i]}
while((j>=0 && arr[j]>temp))
do
arr[$j+1]=${arr[$j]}
j=$j-1
done
arr[j+1]=$temp
done
#printing sorted array
echo "printing sorted array"
for((i=0;i<n;i++))
do
echo ${arr[$i]}
done
Read More...

14 June 2012

Shell Script to find Greatest and Smallest number

3 comments

Q. How do I write shell script to find Smallest and Greatest number in an array


Ans:

#!/bin/bash
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"

Read More...

13 June 2012

Shell Script to Sort Number in Descending Order

2 comments

Q. How do I write Shell Script to print number in descending order


Ans:

#!/bin/bash
echo "enter maximum number"
read n
# taking input from user
echo "enter  Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
#printing the number before sorting
echo "  Numbers in an array are:"
for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -lt ${nos[$j]}  ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number in descending order
echo -e "\nSorted Numbers "
for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
Read More...

31 March 2012

Shell Script to swap two numbers without using third variable

4 comments

Q. How do I swap two numbers using addition and subtraction in Bash


Ans:

#!/bin/bash
echo "enter first number"
read a
echo "enter second number"
read b
echo "a before swapping is $a and b is $b"
#swapping
a=$((a+b))
b=$((a - b))
a=$((a-b))
echo "a after swapping is  $a and b is $b"
Read More...

30 March 2012

Shell Script for Bubble Sort

9 comments

Q. How do I write Bubble sort in Bash


Ans:

#!/bin/bash
echo "enter maximum number"
read n
# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
#printing the number before sorting
echo "Numbers in an array are:"
for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]}  ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number
echo -e "\nSorted Numbers "
for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
Read More...

13 February 2012

Logical Operators in Shell Scripts

Leave a Comment

Here are Logical operators that are used during Shell Script.

OPERATOR DESCRIPTION
cond1 -a cond2 True if cond1 and cond2 are True(Performs AND operation)
cond1 -o cond2 True if con1 or cond2 is True (Perform OR operation)
!cond1 True if cond1 is false

Example


$ cat > rop.sh
#!/bin/bash
a=25
b=29

if [ $a -gt 20 -a $b -gt 25 ] ; then
echo "both condition stisfied"
fi

if [ $a -gt 25 -o $b -gt 25 ] ; then
echo "only one condition is satisfied"
fi
Read More...