Shell Script to read source file and copy it to target file

in Categories Decision Making last updated April 5, 2008
#!/bin/bash
# Shell to read source file and copy it to target file. If the file
# is copied successfully then give message 'File copied successfully'
# else give message 'problem copying file'
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter soruce file name : "
read src
echo -n "Enter target file name : "
read targ
 
if [ ! -f $src ]
then
	echo "File $src does not exists"
	exit 1
elif [ -f $targ ]
then
	echo "File $targ exist, cannot overwrite"
	exit 2
fi
 
# copy file 
cp $src $targ
 
# store exit status of above cp command. It is use to 
# determine  if shell command operations is successful or not
status=$?
 
if [ $status -eq 0 ]
then
	echo 'File copied successfully'
else
	echo 'Problem copuing file'
fi

Shell Script to read price of an article and calculate discount

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Script to read price of an article. If the price is less than 100
# then display "No discount" else give a discount of 10%. Display
# the price of article after discount
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter price of an article : "
read price
 
if [ $price -lt 100 ]
then
   echo "No discount "
   d=0 # 0 means no discount
else
   echo "10% discount "	
   d=$(( $price  * 10 / 100 )) # 10% discount
fi
# how much user need to pay? after discount
pay=$(( $price - $d ))
echo "You need to pay INR. $pay"

Script to display sum of two number and to do calculations such as +, -, / etc

in Categories Shell Math last updated April 15, 2008
#!/bin/bash
# Script to display sum of two number and to do calculations such as +, -, / etc
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo "**** My calculator ****"
echo "M A I N - M E N U"
echo "1. Multiplication"
echo "2. Subtraction"
echo "3. Remainder"
echo "4. Divide"
echo -n "Please select your choice (1-4) : "
read choice
 
echo -n "Enter your first number : "
read n1
echo -n "Enter your second number : "
read n2
 
if [ $choice -eq 1 ]
then
	answer="$n1 x $n2 = $(( $n1 * $n2 ))"
elif [ $choice -eq 2 ]
then
	answer="$n1 - $n2 = $(( $n1 - $n2 ))"
elif [ $choice -eq 3 ]
then
	answer="$n1 % $n2 = $(( $n1 % $n2 ))"
elif [ $choice -eq 4 ]
then
	answer="$n1 / $n2 = $(( $n1 / $n2 ))"
else
	echo "Sorry please select number between 1-4 only"
	exit 1
fi
echo $answer

Shell script to read 3 numbers and find the greaters of the three

in Categories Shell Math last updated April 4, 2008
#!/bin/bash
# Shell script to read 3 numbers and find the greaters of the three
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
echo -n "Please enter three numbers (separate number by space) : "
read a b c
# compare a with b and c. Note -a is logical and operator
if [ $a -gt $b -a $a -gt $c ]
then
   big=$a
elif  [ $b -gt $a -a $b -gt $c ] # compare b with a and c
then
   big=$b
elif  [ $c -gt $a -a $c -gt $b ] # compare c with a and b
then
   big=$c
elif [ $a -eq $b -a $a -eq $c -a $b -eq $c -a $c -eq $b ] # see if all of them are equal or not
then
   big="All three numbers are same (equal)"
else # something must be wrong if we are here, like one of number is character such as 'A'
   big="Can not guess greaters of three numbers"
fi
# display result 
echo "Result : $big"