postheadericon Write a script to copy the file system from two directories to a new directory in such a way that only the latest file is copied in case there are common files in both the directories.

# read directory name
echo "Enter First Directory : \c"
read dir1

echo "Enter Second Dirctory : \c"
read dir2

# check inputed name is directory or not
if [ ! -d $dir1 ]
then
echo "DIRECTORY [ $dir1 ] NOT EXIST"
exit 1
fi


if [ ! -d $dir2 ]
then
echo "DIRECTORY [ $dir2 ] NOT EXIST"
exit 1
fi


# copy all file names in to a text file
ls $dir1 > dir1.txt
ls $dir2 > dir2.txt

mkdir TargetDir

for FileFromDir1 in `cat dir1.txt`
do
flag=0
for FileFromDir2 in `cat dir2.txt`
do
# compare file name is same or not
if [ "$FileFromDir1" = "$FileFromDir2" ]
then

#f file name from directory1 is match to file name of directory2 [ flage is true ]
flag=1

# short both file by time and select first file
record=`ls -lt $dir1/$FileFromDir1 $dir2/$FileFromDir2 | head -n 1`


# copy [ Dir/filename ] of selected file
filePath=`echo $record | cut -d " " -f 8`


# copy selected file to the tageted directory
cp $filePath TargetDir

fi
done

#if file name from directory1 is does NOT match to file name of directory2 [ copy to TargetDir ]
if [ $flag -eq 0 ]
then
cp $dir1/$FileFromDir1 TargetDir
fi
done

# copy rest of file from directory2 to TargetDir
for FileFromDir2 in `cat dir2.txt`
do
flag=0
for FileFromDir1 in `cat dir1.txt`
do
if [ "$FileFromDir2" = "$FileFromDir1" ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
cp $dir2/$FileFromDir2 TargetDir
fi
done

postheadericon Write a shell script to broadcast a message to a specified user or a group of users logged on any terminal.

echo "Enter user Name."
read usrName
echo "\t\t\tNOTE : Press [ Ctrl+d ] To Broadcast"
write $usrName

postheadericon Write a shell script to find the global complete path for any file.

clear
echo "Enter File Name : \c "
read fileName

if [ -f $fileName ]
then
str=`find $fileName`
path=`pwd`
echo "Full path of file is $path/$str"

else
echo "file [ $fileName ] not exist in \c "
pwd
fi

postheadericon write a shell script to fetch the data from a file and display data into another file in reverse order.

echo "Enter File Name : \c "
read fileName



if [ -f $fileName ]
then
str=`cat $fileName`

len=`echo $str|wc -c`

i=$len
while [ $i -ge 1 ]
do
temp=$temp`echo $str|cut -c $i`
i=`expr $i - 1`
done

echo $temp>Result_ex2f.txt
cat Result_ex2f.txt

else
echo "file [ $fileName ] not exist in \c "
pwd
fi


postheadericon Write a shell script to accept filename and displays last modification time if file exists, otherwise display appropriate message.


clear
echo "Enter filename"
read fileName

if [ -f $fileName ]
then
echo "Modification time of [ $fileName ] is \c "
ls -l $fileName | cut -c 37-41
else
echo "file [ $fileName ] not exist in \c "
pwd
fi

postheadericon Accept strings and replace a string by another string

clear
echo "Enter string : \c"
read str

echo "\n\nWhat you want to search from [ $str ] : \c"
read sStr

echo "\n\nEnter string to replace : \c"
read rStr

str=`echo $str | sed s/$sStr/$rStr/`

echo "\n\nstring replaced successfully \n New string is : $str "

postheadericon Write a shell script to Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number.


clear
echo "Enter Number : "
read no

count=0
total=0


if [ `expr $no % 2` -eq 0 ]
then
echo "NUMBER IS EVEN"
else
echo "NUMBER IS ODD"
fi

while [ $no -ne 0 ]
do
a=`expr $no % 10`
no=`expr $no / 10`
total=`expr $total + $a`
count=`expr $count + 1`
done


echo "Sum of All Digit : $total"
echo "Total No. of Digit : $count"

postheadericon Write a shell script to accept the string and checks whether the string is palindrome or not.


clear
echo "Enter String : \c"
read str

len=`echo $str|wc -c`
len=`expr $len - 1`

echo "length of String : "$len

i=1
while [ $i -le $len ]
do
revstr=`echo $str|cut -c$i`$revstr
i=`expr $i + 1`
done

if [ "$revstr" = "$str" ]
then
echo "Your string is palindrome"
else
echo "Your string is not palindrome"
fi

postheadericon Write a shell script to accept numbers and perform addition, subtraction, division and multiplication.


echo "Enter Number 1 : "
read a
echo "Enter Number 2 : "
read b

ans=`expr $a + $b`
echo "Addition : " $ans

ans=`expr $a - $b`
echo "Subtraction : " $ans

ans=`expr $a / $b`
echo "Division : " $ans

ans=`expr $a \* $b`
echo "Multiplication : " $ans

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.