9A) shell script to generate 'bundle.sh' to recover deleted file contents with the file. [VTU SS&OS]

vtu ss & os 9a
Code:
Explanation:
This program models a file recovery logic, It looks difficult. But its logic is very easy. We write code to recover a file in a seperate file, bundle.sh. Then execute bundle.sh to get back what was deleted. see how it works. I've tried my best to put what i know into words. Hope you get it.

if [ $# -eq 0 ]
then
echo "No arguments passed"
exit 1
fi

if no command line arguments are passed (names of files to be backed up are expected actually), we print appropriate message and exit. fi represents end of  if.

for i in $*
do

for each command line argument(name of file) we should back up its "Name+Shell code to recovery it+The contents to recover after restoring file".
we do it for each file using for loop.
$* represents total number of files, for each 'i' in $* means for each file, we write its code to recover.

echo "echo "code to recreate the file $i ""

This will produce a output as echo "code to recreate the file $i " (content in bold letters), on the output screen actually. But since we are redirecting output to a bundle.sh fileecho "code to recreate the file $i " this line will be printed in bundle.sh file.

echo "cat>$i<<endf"
cat $i 
echo "endf"

this line prints cat>$i<<endf  to bundle.sh. which is nothing but (cat>fiilename and hitting enter button on your keyboard).
remember we can write contents to file using cat>filename, and end writing contents by clicking ctrl+d. same this done here. but code to do so is pasted in bundle.sh so that when we execute the bundle.sh, we can restore contents of file.

after cat>i is written to bundle.sh, we need to feed the input to be stored in that i file.
this is done using "cat $i" . This writes contents of i file to bundle.sh.

'endf' here serves as ctrl+d to indicate end of file.

So what we did here was, typing cat>filename and hitting enter (endf which is nothing but \n) on terminal, writing contents of file. and indicating end of input to file by ctrl+d.

echo "echo" Creation done " "

This prints the following to bundle.sh, echo" Creation done ".

done
exit 0

done indicates end of loop body.

----------------------------------------------------------------------------
BUNDLE.SH:

So basically what we have now bundle.sh is now this,
echo "code to recreate the file $
cat>$i<<endf
<content of i'th file written using cat $i above>
endf

which is nothing but code to write a content to a file with a  name equal to content of $i.
When we execute this bundle.sh in shell, we will get back what was deleted. Isn't it? :p


Remember
executing this code is done by this command,
sh 9a.sh a.txt b.txt>bundle.sh


Cool. Done. Sit back and grab your pop corn now.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!