Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with the following below and I also added a picture below the questi

ID: 3881358 • Letter: I

Question

I need help with the following below and I also added a picture below the question to show how the coding I am used to. I am using vi to write the code. Thanks:

Write a shell script which:
1. accepts a file name (ask the user for the file)
2. checks if file exists
3. if file exists, copy the file to the same name + .bak + the current date (if the backup file already exists ask if you want to replace it). The syntax to copy a file is:

cp file1 file2 (this will copy file1 to file2)


When done you should have the original file and one with a .bak at the end.

For example, assume the file is called test.txt.

First time you make a backup the script will rename the file to test.txt.bak

Next time you try to backup test.txt, you need to ask if the user wants to backup the file since it already exists. If the answer is "no", no backup was created. If the answer is "yes" then copy the backup to test.txt.bak.current-date (current date should be formatted to just print year-mm-dd)

cat function.sh #1 bin/bash function check()

Explanation / Answer

#!/bin/bash

# read the filename input

echo -n "Enter the name of the file : "

read inFile

# check if file exists

if [ -e /home/$inFile ]

then

               # check if backup file exists          

               if [ -e /home/$inFile".bak" ]

               then      

                              echo -n "Do you want to take backup again? "

                              read backup_flag

                              # ask if user wants to create another backup                       

                              if [ "$backup_flag" = "yes" ]

                              then                     

                                             DATE=`date +%Y-%m-%d`

                                             cp /home/$inFile /home/$inFile".bak."$DATE

                                             echo "$inFile.bak.$DATE created"

                              else

                                             echo "No backup created"

                              fi

               # if backup file doesn't exist create backup file     

               else

                              cp /home/$inFile /home/$inFile".bak"

                              echo "$inFile.bak created"

               fi                                          

else

               echo " File doesn't exist"

fi

# end of script