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
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
Labels:
shell script
|
0
comments
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
read usrName
echo "\t\t\tNOTE : Press [ Ctrl+d ] To Broadcast"
write $usrName
Labels:
shell script
|
0
comments
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
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
Labels:
shell script
|
0
comments
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
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
Labels:
shell script
|
0
comments
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
Labels:
shell script
|
0
comments
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 "
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 "
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"
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"
Labels:
shell script
|
4
comments
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
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
Labels:
shell script
|
0
comments
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
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
Labels:
shell script
|
2
comments
Subscribe to:
Posts (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2012
(79)
-
▼
August
(9)
- Write a script to copy the file system from two di...
- Write a shell script to broadcast a message to a s...
- Write a shell script to find the global complete p...
- write a shell script to fetch the data from a file...
- Write a shell script to accept filename and displa...
- Accept strings and replace a string by another string
- Write a shell script to Accept number and check th...
- Write a shell script to accept the string and chec...
- Write a shell script to accept numbers and perform...
-
▼
August
(9)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.