A shell script that receives even number of filenames as its arguments and copies the contents of the files at the odd-numbered positions to the files at the immedietly following even number postions .

This script will give you a brief idea on 'set' and 'shift' command... If you have any doubt on the working,you can use echo command to print the instance values. It will help you to debug problems when you write new scripts....





x=$*              #To set all the arguments in a variable 'x'
c=$#        #To set the total number of arguments in variable 'c'

rem=$(( $c % 2 ))

if [ $rem -ne 0 ]
then
    echo Error...!!!
    echo Please give even number of arguments..
    exit
fi


while [ 1 -le $c ]
do
c=`expr $c - 2`     #To decrement the variable 'c' by 1
shift $c        #To shift the total arguments by count 'c'    
cat $1 >>$2 
set $x            #To reset the arguments by the previous arguments
done


This entry was posted by Unknown. Bookmark the permalink.

Leave a Reply