Write a shell script called backup.bash that accepts a filename. Then your scrip
ID: 3631084 • Letter: W
Question
Write a shell script called backup.bash that accepts a filename.Then your script should check if the file exists or not.
If the file exists, copy the file to the same_name.bak.
If the backup file already exists, your script should ask if you want to replace it.
A sample run of the script will be like below:
$ ./backup.bash
Usage ./backup.bash filename
This is another sample run.
$ ./backup.bash intro.doc
intro.doc doesn.t exist.
This is another sample run.
$ ./backup.bash intro.txt
Create a backup file called intro.txt.bak
This is another sample run.
$ ./backup.bash intro.txt
intro.txt.bak already exists.
Do you want to overwrite? Yes
Create a backup file called intro.txt.bak
Explanation / Answer
if [ $# != 1 ] then echo " Usage $0 filename " exit fi if [[ -f $1 ]] then if [[ -f $1.bak ]] then echo " $1.bak already exists " echo "Do you want to overwrite? type yes or no" read var if [ $var = yes ] then cp $1 $1.bak echo "Create a backup file called $1.bak" fi if [ $var = no ] then exit fi else cp $1 $1.bak echo "Create a backup file called $1.bak" fi else echo "$1 doesnt exist." fi
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.