Showing posts with label Unix Shell Scripting. Show all posts
Showing posts with label Unix Shell Scripting. Show all posts

Thursday, November 1, 2012

How to find the number is Armstrong or not in Unix


#################################################
# To find the number is Armstrong or not                   #
#################################################

#!/bin/ksh
echo "This program is to find whether given number is Armstrong number or not”

# To make sure sufficient arguments
if [ $# -ne 1 ]
 then
   echo "Please provide one argument"
exit -1
fi

# Declaring and Assigning the values to the local variables
a=$1
s=0

# Logical part to find the Armstrong number
while [ $a -gt 0 ]
do
b=$(( $a%10 ))
s=$(( $s+$b*$b*$b ))
a=$(( $a/10 ))
done
if [ $s -eq $1 ]
then
 echo "The given number is the Armstrong number"
else
 echo "The given number is not Armstrong number"
fi
exit 0

How to find the sum of digits in the number in Unix


###########################################################
# To find the sum of digits in the number                 #
###########################################################

#!/bin/ksh

# Checking the required arguments
if [ $# -ne 1 ]
 then
   echo "Please provide one argument"
exit -1
fi

# assigning the arguments to the local variables
a=$1
s=0

# Logical part to find the sum of digits in the number
while [ $a -gt 0 ]
do
b=$(( $a%10 ))
s=$(( $s+$b ))
a=$(( $a/10 ))
done

echo "The sum of digits in $1 is $s"
exit 0

How to find whether string is palindrome or not in Unix


#############################################################
# To find whether the string is palindrome or not                                            #
#############################################################

#!/bin/ksh

# To make sure the sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please enter one argument"
 exit -1
fi

# Declaring and assigning the values to the local variables
b=`echo $1 | wc -c `
c=$(( $b-1 ))
d=$(( $c/2 ))
flag=1
m=1

# Logical part to handle the palindrome calculations
while [ $m -le $d ]
do
    e=`echo $1 | cut  -c$c`
    f=`echo $1 | cut  -c$m`
    if [ $e != $f ]
      then
          flag=0
          break
    fi
     c=$(( $c-1 ))
     m=$(( $m+1 ))
done

  if [ flag -eq 0 ]
 then
echo "The string is not a palindrome"
else
echo "The string is palindrome"
fi
exit 0

Saturday, October 6, 2012

How to find the Largest and Smallest number in the given numbers in Unix


###############################################################
# To find the Largest and Smallest number in the given numbers      #
###############################################################

#!/bin/ksh

# To make sure sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please provide the sufficient arguments"
  echo "bigsmall file_name"
  exit -1
fi

# Declaring and Assigning values to the local variables
set -A LIST `cat /clocal/fme/prod/forecast/padu/Lilly/numberlist.txt`
a=${#LIST[*]}
Small=${LIST[0]}
Big=${LIST[0]}

# Implementation of logic to find the biggest and smallest number
for i in ${LIST[*]}
do
 if [ $i -lt $Small ]
  then
  Small=$i
 elif [ $i -gt $Big ]
  then
  Big=$i
 fi
done
 echo "Smallest = $Small"
 echo "Biggest = $Big"

exit 0

How to find the biggest among 3 given numbers in Unix


##################################################################
# To find the biggest among 3 given numbers                                          #
##################################################################

#!/bin/ksh/

# To make sure sufficient arguments
if [ $# -ne 3 ]
then
echo "Please provide the three arguments"
exit -1
fi

# Declaring and assigning values to the local variables
A=$1
B=$2
C=$3

#Logical part to find the largest number among 3 numbers
if [ $A -gt $B  -a  $A -gt $C ]
   then
   echo "A $A is the biggest number"
        elif  [ $B -gt $A  -a  $B -gt $C ]
        then
        echo "B $B is the biggest number"
                elif  [ $C -gt $A -a  $C -gt $B ]
                then
                echo "C $C is the biggest number"

   elif  [ $A -eq $B -a $A -eq $C -a $B -eq $C -a  $C -eq $B ]
   then
  echo "All numbers are same"
fi

exit 0

Reverse the content in the file using Unix


#####################################################
# Reverse the content in the file                                       ##
#####################################################

#!/bin/ksh

# To make sure the sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please enter the file name to be reverse"
  exit -1
fi

# Implementation of logic to reverse the content in the file
if [ -f $1 ]
  then
    rever=`rev $1`
 echo "Original file is"
 cat $1
 echo "Reverse of the file is"
 echo $rever
fi

exit 0

How to combine the files Vertically or Horizontally in Unix


########################################################
# Combine the files vertically or Horizontally                        ##
########################################################

#!/bin/ksh

# To make sure sufficient arguments
if [ $# -ne 2 ]
then
  echo "Please provide the two file names to be combine"
  exit -1
fi

# Initializing the local variables
file1=$1
file2=$2
Hori=/clocal/fme/prod/forecast/padu/Lilly/Horizontal.txt
verti=/clocal/fme/prod/forecast/padu/Lilly/Vertical.txt
echo "Please choose the combination format"
echo "1) Horizontal"
echo "2) Vertical"
read ch
case $ch in
1) cat $file1>$Hori
   cat $file2>>$Hori;;
2) paste $file1 $file2>$verti;;
*) ;;
esac
exit 0

How to display the alternative numbers in the given number in UNIX


##########################################################
# To display the alternative numbers in the given number         #
##########################################################

#!/bin/ksh

echo "Please enter one number"
read num
echo $num>temp.txt
i=1
len=`cat temp.txt|wc -c`

#Logic implementation

while [ $i -le $len ]
do
  alseries=`echo $num|cut -c $i`
  echo $alseries
i=`expr $i + 2`
done

rm -f temp.txt

exit 0

Thursday, April 26, 2012

How to copy directories alone from one directory to other


find . -type d  -depth | cpio -dumpl "Target directory path"
-depth : Always true. Causes descent of the directory hierarchy to  be  done  so  that  all entries in a directory are acted on before the directory  itself.   This  can  be useful  when  find  is  used  with cpio(1) to transfer files that are contained in directories without  write
permission.

cpio: copy files to or from archives.
Options : d : Creating a leading directories when needed.
              u : Unconditional
             m : preserve time, modifications
               l : link files
              p : In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input, and writes the archive onto the standard output. A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unreadable.
         

Friday, March 30, 2012

How do I change the file access permissions in UNIX


Every file has following attributes:
Ø  owner's user ID ( 16 bit integer )
Ø  owner's group ID ( 16 bit integer )
Ø  File access mode word
'r w x -r w x- r w x' 
(user permission-group permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).


Run the UNIX script on server


I wrote one UNIX script which eventually runs for hours together, which does much functionality fetching the data from DB2 tables and formatting and again loading into another tables. If I run the script in my local system by connecting to the client secured network (VPN) , I can’t guarantee the complete execution of the script.

My friend Maddy suggested me to run the script on server using nohup command in UNIX; here how it is

nohup script.cmd “environment variables” >script.cmd.out 2>&1 &


Comparison of DOS and Unix command-line interfaces

The DOS (Disk Operating System) and Unix environment are looking same in appearance but there are many differences, here are the few.

such as to list the directories and sub directories and hidden directories available in a drive we use ls -lRa in UNIX and we use dir/p/od in DOS environment .

Wednesday, March 28, 2012

Scope of variables in UNIX

To restrict the scope of variables to the function which it requires, we use typeset without any options, here is the example how to use the typeset without any functions to achieve the desired result in complex and large programs.


#!/bin/ksh/ 

x=1 
y=2 
function myfun { 
x=3 
typeset y=4 
print "Inside function x is $x" 
print "Inside function y is $y" 
} 
myfun 
print "Outside function x is $x" 
print "Outside function y is $y" 
exit 0   

Sunday, March 18, 2012

Checking Arguments in Unix

Script with one argument to execute the further commands


if [ $# -ne 1 ]
then
   echo "Write your comments"
   exit -1 # exit the script
fi

$# stands for the number of arguments, it is number.
In if command you can change ne (not equal) to other comparing operator according to the requirements.
lt = Less than.
gt = Greater than.
le = Less than or equal.
ge = Greater than or equal.
eq= Equals.

Saturday, March 17, 2012

What is shebang in unix

The very first line in any UNIX shell script is shebang, Why should?
In UNIX the script is in Parent shell and the calculations will be done in child shell, in UNIX each shell has its own way of writing style, The script which we have right in our hands may or may not in that format, to make sure the parent shell execute calculations in the right child shell , we use she bang.
#!/bin/ksh (for korn shell)