write a bash script Synopsis backup s size t tarDir d destDi

write a bash script:

Synopsis:

backup [s size] [t tarDir] [d destDir] < ext_list >

where tarDir and DestDir are supposed to be two valid directories.
Note that an argument between [ and ] is optional while an argument between < and > is

not.

The script backup moves all files from target directory tarDir (or current directory if this argument is missing), with extensions in < ext_list >, into destination directory destDir. If op- tion -d destDir is missing, then create a subdirectory in the current directory called myArchive, to be used as a default desctination. When size option is present, only files exceeding it are concerned by the move.

Then, archive all moved files using the tar utility to create a single file, called myArchive.tar. You should have only myArchive.tar in your destination directory, if necessary, delete original files.
Call example:
backup -s 1000 -d /oldFiles/ pdf doc exe
In this case, move all files, exceeding 1000 bytes, with extensions pdf, doc or exe, from my current directory to /oldFiles/. Then, create myArchive.tar and remove the original moved files.

Important: options can appear in any order. The above call could also be: archive -d /oldFiles/ -s 1000 pdf doc exe

Solution

Hi Inform me If you need help

#!/bin/bash
if [ $1 = \'-s\' ]
then
size=$2
fi
if [ $1 = \'-t\' ] || [ $3 = \'-t\' ] >/dev/null 2>&1
then
if [ $1 = \'-t\' ]
then
tarDir=$2
count=2
else
tarDir=$4
count=4
fi
else
tarDir=`pwd`
fi

if [ $1 = \'-d\' ] || [ $3 = \'-d\' ] || [ $5 = \'-d\' ] >/dev/null 2>&1

then
if [ $1 = \'-d\' ]
then
desDir=$2
count=2
elif [ $3 = \'-d\' ]
then
desDir=$4
count=4
elif [ $5 = \'-d\' ]
then
desDir=$6
count=6
fi
else
desDir=myArchive/
mkdir myArchive
fi
#echo $size $tarDir $desDir $count
myarr=( \"$@\" )
x=0
y=0
count=$(( $count - 1 ))
for arg in \"${myarr[@]}\"
do
if [ $x -eq $count ]
then
filearr[$y]=$arg
count=$(( $count + 1 ))
y=$(( $y + 1 ))
fi
x=$(( $x + 1 ))
done

for file in \"${filearr[@]}\"
do

find $tarDir -maxdepth 1 -name \"*.$file\" -exec mv \'{}\' $desDir \\;
#find $tarDir -maxdepth 1 -size $sizeb -name \"*.$file\" -exec mv {} $desdir \\;

done
echo \"file moved\"

=========================================

I need to change the code littile bit and I am explaing some basics of shell scripting here

save this code to a file name backup.sh then run a command as chmod 755 backup.sh after that give input as mentioned in question .chmod 755 backup.sh
if you run a command like ls -ltr backup.sh you will get the following result
rwxr-xr-x 1 diganta ca 1002 Oct 21 23:54 backup.sh

where r means read permission,w means write permission and x means execute permission
here you have seen like rwx (first 3bit for user) 2nd 3bits for group last 3 bits for other.
to run a script x permission should be there

====================

chmod 755 backup.sh
if you run a command like ls -ltr backup.sh you will get the following result
rwxr-xr-x 1 diganta ca 1002 Oct 21 23:54 backup.sh

where r means read permission,w means write permission and x means execute permission
here you have seen like rwx (first 3bit for user) 2nd 3bits for group last 3 bits for other.
to run a script x permission should be there
to run this suppose you are giving the command like below
./backup.sh -t /builds/BPPCKTV/Diganta c
it means all the file with extension with c will move from /builds/BPPCKTV/Diganta to myArchive directory as
destination is not mentioned
Now you need to add some lines to your script as it demands to archive the file also

for file in \"${filearr[@]}\"
do
echo $file
find $tarDir -maxdepth 1 -name \"*.$file\" -exec touch {} \\; -exec mv \'{}\' $desDir \\;
#find $tarDir -maxdepth 1 -size $sizeb -name \"*.$file\" -exec mv {} $desdir \\;
echo \"file moved\"
done
cd $desDir
touch -t `date +%m%d0000` /tmp/$$
find . -type f -newer /tmp/$$ -exec tar -cvf myArchive.tar   \\;
rm /tmp/$$

I will explain this code to you

here filearr will store the file extension
now the find commmand will find the file with name ending with required extension and moved it to destination directory
and after that in destination directory you need to tar the file to myArchive.tar

write a bash script: Synopsis: backup [s size] [t tarDir] [d destDir] < ext_list > where tarDir and DestDir are supposed to be two valid directories. Note
write a bash script: Synopsis: backup [s size] [t tarDir] [d destDir] < ext_list > where tarDir and DestDir are supposed to be two valid directories. Note
write a bash script: Synopsis: backup [s size] [t tarDir] [d destDir] < ext_list > where tarDir and DestDir are supposed to be two valid directories. Note

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site